How d I access kivy widget properties within panda3d code.
from panda3d_kivy.app import App
from kivy.uix.button import Button
class Example(App):
def build(self):
button = Button(text='Hello, world!')
return button
from direct.showbase.ShowBase import ShowBase
class PandaApp(ShowBase):
def __init__(self):
ShowBase.__init__(self)
self.kivy_app = kivy_app = Example(self)
kivy_app.run()
print(kivy_app.button.text) ##### how to make this work
PandaApp().run()
First you should put your variable “button” as “self.button” to access it from the PandaApp class
At the PandaApp init() time, the button class is probably not built immediately, so you need to wait a little bit (hence the task below) before accessing it
Probably (not tested), if you declare your classes through a kv file, you may not have that situation…
This should work (tested for me):
from panda3d_kivy.app import App
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from direct.showbase.ShowBase import ShowBase
class Example(App):
def build(self):
self.button = Button(text='Hello, world!')
return self.button
class PandaApp(ShowBase):
def __init__(self):
ShowBase.__init__(self)
self.kivy_app = kivy_app = Example(self)
self.kivy_app.run()
self.taskMgr.add(self.check_ui, 'check_ui')
def check_ui(self,task):
if hasattr(self.kivy_app,"button"):
print(self.kivy_app.button.text)
else:
print("Nok")
return task.cont
PandaApp().run()
I think Melan did a wonderful job at answering this just a small detail, I’d probably set “self.button” to “None” inside of the init of your Example class in this case (don’t forget to call super().init(self) !), so that you can simplify your test to “if self.kivy_app.button” instead of using “hasattr()”.
(Also FYI Melan, I saw your issues, just a bit busy at the moment )
Thank you! Yes using a kv file might be a lot easier as then you can access the widgets through their ids. I’ll have to test it out when I get the chance