如何调用方法然后在 Kivy 中进行转换

How to call a method then do a transition in Kivy

我正在学习如何使用 Kivy 以及如何在应用程序的屏幕之间创建转换。我目前有两个带有按钮的屏幕,这些按钮可以在两者之间转换并且可以正常工作,因为这就是所需要的。这看起来像这样:

    Button:
        text: 'Back'
        on_press:
            root.manager.transition.direction = 'up'
            root.manager.current = 'register'

此按钮位于其中一个屏幕上,单击后会转换到屏幕 register

屏幕(小部件)定义如下:

class DataApp(MDApp):
    def build(self):
        sm = ScreenManager()
        sm.add_widget(LoginScreen(name='login'))
        sm.add_widget(RegisterScreen(name='register'))
        sm.add_widget(HomeScreen(name='home'))
        return sm

我现在要做的是使用其中一个 kivy MDRoundFlatButton 在用户注册时检查一些详细信息。所有先决条件都按预期工作并打印错误消息或成功消息等,但是当按钮调用的方法成功时,它不会执行到新屏幕的转换。调用这个方法不会抛出任何错误,它只是不执行我也想要的任务。

这是按钮:

        MDRoundFlatButton:
            text: "Register"
            font_size: 12
            pos_hint: {"center_x": 0.5}
            on_press: root.register(root) 

方法如下:

class RegisterScreen(Screen):
    def check_user(self, username):
        try:
            self.db = MySQLdb.connect(host="localhost", user="root",passwd="",db="SDV_Graphs")
            self.c = self.db.cursor()
            self.c.execute("SELECT * FROM `tbl_user` WHERE `username`=%s", (username,))
            return self.c.fetchone()
        except (MySQLdb.Error, MySQLdb.Warning) as e:
            print(e)
            
    def register(self, root):
        self.db = MySQLdb.connect(host="localhost", user="root",passwd="",db="SDV_Graphs")
        self.c = self.db.cursor()
        
        username = self.ids.user.text
        email = self.ids.email.text
        confirm_password = self.ids.confirm_password.text
        password = self.ids.password.text
        
        #This method is not relevant to the question
        user = self.check_user(username)    
        
        if password != confirm_password:
            print("Passwords do not match")
            pass
        else:
            if user != None:
                print("username exists")
                pass
            else:
                hash_password = bcrypt.hashpw(password.encode('utf8'), bcrypt.gensalt())
                self.c.execute("INSERT INTO tbl_user (username, password, email) VALUES (%s,%s,%s)",
                    (username, hash_password, email,))
                self.db.commit()

                #This is where the method reaches
                #It prints the message but does not perform the transition

                print("User created successfully")
                root.manager.transition.direction == "up"
                root.manager.current == "home"
                pass

只是为了测试,我尝试使用常规按钮而不调用上面的方法,它按预期工作:

    Button:
        text: 'Home'
        on_press:
            root.manager.transition.direction = 'up'
            root.manager.current = 'home'

关于如何实现此目标的任何想法?

您在应该使用“=”的地方使用了“==”。只需更改:

            root.manager.transition.direction == "up"
            root.manager.current == "home"

至:

            root.manager.transition.direction = "up"
            root.manager.current = "home"