How To Get Id And Text Value Of A Kivy Button As String?
Solution 1:
First, you must pass the button instance explicitly to the method when it is called from the kv:
on_press: app.Pressbtn(self)
You can then use the instance reference to modify the button or see its attributes, you do not need the id
. If you want to get the id
, you can only do it using the ids
dictionary of the button parent.
An example based on your code:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
kv_file = '''
<MyButton@Button>:
color: .8,.9,0,1
font_size: 32
<KVMyHBoxLayout>:
orientation: 'vertical'
MyButton:
id:"idBtn1"
text: "Btn1"
background_color: 1,0,0,1
on_press:app.Pressbtn(self)
MyButton:
id:"idBtn2"
text: "Btn2"
background_color: 0,1,0,1
on_press:app.Pressbtn(self)
Label:
id: lobj
text: "Object"
background_color: 1,0,1,1
Label:
id: lid
text: "ID"
background_color: 0,0,1,1
Label:
id: ltext
text: "Text"
background_color: 1,0,1,1
'''classKVMyHBoxLayout(BoxLayout):
passclassExampleApp(App):
defPressbtn(self, instance):
instance.parent.ids.lobj.text = str(instance)
instance.parent.ids.ltext.text = instance.text
instance.parent.ids.lid.text= self.get_id(instance)
defget_id(self, instance):
forid, widget in instance.parent.ids.items():
if widget.__self__ == instance:
returniddefbuild(self):
Builder.load_string(kv_file)
return KVMyHBoxLayout()
if __name__ == "__main__":
app = ExampleApp()
app.run()
Output:
Edit:
If you define the widget (button) in the .py file you do not need to pass the instance to the function, it is passed automatically as argument:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.button import Button
from kivy.uix.scrollview import ScrollView
classFirstScreen(Screen):
def__init__(self,**kwargs):
super(FirstScreen, self).__init__(**kwargs)
layout=BoxLayout(orientation="vertical",size_hint_y= None)
layout.bind(minimum_height=layout.setter('height'))
for i inrange(50):
btn = Button(text="Button"+str(i),
id=str(i),
size_hint=(None, None),
on_press=self.Press_auth) #<<<<<<<<<<<<<<<<
layout.add_widget(btn)
root = ScrollView()
root.add_widget(layout)
self.add_widget(root)
defPress_auth(self,instance):
print(str(instance))
classTestScreenManager(ScreenManager):
def__init__(self, **kwargs):
super(TestScreenManager, self).__init__(**kwargs)
self.add_widget(FirstScreen())
classExampleApp(App):
defbuild(self):
return TestScreenManager()
defmain():
app = ExampleApp()
app.run()
if __name__ == '__main__':
main()
Solution 2:
You can use a callback function something along these lines. In your KV file:
ToggleButton:text:'Label'id:halftimebuttonon_state:root.callback(self)
And then in your .py file you can do this:
defcallback_halftime_state(self, instance):
if instance.state == 'down':
self.halftime == Trueelse:
self.halftime == False
This is of course showing the instance.state - but it could be any attribute of the button that Kivy exposes; instance.text, instance.id, etc.
Solution 3:
example to get button text on button click :
class MainApp(MDApp):
def build(self):
VB = BoxLayout(orientation='vertical', padding=50,spacing="10")
Atbtn = Button(text="AT (Automata Theory)", font_size="25sp")
Atbtn.bind(on_press=self.callback)
Pybtn = Button(text="Python", font_size="25sp")
Pybtn.bind(on_press=self.callback)
VB.add_widget(Atbtn)
VB.add_widget(Pybtn)
return VB
def callback(self, instance):
print(instance.text)
Post a Comment for "How To Get Id And Text Value Of A Kivy Button As String?"