Tio Mob
Tio Mob

Reputation: 15

How to call functions inside the kivy code

When i click on the Button: Profile, one error appears, i need the function to call the button to be pressed, the function checks if the variable is correct, and sends the person to another window

I've tried to put the function inside the kivy code but it doesn't recognize it, I don't know what else to do I just want to check the login and change the page, can someone help me?

Images: Button Profile

Thats my code:

from kivymd.app import MDApp
from kivy.lang.builder import Builder
from kivy.uix.screenmanager import Screen, ScreenManager

txtuser = 'Root'

def verified_Login():
    if txtuser == 'Root':
        root.manager.current = 'profile'
    else:
        print("oi")

screen_helper = """
ScreenManager:
    LoginScreen:
    ProfileScreen:


<LoginScreen>:
    name: 'login'
    MDRectangleFlatButton:
        text: 'Profile'
        pos_hint: {'center_x':0.5,'center_y':0.5}
        on_press: verified_Login()
<ProfileScreen>:
    name: 'profile'
    MDLabel:
        text: 'Test'
        halign: 'center'

    MDRectangleFlatButton:
        text: 'Back'
        pos_hint: {'center_x':0.5,'center_y':0.2}
        on_press: root.manager.current = 'login'

"""

class LoginScreen(Screen):
    pass

class ProfileScreen(Screen):
    pass

sm = ScreenManager()
sm.add_widget(LoginScreen(name='menu'))
sm.add_widget(ProfileScreen(name='profile'))

class DemoApp(MDApp):

    def build(self):
        screen = Builder.load_string(screen_helper)
        return screen

DemoApp().run()

The Error when i click on the button:

  line 57, in custom_callback
     exec(__kvlang__.co_value, idmap)
   File "<string>", line 12, in <module>
 NameError: name 'verified_Login' is not defined

Upvotes: 0

Views: 127

Answers (1)

John Anderson
John Anderson

Reputation: 39092

You need to import the verified_Login() method in your screen_helper. At the beginning of that string add:

#:import verified_Login file_name.verified_Login

where file_name is the name of the file containing your code (without the .py extension).

You also need to fix your verified_Login() method:

def verified_Login():
    if txtuser == 'Root':
        MDApp.get_running_app().root.current = 'profile'
    else:
        print("oi")

By the way, the following lines:

sm = ScreenManager()
sm.add_widget(LoginScreen(name='menu'))
sm.add_widget(ProfileScreen(name='profile'))

are building an unused instance of your App and can be removed. The actual App is built in the build() method by the line:

screen = Builder.load_string(screen_helper)

Upvotes: 1

Related Questions