【问题标题】:Kivy Screen ChangingKivy 换屏
【发布时间】:2014-04-22 11:18:07
【问题描述】:

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

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

【问题讨论】:

    标签: screen kivy


    【解决方案1】:

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

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

    【讨论】:

    • 这将变得更容易,因为一旦我可以显示第 2 页,这个应用程序就会变大.....我可以使用 .kv 文件语言,但这会花费我更多时间弄清楚
    • 感谢您的帮助,我不得不稍微修改 check_menu_press 功能以做出响应....不只是为了让应用程序进一步开发感谢大家的帮助
    • 不使用KV语言是短视的。学起来不会花很长时间,从长远来看,它会给你更干净的代码和更少的代码。
    猜你喜欢
    • 2021-10-06
    • 1970-01-01
    • 1970-01-01
    • 2021-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多