简体   繁体   English

screenmanager kivy定义回调函数以切换屏幕

[英]screenmanager kivy define callback function to switch screens

I want to switch screens in python file with kivy. 我想用kivy切换python文件中的屏幕。 I want this to be via the callback function gonext . 我希望这是通过回调函数gonext How do have to define this?. 如何定义这个? When I run the code it shows mainwidgetApp but the button doesnt work. 当我运行代码时,它显示mainwidgetApp,但该按钮不起作用。 Here is what I tried: changing the sm to global (propably not a good idea). 这是我尝试的方法:将sm更改为global(可能不是一个好主意)。 changing the kivy file for mainwidget to: on_press: root.manager.current = 'settings' 将mainwidget的kivy文件更改为:on_press:root.manager.current ='settings'

here is my code: 这是我的代码:

Builder.load_string("""
<MenuScreen>:
 mainwidgetApp






<SettingsScreen>:
 BoxLayout:
    Button:
        text: 'My settings button'
    Button:
        text: 'Back to menu'
        on_press: root.manager.current = 'settings'
""")


 class mainwidget(Widget):




def __init__(self, **kwargs):

    btnnext = Button(text='go to next', pos=(200, 400))
    btnnext.bind(on_press=self.gonext)
    self.add_widget(btnnext)


# def savecard(self, btn_instance):




def gonext(self ,btn_inst):

  ScreenManager().current = "SettingsScreen"

# Create the screen manager
sm = ScreenManager()
sm.add_widget(MenuScreen(name='menu'))
sm.add_widget(SettingsScreen(name='settings'))



class mainwidgetApp(App):

 def build(self):   
    Window.clearcolor = (0,0,0.3,1)
    return mainwidget()



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


class TestApp(App):

 def build(self):
    return sm

if __name__ == '__main__':
 TestApp().run()
  1. Replace ScreenManager().current = "SettingsScreen" with sm.current = "settings" sm.current = "settings"替换ScreenManager().current = "SettingsScreen" sm.current = "settings"
  2. Rename class name from mainwidget to MainWidget ( CapWords convention) 将类名从mainwidget重命名为MainWidgetCapWords约定)

Example

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
from kivy.core.window import Window
from kivy.lang import Builder


Builder.load_string("""
#:kivy 1.11.0

<MenuScreen>:
    MainWidget:


<SettingsScreen>:
    BoxLayout:
        Button:
            text: 'My settings button'
        Button:
            text: 'Back to menu'
            on_press: root.manager.current = 'settings'
""")


class MainWidget(Widget):

    def __init__(self, **kwargs):
        super(MainWidget, self).__init__(**kwargs)
        print("\nmainwidget:")
        btnnext = Button(text='go to next', pos=(200, 400))
        btnnext.bind(on_press=self.gonext)
        self.add_widget(btnnext)

    # def savecard(self, btn_instance):

    def gonext(self ,btn_inst):
        sm.current = "settings"


class MenuScreen(Screen):
    pass


class SettingsScreen(Screen):
    pass


class ScreenManager(ScreenManager):
    pass

# Create the screen manager
sm = ScreenManager()
sm.add_widget(MenuScreen(name='menu'))
sm.add_widget(SettingsScreen(name='settings'))


class TestApp(App):

    def build(self):
        Window.clearcolor = (0,0,0.3,1)
        return sm


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

Output 产量

Img01-应用程序启动 Img02-按下按钮,“转到下一个”

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

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