Kivy Screen Changing

时间:2014-04-22 11:18:08

标签: screen kivy

好的家伙,我有一个问题,我想点击弹出窗口上的按钮,然后我点击弹出窗口后,它必须转换到另一个屏幕....这是我的代码 我想点击弹出按钮

后得到一个全新的屏幕
from kivy.uix.popup import Popup
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
# from kivy.uix.boxlayout import BoxLayout
# from kivy.uix.stacklayout import StackLayout
from kivy.uix.screenmanager import ScreenManager, Screen


class LoginScreen(GridLayout, Screen):
    def __init__(self, sm, **kwargs):
        super(LoginScreen, self).__init__(**kwargs)
        self.sm = sm
        self.cols = 2
        self.row = 2
        self.add_widget(Label(text='User Name', font_size='20sp'))
        self.username = TextInput(multiline=False)
        self.add_widget(self.username)
        self.add_widget(Label(text='password'))
        self.password = TextInput(password=True, multiline=False)
        self.add_widget(self.password)
        self.hello = Button(text="hello", on_press=lambda a: self.save(), size=(100, 100),
                            size_hint=(0.3, 0.3))
        self.add_widget(self.hello)

    def save(self):
        print("s")
        id_name = self.username._get_text()
        id_num = self.password._get_text()
        if id_name == "Hendricko" and id_num == "stokkies123":
            content = Button(text="Press Here", size=(100, 100), size_hint=(0.3, 0.3))
            popup = Popup(title="You May Proceed ",
                        content=content,
                        size=(50, 50),
                        size_hint=(0.3, 0.3),
                        auto_dismiss=False)
            content.bind(on_press=lambda b: self.check_menu_press)
            popup.open()

    def check_menu_press(self, button, *args):
        if button.state == 'normal':
            self.sm.current = "SecondScreen"


class SecondScreen(GridLayout,Screen):
    def __init__(self, sm,  **kwargs):
        super(SecondScreen, self).__init__(**kwargs)
        self.row = 2
        self.cols = 2
        self.add_widget(Label(text="hello", font_size="20sp"))

        self.sm = sm

    def on_touch_down(self, touch):
        self.sm.current = "LoginScreen"


class MyApp(App):
    def build(self):
        sm = ScreenManager()
        sm.add_widget(LoginScreen(sm, name="SecondScreen"))
        sm.add_widget(SecondScreen(sm, name="LoginScreen"))
        return sm




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

1 个答案:

答案 0 :(得分:0)

  

content.bind(on_press = lambda b:self.check_menu_press)

这个lambda函数什么都不做。你可能意味着content.bind(on_press=lambda b: self.check_menu_press(b))。我认为,如果你想以这种方式做事,那么使用functools.partial会更简洁。