KivyMD:独立的登录屏幕和 MDBottomNavigation

Posted

技术标签:

【中文标题】KivyMD:独立的登录屏幕和 MDBottomNavigation【英文标题】:KivyMD: An independent Login screen and MDBottomNavigation 【发布时间】:2020-11-08 20:18:07 【问题描述】:

我希望我的登录屏幕作为应用程序启动时的默认屏幕。 用户登录后,屏幕管理器应该切换到主屏幕,这是我的 MDBottomNavigation 的一部分(而登录屏幕不是!)。

基本上,我正在尝试使登录屏幕独立于 MDBottomNavigation ......但我做不到。 现在一开始我看到登录屏幕和带有 MDBottomNavigation 的主屏幕。

将来,我计划有其他屏幕,应该在特定情况下访问,但不能从 MDBottomNavigation 访问。我对 kivy 和 kivymd 都很陌生,不胜感激!

这是我的 main.py:

from kivymd.app import MDApp
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.bottomnavigation import MDBottomNavigationItem
import requests
import json
from kivy.uix.screenmanager import Screen

# the Root
class BL(MDBoxLayout):
   pass

# screens
class LoginScreen(Screen):
   pass

class HomeScreen(MDBottomNavigationItem):
   pass

class OrdersScreen(MDBottomNavigationItem):
   pass

class ClientsScreen(MDBottomNavigationItem):
   pass

class AccountScreen(MDBottomNavigationItem):
   pass


class MainApp(MDApp):

def log_in(self, username, password):
    log_in_url = "http://127.0.0.1:8000/api-token-auth/"
    log_in_data = "username": username, "password": password
    log_in_request = requests.post(log_in_url, data=log_in_data)
    response_data = json.loads(log_in_request.content.decode())
    if log_in_request.ok:
        token = response_data["token"]
        with open("token.txt", "w") as f:
            f.write(token)
        user_id = response_data["user_id"]

def change_screen(self, screen_name):
    screen_manager = self.root.ids['screen_manager']
    screen_manager.current = screen_name

def build(self):
    return BL()

MainApp().run()

这里是main.kv

#: include kv/loginscreen.kv
#: include kv/homescreen.kv
#: include kv/ordersscreen.kv
#: include kv/clientsscreen.kv
#: include kv/accountscreen.kv

<BL>:
    orientation: "vertical"
    id: bl

    LoginScreen:
        name: "login_screen"
        id: login_screen

    MDBottomNavigation:
        id: screen_manager

        HomeScreen:
            name: "home_screen"
            id: home_screen

        OrdersScreen:
            name: "orders_screen"
            id: orders_screen

        ClientsScreen:
            name: "clients_screen"
            id: clients_screen

        AccountScreen:
            name: "account_screen"
            id: account_screen

loginscreen.kv:

<LoginScreen>:
    text: "Login"
    icon: "login"

    MDBoxLayout:
        orientation: "vertical"

        MDTextField:
            id: username
            hint_text: "Username"
            required: True
            helper_text_mode: "on_error"

        MDTextField:
            id: password
            password: True
            hint_text: "Password"
            required: True
            helper_text_mode: "on_error"

        MDRaisedButton:
            text: "Sign In"
            on_release:
                app.log_in(username.text, password.text)
                app.change_screen("home_screen")

homescreen.kv:

<HomeScreen>:
    text: "Home"
    icon: "home"
    BoxLayout:
        orientation: 'vertical'
        MDLabel:
            text: "SM-COMMERCE APP Main"
            halign: "center"

【问题讨论】:

【参考方案1】:

您可以使用ScreenManager 来制作两个分离的屏幕。一个屏幕用于您的登录屏幕,另一个用于您的MDBottomNavigation

<BL>:
    orientation: "vertical"
    id: bl

    ScreenManager:
        id: auth_screenmanager
        LoginScreen:
            name: "login_screen"
            id: login_screen
        
        Screen:
            name: 'dashboard'
            MDBottomNavigation:
            id: screen_manager

                HomeScreen:
                    name: "home_screen"
                    id: home_screen

        ### add more screens in future

main.py:

class MainApp(MDApp):
    def on_start(self):
        # check if the user is already logged in then 'dashboard' will show up
        # else redirect to 'login_screen'

【讨论】:

无法启动应用程序,我收到以下错误: 19: name: 'dashboard' 20: MDBottomNavigation: >> 21: id: dashboard 22: HomeScreen: 23: name: "home_screen" ...声明后数据无效

以上是关于KivyMD:独立的登录屏幕和 MDBottomNavigation的主要内容,如果未能解决你的问题,请参考以下文章

如何在 KivyMD 的 .kv 文件中使用 kivy 中的数据表?

KivyMD - 如何使用 KivyMD BottomNavigationItems 和屏幕本身内的按钮在屏幕上导航?

KivyMD - ScreenManager 和 ids

无法在 MDGridLayout 中添加 kivymd 的 MDTextField

kivymd 中的动态屏幕

如果我在另一个屏幕中,我如何获取屏幕的 MDTextField(或数据“登录”)的文本。 (基维/基维MD/Python)