Include and use external Kv files in Python Kivy

Overview

I tried to test a basic application that imports an external Kv file using Include.

environment

point

--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

File placement

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()

Kv file containing Root Widget

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

External Kv file

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

Execution result

kivy_test_include_tab1.png kivy_test_include_tab2.png

Operation result of each button

Button1          ->   <kivy.uix.button.Button object at 0x000001E0A8DE18D0>
Button2          ->   <kivy.factory.ButtonInclude object at 0x000001E0A8DE1A08>
Button include   ->   <kivy.factory.ButtonInclude object at 0x000001E0A8DE1A08>

reference

Recommended Posts

Include and use external Kv files in Python Kivy
Manipulate files and folders in Python
Export and output files in Python
Read and write JSON files in Python
How to use is and == in Python
Reading and writing CSV and JSON files in Python
Download and import files with Splunk external python
Use config.ini in Python
Use dates in Python
Use Valgrind in Python
Use profiler in Python
How to use Python Kivy ① ~ Basics of Kv Language ~
% And str.format () in Python. Which one do you use?
Save lists, dictionaries and tuples to external files python
Recursively search for files and directories in Python and output
Use let expression in Python
Use Measurement Protocol in Python
Use callback function in Python
Use parameter store in Python
Use HTTP cache in Python
Execute external command in python
Use MongoDB ODM in Python
Use list-keyed dict in Python
Use Random Forest in Python
Use regular expressions in Python
Use Spyder in Python IDE
Stack and Queue in Python
External command execution in Python
Unittest and CI in Python
[Introduction to Udemy Python 3 + Application] 36. How to use In and Not
Comparison of how to use higher-order functions in Python 2 and 3
[Python] Use this to read and write wav files [wavio]
[Tips] Problems and solutions in the development of python + kivy
How to execute external shell scripts and commands in python
Print PDF in Python on Windows: Use an external application
Transpose CSV files in Python Part 1
processing to use notMNIST data in Python (and tried to classify it)
MIDI packages in Python midi and pretty_midi
Difference between list () and [] in Python
Difference between == and is in python
View photos in Python and html
Use fabric as is in python (fabric3)
Sorting algorithm and implementation in Python
[Python / matplotlib] Understand and use FuncAnimation
How to use SQLite in Python
Progress bar in pop-up in Python Kivy
About dtypes in Python and Cython
Handling of JSON files in Python
Download Google Drive files in Python
Assignments and changes in Python objects
Use Flask to run external files
Use rospy with virtualenv in Python3
Check and move directories in Python
Ciphertext in Python: IND-CCA2 and RSA-OAEP
Sort large text files in Python
Hashing data in R and Python
How to use Mysql in python
Use Python in pyenv with NeoVim
How to use ChemSpider in Python
How to use PubChem in Python
Function synthesis and application in Python