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

时间:2017-04-18 23:52:29

标签: python kivy

我只是想让代码工作,我在Python Kivy应用程序中有两个屏幕可以来回切换,而不使用.kv文件。

在这个页面上:https://kivy.org/docs/api-kivy.uix.screenmanager.html,顶部的第二个代码块是我想要完成的,除了我想要在没有'Builder.load_string(“”“'部分的情况下这样做,而是只是正常实例化按钮。

这是我尝试这样做的,除了我无法让它工作:

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

运行此代码只会创建一个不会产生错误的空白页。

有没有办法指定它来绘制某个屏幕,从我缺少的开始?我不确定我的问题在哪里。

1 个答案:

答案 0 :(得分:0)

你做错了什么:

  • 如果您想从Python代码创建Widget内容,您应该将其放在Widget __init__方法中,而不是build
  • 您正在创建布局然后丢弃它。您需要使用self.add_widget(f)在创建后实际使用它
  • 您正在绑定到switchScreen方法,因此需要接受调用方窗口小部件作为参数。或者你可以简单地使用*args而不用担心它。
  • 你不再在kv,所以没有根。请改用self

把这一切放在一起:

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