简体   繁体   English

使用Python Kivy制作多个屏幕时遇到困难

[英]Having difficulties making multiple screens using Python Kivy

I am just trying to get code working where I have two screens in a Python Kivy app that can switch back and forth, without using the .kv file stuff. 我只是想让代码工作,我在Python Kivy应用程序中有两个屏幕可以来回切换, 而不使用.kv文件。

On this page: https://kivy.org/docs/api-kivy.uix.screenmanager.html , the second block of code from the top is what I am trying to accomplish, except I want to do it without the 'Builder.load_string("""' section, and instead just instantiate buttons normally. 在这个页面上: https//kivy.org/docs/api-kivy.uix.screenmanager.html ,顶部的第二个代码块是我想要完成的,除了我想在没有'Builder的情况下完成它.load_string(“”“'section,而不是正常实例化按钮。

Here is my attempt at doing so, except I can't get it to work: 这是我尝试这样做的,除了我不能让它工作:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.label import Label
from kivy.uix.button import Button

class MenuScreen(Screen):
    def build(self):

        def switchScreen():
            root.manager.current = 'settings'

        f = FloatLayout()

        button1 = Button(text = "My settings button")
        button2 = Button(text = "Back to menu", on_press = switchScreen)

        f.add_widget(button1)
        f.add_widget(button2)

class SettingsScreen(Screen):
    def build(self):

        def switchScreen():
            root.manager.current = 'menu'

        f = FloatLayout()

        button1 = Button(text = "My settings button")
        button2 = Button(text = "Back to menu", on_press = switchScreen)

        f.add_widget(button1)
        f.add_widget(button2)

sm = ScreenManager()
sm.add_widget(MenuScreen(name='menu'))
sm.add_widget(SettingsScreen(name='settings'))

class MainApp(App):
    def build(self):
        return sm

if __name__ == '__main__':
    MainApp().run()

Running this code just creates a blank page that produces no errors. 运行此代码只会创建一个不会产生错误的空白页面。

Is there a way to designate it to draw a certain screen to begin with that I am missing? 有没有办法指定它绘制一个屏幕开始我错过了? I'm not really sure where my issue is. 我不确定我的问题在哪里。

What you did wrong: 你做错了什么:

  • If you want to create Widget content from Python code you should place it inside Widget __init__ method, not build 如果你想从Python代码创建Widget内容,你应该将它放在Widget __init__方法中,而不是build
  • You're creating a layout and then discarding it. 您正在创建布局然后丢弃它。 You need to use self.add_widget(f) to actually use it after its creation 你需要使用self.add_widget(f)在创建后实际使用它
  • You're binding to your switchScreen method, so it needs to accept caller widget as an argument. 您绑定到switchScreen方法,因此需要接受调用方窗口小部件作为参数。 Or you can simply use *args and not worry about it. 或者你可以简单地使用*args而不用担心它。
  • You're not in kv anymore, so there's no root. 你不再在kv ,所以没有根。 Use self instead. self代替。

Putting this all together: 把这一切放在一起:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.label import Label
from kivy.uix.button import Button

class MenuScreen(Screen):
    def __init__(self, **kwargs):
        super(MenuScreen, self).__init__(**kwargs)

        def switchScreen(*args):
            self.manager.current = 'settings'

        f = FloatLayout()

        button1 = Button(text = "My settings button")
        button2 = Button(text = "Back to menu", on_press = switchScreen)

        f.add_widget(button1)
        f.add_widget(button2)

        self.add_widget(f)

class SettingsScreen(Screen):
    def __init__(self, **kwargs):
        super(SettingsScreen, self).__init__(**kwargs)

        def switchScreen(*args):
            self.manager.current = 'menu'

        f = FloatLayout()

        button1 = Button(text = "My settings button")
        button2 = Button(text = "Back to menu", on_press = switchScreen)

        f.add_widget(button1)
        f.add_widget(button2)
        self.add_widget(f)

sm = ScreenManager()
sm.add_widget(MenuScreen(name='menu'))
sm.add_widget(SettingsScreen(name='settings'))

class MainApp(App):
    def build(self):
        return sm

if __name__ == '__main__':
    MainApp().run()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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