Button's attribute

30 views
Skip to first unread message

gorskijoza

unread,
Aug 7, 2020, 6:49:43 AM8/7/20
to Kivy users support
Hi everyone,

one short question - how can I use button attrubute in python?

label1= ToggleButton(text='Basic\nlayer', group='b', background_normal= 'atlas://tabs/normal', background_down= 'atlas://tabs/down', color= (1, 1, 1, .6) if self.state == 'normal' else (1, 1, 1, 1) )

I want to use button state, to define text color.

Thanks

Elliot Garbus

unread,
Aug 7, 2020, 11:45:42 AM8/7/20
to kivy-...@googlegroups.com

Use bind, to associate the change in state with a method to change the color.

https://kivy.org/doc/stable/api-kivy.properties.html?highlight=bind#observe-using-bind

 

from kivy.app import App
from kivy.uix.togglebutton import ToggleButton


class BindApp(App):
   
def build(self):
        tb = ToggleButton(
text='Test Button',color=(1, 0, 0, 1))
        tb.bind(
state=self.color_change)
       
return tb

   
def color_change(self, instance, value):
       
print(f'ins {instance}, v: {value}')
        instance.color = (
1, 0, 0, 1) if value == 'normal' else (1, 1, 1, 1)


BindApp().run()

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/236361ff-7ec1-43d3-84ef-70621ae98f46o%40googlegroups.com.

 

gorskijoza

unread,
Aug 10, 2020, 1:32:02 AM8/10/20
to Kivy users support
Elliot,

thanks for your answer. Is it possible to use one method to change the color to more than one button - to have array of instances and values?

Dne petek, 07. avgust 2020 17.45.42 UTC+2 je oseba Elliot Garbus napisala:

Use bind, to associate the change in state with a method to change the color.

https://kivy.org/doc/stable/api-kivy.properties.html?highlight=bind#observe-using-bind

 

from kivy.app import App
from kivy.uix.togglebutton import ToggleButton


class BindApp(App):
   
def build(self):
        tb = ToggleButton(
text='Test Button',color=(1, 0, 0, 1))
        tb.bind(
state=self.color_change)
       
return tb

   
def color_change(self, instance, value):
       
print(f'ins {instance}, v: {value}')
        instance.color = (
1, 0, 0, 1) if value == 'normal' else (1, 1, 1, 1)


BindApp().run()

 

 

 

From: gorskijoza
Sent: Friday, August 7, 2020 3:49 AM
To: Kivy users support
Subject: [kivy-users] Button's attribute

 

Hi everyone,

 

one short question - how can I use button attrubute in python?

 

label1= ToggleButton(text='Basic\nlayer', group='b', background_normal= 'atlas://tabs/normal', background_down= 'atlas://tabs/down', color= (1, 1, 1, .6) if self.state == 'normal' else (1, 1, 1, 1) )

 

I want to use button state, to define text color.

 

Thanks

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.

To unsubscribe from this group and stop receiving emails from it, send an email to kivy-...@googlegroups.com.

Elliot Garbus

unread,
Aug 10, 2020, 9:41:05 AM8/10/20
to kivy-...@googlegroups.com

Yes,  you can create a new class of button derived from button, and instance that new button where you want this behavior.

 

 

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.togglebutton import ToggleButton

kv =
"""
<ColorToggle>:
    color: 1, 0, 0, 1
    on_release: self.color = {'normal':(1, 0, 0, 1), 'down':(1, 1, 1, 1)}[self.state]
    
GridLayout:
    id: grid
    cols: 3
   
"""


class ColorToggle(ToggleButton):
   
pass


class
ColorButtonApp(App):
   
def build(self):
       
return Builder.load_string(kv)

   
def on_start(self):
       
for i in range(30):
            ct = ColorToggle(
text=f'Button {i}')
           
self.root.add_widget(ct)


ColorButtonApp().run()

To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/e1d66cb3-3216-4cd1-9494-72d69e4701b6o%40googlegroups.com.

 

