简体   繁体   English

如何在Kivy中浏览屏幕?

[英]How to navigate through screens in Kivy?

I am trying to create an application with two screens, one is the login screen and next is the main page. 我正在尝试用两个屏幕创建一个应用程序,一个是登录屏幕,然后是主页。 I'm unable to incorporate Kivy code for my second screen. 我无法在第二个屏幕上合并Kivy代码。 How can I add code so I can switch to the second screen? 如何添加代码,以便可以切换到第二个屏幕?

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.image import Image
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.uix.label import Label


class SigninWindow(BoxLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    def validate_user(self):
        user = self.ids.username_field
        pwd = self.ids.pwd_field
        info = self.ids.info

        uname = user.text
        passw = pwd.text

        if uname == '' or passw == '':
            info.text = '[color=#FF0000]username and/ or password required[/color]'
        else:
            if uname == 'admin' and passw == 'admin':
                info.text = '[color=#00FF00]Logged In successfully![/color]'
            else:
                info.text = '[color=#FF0000]Invalid Username and/or Password[/color]'




class SigninApp(App):
    def build(self):
        return SigninWindow()

if __name__=="__main__":
    sa = SigninApp()
    sa.run()

kvfile: kv文件:

<FlatButton@ButtonBehavior+Label>:
    font_size: 16

<SigninWindow>:
    id: main_win
    orientation: "vertical"
    spacing: 10
    space_x: self.size[0]/3
    canvas.before:
        Color:
            rgba: (1,1,1, 1)
        Rectangle:
            size: self.size
            pos: self.pos
        BorderImage:
            source: 'E:\pythonpics/blu2.png'
            pos: self.pos
            size: self.size
    BoxLayout:
        size_hint_y: None
        height: 50
        canvas.before:
            Color:
                rgba: (.06, .45, .45, 1)
            Rectangle:
                size: self.size
                pos: self.pos
        Label:
            text: "Hello! Dear User Please Sign In"
            bold: True
            size_hint_x: .9

    BoxLayout:
        orientation: 'vertical'
        padding: main_win.space_x, 10
        #spacing: 20
        BoxLayout:
            orientation: "vertical"
            spacing: 10
            size_hint_y: None
            height: 100

            Label:
                id: info
                text: ''
                markup: True
                size_hint_y: None
                height: 20
            TextInput:
                id: username_field
                hint_text: "Username"
                multiline: False
                focus: True
                on_text_validate: pwd_field.focus = True
            TextInput:
                id: pwd_field
                hint_text: "Password"
                multiline: False
                password: True
                on_text_validate: root.validate_user()
        Label:
            id: sp
            size_hint_y: None
            height: 40
        Button:
            text: "Sign In"
            size_hint_y: None
            height: 40
            background_color: (.06,.45,.45, 1)
            background_normal: ''
            on_release: root.validate_user()

        Label:
            id: sp2

Welcome to Stackoverflow! 欢迎来到Stackoverflow!

You need a ScreenManager and Screen s. 您需要一个ScreenManagerScreen You can switch to a specific screen via ScreenManager.current = "Name of Other Screen" . 您可以通过ScreenManager.current = "Name of Other Screen"切换到特定屏幕。

You might want to change your widgets, so you can omit something like App.get_running_app().root.current . 您可能想要更改窗口小部件,因此可以省略诸如App.get_running_app().root.current

Your app python code:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.image import Image
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.uix.label import Label


Builder.load_string("""

<Rootwidget>:
    Screen:
        name: "SignIn"
        SigninWindow:
    Screen:
        name: "SignedIn"
        Label:
            text: "You have signed in."

<FlatButton@ButtonBehavior+Label>:
    font_size: 16

<SigninWindow>:
    id: main_win
    orientation: "vertical"
    spacing: 10
    space_x: self.size[0]/3
    canvas.before:
        Color:
            rgba: (1,1,1, 1)
        Rectangle:
            size: self.size
            pos: self.pos
        BorderImage:
            source: 'E:\pythonpics/blu2.png'
            pos: self.pos
            size: self.size
    BoxLayout:
        size_hint_y: None
        height: 50
        canvas.before:
            Color:
                rgba: (.06, .45, .45, 1)
            Rectangle:
                size: self.size
                pos: self.pos
        Label:
            text: "Hello! Dear User Please Sign In"
            bold: True
            size_hint_x: .9

    BoxLayout:
        orientation: 'vertical'
        padding: main_win.space_x, 10
        #spacing: 20
        BoxLayout:
            orientation: "vertical"
            spacing: 10
            size_hint_y: None
            height: 100

            Label:
                id: info
                text: ''
                markup: True
                size_hint_y: None
                height: 20
            TextInput:
                id: username_field
                hint_text: "Username"
                multiline: False
                focus: True
                on_text_validate: pwd_field.focus = True
            TextInput:
                id: pwd_field
                hint_text: "Password"
                multiline: False
                password: True
                on_text_validate: root.validate_user() 
        Label:
            id: sp
            size_hint_y: None
            height: 40
        Button:
            text: "Sign In"
            size_hint_y: None
            height: 40
            background_color: (.06,.45,.45, 1)
            background_normal: ''
            on_release: root.validate_user() 

        Label:
            id: sp2
""")

class Rootwidget(ScreenManager):
    pass

class SigninWindow(BoxLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    def validate_user(self):
        user = self.ids.username_field
        pwd = self.ids.pwd_field
        info = self.ids.info

        uname = user.text
        passw = pwd.text

        if uname == '' or passw == '':
            info.text = '[color=#FF0000]username and/ or password required[/color]'
        else:
            if uname == 'admin' and passw == 'admin':
                info.text = '[color=#00FF00]Logged In successfully![/color]'
                App.get_running_app().root.current = "SignedIn"
            else:
                info.text = '[color=#FF0000]Invalid Username and/or Password[/color]'





class SigninApp(App):
    def build(self):
        return Rootwidget()

if __name__=="__main__":
    sa = SigninApp()
    sa.run()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM