Search code examples
pythonanimationkivykivymd

How to call a method then do a transition in Kivy


I am learning how to use Kivy and creating transitions between my screens in an application. I currently have two screens with buttons that transition between the two and work fine as that's all that is required. This looks like this:

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

This button is on one of the screens and when clicked transitions to the screen register.

The screens (widgets) are defined as followed:

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

What I am trying to now do is use one of the kivy MDRoundFlatButton's to check some details when a user registers. All of the prerequisites work as expected and prints the error messages or success messages etc, but when the method called by the button is successful, it does not perform a transition to a new screen. Calling this method does not throw any errors, it just doesn't perform the task I would like it too.

Here is the button:

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

And here is the method:

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

Just to test, I tried with a regular button without calling the method above and it worked as expected:

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

Any ideas of how I could achieve this?


Solution

  • You are using '==' where you should use '='. Just change:

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

    to:

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