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.
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.
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/5f314e66.1c69fb81.21f44.8fedSMTPIN_ADDED_MISSING%40gmr-mx.google.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.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/CAGf4y6yFaxLzoN9JOC6hugkPxy%2BM4XjvhWpyt9swAr9Od5unsQ%40mail.gmail.com.
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?
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/5f319415.1c69fb81.1aab3.112dSMTPIN_ADDED_MISSING%40gmr-mx.google.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()
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/CAGf4y6xweEcndsO49%3DZLDLQ_F6BhqOkburXLoYaUra8OikjdoQ%40mail.gmail.com.
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
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/5f31a7b2.1c69fb81.6387f.7ec7SMTPIN_ADDED_MISSING%40gmr-mx.google.com.