I tried to test a basic application that imports an external Kv file using Include.
--Includes Kv files saved in a folder one level below --Redefine the properties in the external Kv file as RootWidget properties --Associate the method in RootWidget with the on_press property in the external Kv file
project-root
|-- main.py
|-- test.kv
`-- gui/
`-- tab_test.kv
The code is as follows.
main.py
main.py
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
class RootWidget(BoxLayout):
#Button defined in the Kv file_Define include Widget in Python file
button_include = ObjectProperty()
def print_obj(self, obj):
print(obj)
def check_button_include(self):
print(self.button_include) #Access widget classes in external Kv files from within a Python file
class TestApp(App):
def build(self):
return RootWidget()
if __name__ == '__main__':
TestApp().run()
test.kv
#:include gui/tab_test.kv #Import an external Kv file
<RootWidget>:
# button_Define include as a property of RootWidget
button_include: tab_button_include.button_include
TabbedPanel:
do_default_tab: False
TabbedPanelItem:
text: 'Tab1'
BoxLayout:
Button:
text: 'button 1'
on_press: root.print_obj(self)
Button:
text: 'button 2'
on_press: root.check_button_include()
TabbedPanelItem:
text: 'Tab2'
ButtonInclude: #Use the widget class defined in the external Kv file
id: tab_button_include #Assign id to the defined widget class
tab_test.kv
<ButtonInclude@Button>:
button_include: button_include
id: button_include
text: 'button include'
on_press: app.root.print_obj(self) # "app.root.method"Execute the RootWidget method from within the external Kv file with
Button1 -> <kivy.uix.button.Button object at 0x000001E0A8DE18D0>
Button2 -> <kivy.factory.ButtonInclude object at 0x000001E0A8DE1A08>
Button include -> <kivy.factory.ButtonInclude object at 0x000001E0A8DE1A08>
Recommended Posts