【问题标题】:How do I switch between Kivy screens on different .py files如何在不同 .py 文件上的 Kivy 屏幕之间切换
【发布时间】:2021-04-29 18:31:40
【问题描述】:

我目前有 3 个 python 文件。 1 这是所有主要初始化方法发生的地方,一种用于登录页面,一种用于注册页面。但是,我无法在登录页面上获得注册按钮以将我带到注册页面。请有人帮忙。我曾尝试更改 login.py 文件中的 sm.current,但这也不起作用。

这是 main.py 文件:

# Fitness App Main Code
from kivy.app import App
from kivy.core.window import Window
from kivy.uix.screenmanager import ScreenManager
from LogInWindow import LogInWindow  # importing the class from Login.py
from SignUpWindow import SignUpWindow


class WindowManager(ScreenManager):  # creating a class that deals with screen management
    pass


sm = WindowManager()  # setting a variable to the window manager class

screens = [LogInWindow(name="Login_window"), SignUpWindow(name="Signup_window")]  # setting an array of lists
for screen in screens:  # going through all of the screens
    sm.add_widget(screen)  # adding the screen widget to each

sm.current = "Login_window"  # setting current window to login


class FitnessApp(App):  # the class that runs the app
    def build(self):
        App.title = "Fitness App"  # setting the app title
        Window.size = (1080, 720)  # setting window size
        return sm  # running the app by returning the current window


if __name__ == '__main__':  # The value of __name__ attribute is set to “__main__” when module is run as main program
    FitnessApp = FitnessApp()
    FitnessApp.run()

这里是 login.py 文件

from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import Screen
from database import get_details


def switch_to_signup():
    from main import sm
    sm.current_screen = "Signup_window"


class LogInWindow(Screen):  # creating LogInWindow class
    kv = Builder.load_file("LogInWindow.kv")  # loading the kivy file which has all the

    def __init__(self, **kwargs):  # defining an init method
        super().__init__(**kwargs)  # giving the subclass the same parameter signature as the parent

    def validate_user(self):  # creating a function to validate the user
        username_input_kivy = self.ids.username_field  # setting the input to a variable
        password_input_kivy = self.ids.password_field  # setting the input to a variable
        info = self.ids.info  # setting the input to a variable

        username_input = username_input_kivy.text  # getting the text from an input and putting it into a variable
        password_input = password_input_kivy.text  # getting the text from an input and putting it into a variable

        if username_input == '' or password_input == '':  # checking if the fields are empty
            info.text = '[color=#FF0000]Username And / Or Password Required[/color]'  # red error message
        else:
            user_details = get_details(username_input)

            username = user_details[0]  # set the username to the 1st value
            users_password = user_details[1]  # set the password to the 2nd value

            if username_input == username and password_input == users_password:  # checking if they are correct
                info.text = '[color=#00FF00]Logged In successfully!!![/color]'  # green success message
            else:
                info.text = '[color=#FF0000]Invalid Username and/or Password[/color]'  # red error message

这里是相关的 kivy 按钮:

            Button:
                text: "Don't have an account? Sign Up Here!"  ## setting the text to the sign up
                font_size:20  ## changing font size
                size_hint_y: None  ## setting size hint to none so it can be set to anything
                height: 60  ## setting height to 60
                background_color: (0, 0, 0, 1)  ## setting background colour
                background_normal: ''  ## changing the background_normal to nothing
                on_release: root.switch_to_signup()  ##will change to put it to sign up screen

signup.py 文件中几乎没有任何内容,但我会将其包含在内以防万一。

from kivy.lang import Builder
from kivy.uix.screenmanager import Screen


class SignUpWindow(Screen):  # creating LogInWindow class
    kv = Builder.load_file("SignUpWindow.kv")  # loading the kivy file which has all the

    def __init__(self, **kwargs):  # defining an init method
        super().__init__(**kwargs)  # giving the subclass the same parameter signature as the parent

任何帮助将不胜感激

【问题讨论】:

    标签: python python-3.x user-interface kivy


    【解决方案1】:

    在您的kv 文件中,您始终可以使用关键字app 访问App。请参阅documentation

    因此,在您的 kv 中,将 Button 更改为:

           Button:
                text: "Don't have an account? Sign Up Here!"  ## setting the text to the sign up
                font_size:20  ## changing font size
                size_hint_y: None  ## setting size hint to none so it can be set to anything
                height: 60  ## setting height to 60
                background_color: (0, 0, 0, 1)  ## setting background colour
                background_normal: ''  ## changing the background_normal to nothing
                on_release: app.root.current = "Signup_window"  ##will change to put it to sign up screen
    

    当然,这里假设SignUpWindowname 是“Signup_window”。

    【讨论】:

    • 非常感谢。我忘记了我可以做到这一点,而且效果很好。我对 kivy 比较陌生,但我很快就学会了! :)
    猜你喜欢
    • 2017-12-05
    • 1970-01-01
    • 1970-01-01
    • 2021-10-06
    • 2018-12-30
    • 1970-01-01
    • 2021-09-20
    • 2016-09-10
    • 1970-01-01
    相关资源
    最近更新 更多