Tomaž Vodlan

unread,
Aug 10, 2020, 1:24:16 PM8/10/20
to kivy-...@googlegroups.com
Thanks Elliot. Just one more question. Is it possible to change the state of the button anywhere in the same class.  I want to call tab state (and change it) inside xy method. Something like that we can do with attributes defined directly in kivy - > self.ids...

V pon., 10. avg. 2020 15:40 je oseba Elliot Garbus <elli...@cox.net> napisala:

Elliot Garbus

unread,
Aug 10, 2020, 2:38:24 PM8/10/20
to kivy-...@googlegroups.com

Yes you can change the state of the button, the same way you would change any other attribute.  You can do this in kv or in any class.

Tomaž Vodlan

unread,
Aug 10, 2020, 3:06:45 PM8/10/20
to kivy-...@googlegroups.com
So if I define button in python class under method x

def x(self):
 tb = ToggleButton(text='Test Button', id = 'tog', ,color=(1, 0, 0, 1))
and now I want to change the button state in method y
def y(self):
self.ids.tog.state = 'normal' 
Is this correct? 


V pon., 10. avg. 2020 20:38 je oseba Elliot Garbus <elli...@cox.net> napisala:

Elliot Garbus

unread,
Aug 10, 2020, 4:02:05 PM8/10/20
to kivy-...@googlegroups.com

This is not correct.

 

The id and ids dict,  are created in kv.  You declare an id in kv, when the kv is processed it creates it puts the id in the ids dictionary.  The id is the key, the object with the id is the value.

 

In your example, make tb an instance variable, and change the attributed directly.

 

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.togglebutton import ToggleButton
from kivy.uix.boxlayout import BoxLayout

kv =
"""

<ColorToggle>:
    color: 1, 0, 0, 1
    on_state: self.color = {'normal':(1, 0, 0, 1), 'down':(1, 1, 1, 1)}[self.state] 
    
RootLayout:  # BoxLayout
    orientation: 'vertical'

    GridLayout:
        id: grid
        cols: 3
    BoxLayout:
        size_hint_y: None
        height: 48
        Button:
            text: 'Toggle down'
            on_release: root.toggle('down')
        Button:
            text: 'Toggle Normal'
            on_release: root.toggle('normal')
   
"""


class RootLayout(BoxLayout):
   
def __init__(self, **kwargs):
       
super().__init__(**kwargs)
       
self.toggles = []

   
def on_kv_post(self, base_widget):
       
for i in range(30):
           
self.toggles.append(ColorToggle(text=f'Button {i}'))
           
self.ids.grid.add_widget(self.toggles[i])

   
def toggle(self, state):
       
for t in self.toggles:
            t.state = state


class ColorToggle(ToggleButton):
   
pass


class
ColorButtonApp(App):
   
def build(self):
       
return
Builder.load_string(kv)


ColorButtonApp().run()

Tomaž Vodlan

unread,
Aug 11, 2020, 12:50:03 AM8/11/20
to kivy-...@googlegroups.com
Thanks Elliot for all your responses / help.

V V pon., 10. avg. 2020 ob 22:01 je oseba Elliot Garbus <elli...@cox.net> napisala:

Sent: Monday, August 10, 2020 12:06 PM


To: kivy-...@googlegroups.com
Subject: Re: [kivy-users] Button's attribute

 

So if I define button in python class under method x

 

def x(self):

 tb = ToggleButton(text='Test Button', id = 'tog', ,color=(1, 0, 0, 1))
and now I want to change the button state in method y
def y(self):
self.ids.tog.state = 'normal' 
Is this correct? 
 

 

V pon., 10. avg. 2020 20:38 je oseba Elliot Garbus <elli...@cox.net> napisala:

Yes you can change the state of the button, the same way you would change any other attribute.  You can do this in kv or in any class.

 

Sent: Monday, August 10, 2020 10:25 AM

Sent: Sunday, August 9, 2020 10:32 PM

Reply all
Reply to author
Forward
0 new messages