python—当屏幕在主.kv文件中管理时,如何从.py文件切换屏幕?

k0pti3hp  于 2021-09-08  发布在  Java
关注(0)|答案(2)|浏览(354)

我有三个代码,我不知道如何从login.py导航到organize.kv中的下一个屏幕,我有保存所有屏幕的主kv文件和管理登录屏幕的登录python和kv文件,登录kv文件平衡两个不同的选项卡,到目前为止,我有一个create选项卡,它将您发送到连接到mysql的login python文件的create类,我如何将屏幕从login python文件切换到主organize.kv文件中的主菜单?对不起,如果这不合理的话,我想弄清楚这件事,我非常抱歉。
有什么有用的吗,谢谢
具有所有屏幕的主屏幕为0.kv


# :import organize organize

# :import navigation_screen_manager navigation_screen_manager

# :import anchorlayout_with_action_bar anchorlayout_with_action_bar

# :import sharpen_skills sharpen_skills

# :import login login

<MainMenu@BoxLayout>:
    orientation: "vertical"
    padding: "40dp"
    spacing: "20dp"
    size_hint: .7, .9
    pos_hint: {"center_x": .5}
    Button:
        text: "Organize"
        on_press: app.manager.push("screen2")
    Button:
        text: "Journal"
        on_press: app.manager.push("screen3")
    Button:
        text: "Sharpen Skills"
        on_press: app.manager.push("SkillsExamplesTabs")
    Button:
        text: "Exit"
        on_press: app.stop()

<MyScreenManager>:
    Screen:
        name: "Login"
        title: "login"
        LoginTabs:
    Screen:
        name: "MainMenu"
        MainMenu:
    Screen:
        name: "screen2"
        AnchorLayoutWithActionBar:
            title: "Organize"

    Screen:
        name: "screen3"
        AnchorLayoutWithActionBar:
            title: "Journal"

    Screen:
        name: "SkillsExamplesTabs"
        AnchorLayoutWithActionBar:
            title: "Sharpen Skills"
            SkillsExamplesTabs:

我的login.kv文件

<LoginTabs@TabbedPanel>:
    do_default_tab: False
    TabbedPanelItem:
        text: "Create Account"
        Create:
    TabbedPanelItem:
        text: "Login"
        Login:

最后是我的login.py文件

from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import ScreenManager
from kivy.uix.stacklayout import StackLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
from kivy.graphics import Color, Rectangle
import mysql.connector
import navigation_screen_manager
from kivy.uix.screenmanager import ScreenManager, Screen

from navigation_screen_manager import NavigationScreenManager

Builder.load_file("login.kv")
Builder.load_file("Organize.kv")

sm = ScreenManager()

class MyScreenManager(NavigationScreenManager):
    pass

class Create(BoxLayout):
    def __init__(self,**kwargs):
        super().__init__(**kwargs)
        self.orientation = "vertical"
        self.build()

    def build(self):
        self.warning = Label(text="", pos_hint={'center_x': .5, 'center_y': .5})
        user_label = Label(text="Username", size_hint=(.5, .5),
                           pos_hint={'center_x': .5, 'center_y': .5})
        self.user_textinput = TextInput(text='Enter Username')
        pas_label = Label(text="Password", size_hint=(.5, .5),
                          pos_hint={'center_x': .5, 'center_y': .5})
        self.pas_textinput = TextInput(text="enter Password")
        enter_button = Button(text="Create")
        with user_label.canvas:
            Color(0, .25, 1, .25)
        enter_button.bind(on_press=self.Add)
        self.add_widget(self.warning)
        self.add_widget(user_label)
        self.add_widget(self.user_textinput)
        self.add_widget(pas_label)
        self.add_widget(self.pas_textinput)
        self.add_widget(enter_button)

    def Add(self, btn):
        print("Working")
        t1 = self.user_textinput.text
        t2 = self.pas_textinput.text
        print(f"T1 = {t1} and {t2}")
        conn = mysql.connector.connect(
            user='root',
            password='EndGame?!72?',
            host='127.0.0.1',
            database='Usersdb')
        mycursor = conn.cursor()
        try:
            sql = "INSERT INTO user_info (user_name, user_pass) VALUES (%s, %s)"
            val = (t1,t2)
            mycursor.execute(sql,val)
            conn.commit()
        except:
            self.warning = "Username Already Taken"

class Login(StackLayout):
    pass

最后是我的应用程序类

from kivy.app import App
from kivy.uix.label import Label
from kivy.metrics import dp
from kivy.properties import StringProperty, BooleanProperty, ObjectProperty
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.uix.popup import Popup
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.stacklayout import StackLayout
from kivy.uix.widget import Widget

from navigation_screen_manager import NavigationScreenManager

class MyScreenManager(NavigationScreenManager):
    pass

class Organizer(App):

    manager = ObjectProperty(None)

    def build(self):
        self.manager = MyScreenManager()
        return self.manager

Organizer().run()

还有我的导航屏幕管理器

from kivy.uix.screenmanager import ScreenManager

class NavigationScreenManager(ScreenManager):
    screen_stack = []

    def push(self, screen_name):
        self.screen_stack.append(self.current)
        self.transition.direction = "left"
        self.current = screen_name

    def pop(self):
        if len(self.screen_stack) > 0:
            screen_name = self.screen_stack[-1]
            del self.screen_stack[-1]
            self.transition.direction = "right"
            self.current = screen_name
yeotifhr

yeotifhr1#

您的帖子不包括您的 App 类,因此不可能给出具体的答案。事实上,你有多个 kv 一旦这些文件被删除,它们就变得无关紧要了 kv 文件已加载。通常,您只需设置 current 财产 ScreenManagernameScreen 您希望显示的。

q3qa4bjr

q3qa4bjr2#

很好的一天。必须访问屏幕管理器才能设置当前屏幕,即活动屏幕。在你的 App 类,我建议通过 on_start 功能。

class MyApp(App):
    def on_start(self):
        screen_manager = self.root

从那里,您可以使用kivy或python中的回调方法设置当前屏幕。


# in kv

on_release: setattr(app.screen_manager, 'current', 'the next screen name')

# in python

def set_screen_callback(self, instance *args):
    app.screen_manager.current = "the next screen name"

相关问题