Python-Kivy:使用Python方法更改屏幕(在Kivy代码中创建嵌套屏幕管理器)

时间:2017-05-23 22:45:21

标签: python-3.x kivy kivy-language

我是Kivy的新人,但是使用Tkinter创建了一个库存程序。

我正在使用Kivy创建一个库存程序,并计划首先使用TextInput框登录用户名(仅限),并使用on_text_verify我希望它最终运行一个方法来检查数据库的用户名,但是现在我只想让它运行一个方法(在Python代码中),将屏幕更改为包含嵌套ScreenManager的方法。

我知道如何使用KV代码来更改屏幕,但由于我需要针对数据库检查用户名,我将需要它来运行一个Python方法,然后,如果用户名被接受,然后改变屏幕。以下是我到目前为止的情况:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.properties import ObjectProperty
from kivy.uix.popup import Popup
## I have imported all of these as I have been trying out different things ##

class LoginScreen(Screen):
    pass
class MainScreen(Screen):
    pass
class ScanScreen(Screen):
    pass
class InventoryScreen(Screen):
    pass
class ItemsScreen(Screen):
    pass
class LogsScreen(Screen):
    pass
class AlertsScreen(Screen):
    pass
class HelpScreen(Screen):
    pass


class MainWindow(FloatLayout):


    def __init__(self, **kwargs):
        super(MainWindow, self).__init__(**kwargs)

    def goto_Mainscr(self):
        self.current="Mainscr"


class Invtest3App(App):
    def build(self):
        return MainWindow()


if __name__ == '__main__':
    Invtest3App().run()

这是KV代码:

<MainWindow>:
    orientation: 'vertical'
    name: "mainw"
    id: mw
    ScreenManager:
        id: loginsm
        LoginScreen:
        MainScreen:

<ScanScreen>:
    name: "Scan"
    Button:
        text: "Scan In"
        pos_hint: {"x": 0, 'y': 0}
        size_hint: 1.0/5.0, 0.1
    Button:
        text: "Scan Out"
        pos_hint: {"x": 1.0/5.0, 'y': 0}
        size_hint: 1.0/5.0, 0.1
    Button:
        text: "Event"
        pos_hint: {"x": 2.0/5.0, 'y': 0}
        size_hint: 1.0/5.0, 0.1
    Button:
        text: "Recount"
        pos_hint: {"x": 3.0/5.0, 'y': 0}
        size_hint: 1.0/5.0, 0.1
    Button:
        text: "Logout"
        pos_hint: {"x": 4.0/5.0, 'y': 0}
        size_hint: 1.0/5.0, 0.1
        on_press: app.root.ids.loginsm.current = 'Loginscr'

<InventoryScreen>:
    name: "Inventory"
    Label:
        text: "Inventory Screen"

<ItemsScreen>:
    name: "Items"
    Label:
        text: "Items Screen"

<LogsScreen>:
    name: "Logs"
    Label:
        text: "Logs Screen"

<AlertsScreen>:
    name: "Alerts"
    Label:
        text: "Alerts Screen"

<HelpScreen>:
    name: "Help"
    Label:
        text: "Help Screen"

<LoginScreen>:
    name: "Loginscr"
    id: loginscr
    Label:
        name: "loginlabel"
        text: "Scan ID Barcode or Enter V# and Press Enter"
        pos_hint: {"center_x": .5, "center_y": .5}
    TextInput:
        hint_text: "Scan ID Barcode or Enter V# and Press Enter"
        id: login_input
        size_hint: 0.2, 0.1
        pos_hint: {"center_x": .5, "center_y": 0.2}
        multiline: False
        focus: True
        on_text_validate: app.root.goto_Mainscr; self.text=""
        #app.root.goto_Mainscr
        #app.root.ids.loginsm.current = "Mainscr"

<MainScreen>:
    name: "Mainscr"
    FloatLayout:
        pos_hint: {'top': 1}
        size_hint: 1, 0.1
        Button:
            name: "scanButton"
            text: "Scan"
            pos_hint: {'x': 0, 'y': 0}
            size_hint: 1.0/6.0, 1
            on_press: root.ids.sm.current = "Scan"
        Button:
            name: "inventoryButton"
            text: "Inventory"
            pos_hint: {'x': 1.0/6.0, 'y': 0}
            size_hint: 1.0/6.0, 1
            on_press: root.ids.sm.current = "Inventory"
        Button:
            name: "itemsButton"
            text: "Items"
            pos_hint: {'x': 2.0/6.0, 'y': 0}
            size_hint: 1.0/6.0, 1
            on_press: root.ids.sm.current = "Items"
        Button:
            name: "logsButton"
            text: "Logs"
            pos_hint: {'x': 3.0/6.0, 'y': 0}
            size_hint: 1.0/6.0, 1
            on_press: root.ids.sm.current = "Logs"
        Button:
            name: "alertsButton"
            text: "Alerts"
            pos_hint: {'x': 4.0/6.0, 'y': 0}
            size_hint: 1.0/6.0, 1
            on_press: root.ids.sm.current = "Alerts"
        Button:
            name: "helpButton"
            text: "Help"
            pos_hint: {'x': 5.0/6.0, 'y': 0}
            size_hint: 1.0/6.0, 1
            on_press: root.ids.sm.current = "Help"
    FloatLayout:
        ScreenManager:
            id: sm
            pos_hint: {'top': 0.9}
            size_hint: 1, 0.9
            ScanScreen:
            InventoryScreen:
            ItemsScreen:
            LogsScreen:
            AlertsScreen:
            HelpScreen:

1 个答案:

答案 0 :(得分:1)

如果你想在不使用kv的情况下管理屏幕,那么它看起来像这样:

  1. 导入所需的课程:

    from kivy.uix.screenmanager import ScreenManager, Screen
    
  2. 创建一个屏幕管理器变量:

    scene=ScreenManager()
    
  3. 创建一个屏幕:

    screen=Screen(name="login_screen")
    
  4. 创建第二个屏幕:

    second_screen=Screen(name="main_screen")
    
  5. 现在你可以改变这样的屏幕:

    scene.current="login_screen"
    scene.current="main_screen"
    

    您可以在这些屏幕上添加小部件。