简体   繁体   中英

Use kivy widget made in python in .kv file

I'm working on a kivy app in Python. All my code for the UI is written in a KV file. I now have a very complex Widget that I'd like to design in the .py file.

My problem now is that I can not figure out how to make it useable in my .kv file. I provided an example of my problem:

My Python file:

    #main.py

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.widget import Widget
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.button import Button

class MyApp(App):
    pass

class WindowManager(ScreenManager):
    pass

class MainWindow(Screen):
    pass

class KvWidget(Widget):
    pass

class PythonWidget(Widget):
    def build(self):
        layout = FloatLayout(size=(self.width, self.height))
        test_button = Button(text = "Test button made in PY file",
                             size_hint = (1, .5),
                             pos_hint = {"x": 0, "top": .5})
        layout.add_widget(test_button)
        return layout

if __name__ == "__main__":
    MyApp().run()

And my "my.kv" Kivy file:

#my.kv

WindowManager:
    MainWindow:

<MainWindow>:
    KvWidget:
    PythonWidget:


<KvWidget>:
    FloatLayout:
        size: root.width, root.height

        Button:
            text: "Test Button made in KV file"
            size_hint: 1, .5
            pos_hint: {"x": 0, "top": 1}

Thank you for every help, I'm a beginner and pretty sure that I only forgot to read something in the docs.

Firstly,you need to give return to your Screenmanager.It checks your screens and childrens.So if you want to use Screenmanager , return it on your App class just like i did. After that, kivy widgets don't have build function.If you creating function which has not in widget's own functions, you need to call this function's name for run it's own commands. Actually you shouldn't create your widgets like that way.You can use Screen's functions just like: on_enter .You can create this button with any click, or timer with Clock function which is in kivy. I choosed on_enter method.So i changed your design. Check this minimal code below. main.py:

#Main.py:
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.widget import Widget
from kivy.uix.button import Button
class WindowManager(ScreenManager):
    pass
class MainWindow(Screen):
    def on_enter(self, *args):
        self.add_widget(Button(text="Test button made in PY file",size_hint=(1, .5),pos_hint={"x": 0, "top": .5}))
class KvWidget(Widget):
    pass
class MyApp(App):
    def build(self):
        return WindowManager()
if __name__ == "__main__":
    MyApp().run()

my.kv:

<WindowManager>:
    MainWindow:
<MainWindow>:
    KvWidget:
<KvWidget>:
    FloatLayout:
        size: root.width, root.height

        Button:
            text: "Test Button made in KV file"
            size_hint: 1, .5
            pos_hint: {"x": 0, "top": 1}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM