简体   繁体   English

Kivy ScreenManager 的问题(ScreenManager.current 故障)

[英]Problems with Kivy ScreenManager (ScreenManager.current trouble)

I have problem with ScreenManager that it wont change screen after password check.我对 ScreenManager 有疑问,它在密码检查后不会更改屏幕。 When the password has been checked and its all right — there is need to be switched to the main_menu screen.当密码被检查并且一切正常时——需要切换到main_menu屏幕。 Now it works after next start of the programm only, because when you are typing the right password — you just see that the password is correct and thats all!现在它只在下次启动程序后才能工作,因为当你输入正确的密码时——你只会看到密码是正确的,仅此而已!

There is my main.py:有我的 main.py:

from kivy.app import App
from kivymd.theming import ThemeManager
from kivymd.label import MDLabel
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.metrics import dp, sp, pt
from kivy.properties import ObjectProperty, NumericProperty, StringProperty, BooleanProperty, ListProperty
from kivymd.toast.kivytoast import toast
from kivymd.textfields import MDTextField
from kivy.storage.jsonstore import JsonStore
from kivy.uix.textinput import TextInput
from kivy.uix.screenmanager import SlideTransition
from kivy.uix.screenmanager import NoTransition

class keyinput(MDTextField):
    pass

class Manager(ScreenManager):
    def __init__(self, **kwargs):
        super(Manager, self).__init__(**kwargs)

        psw = JsonStore('password.json')

        if psw.exists('key'):
            registeredkey = psw.get('key')['keyvalue']
            if registeredkey != '12345678':
                self.transition = NoTransition()
                self.current = 'login_screen'
            else:
                self.transition = NoTransition()
                self.current = 'main_menu'
        else:
            toast('THERE IS NO KEY IN YOUR SYSTEM!')
            self.transition = NoTransition()
            self.current = 'login_screen'

class LoginScreen(Screen):

    def keycheck(self):

        psw = JsonStore('password.json')

        if self.kinput.text == '12345678':
            toast('KEY IS CORRECT')
            mngr = Manager()
            mngr.transition = NoTransition()
            mngr.current = 'main_menu'
            psw.put('key', keyvalue=str(self.kinput.text))
        elif len(self.kinput.text) > 8:
            toast('Too much text!')
        else:
            toast('KEY IS INCORRECT!')

class MainMenu(Screen):

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

        kinput = ObjectProperty(None)

        self.menu_items = [
                {
                    "viewclass": "MDMenuItem",
                    "text": "text%d" % i,
                    "callback": self.callback,
                }
                for i in range(1, 3)
            ]

        self.menu_button = None

    def change_variable(self, value):
        print("\nvalue=", value)
        self.VARIABLE = value
        print("\tself.VARIABLE=", self.VARIABLE)

    def callback(self, *args):
        toast(args[0])

class MainApp(App):
    title = "KivyMD MDDropdownMenu Demo"
    theme_cls = ThemeManager()

    def build(self):
        return Manager()

if __name__ == "__main__":
    MainApp().run()

There is my main.kv:有我的main.kv:

#:import MDDropdownMenu kivymd.menus.MDDropdownMenu
#:import MDRaisedButton kivymd.button.MDRaisedButton
#:import MDLabel kivymd.label.MDLabel

<OptionalLabel@MDLabel>:
    halign: 'center'
    font_size: dp(12)

<MDRB@MDRaisedButton>:
    size_hint: None, None
    size: 3 * dp(48), dp(48)
    pos_hint: {'center_x': 0.5, 'center_y': 0.5}
    opposite_colors: True

<keyinput>:
    size_hint_x: 0.5
    halign: 'center'
    pos_hint: {'center_x': .5, 'center_y': .5}
    max_text_length: 8

<Manager>:
    LoginScreen:
        id: login_screen
        name: 'login_screen'
    MainMenu:
        id: main_menu
        name: 'main_menu'

<LoginScreen>:
    kinput: kinput
    AnchorLayout:
        anchor_y: 'center'
        BoxLayout:
            orientation: 'vertical'
            size_hint: 0.5, 0.5
            pos_hint: {'center_x': 0.5, 'center_y': 0.5}
            spacing: dp(10)
            MDRB:
                text: 'Login'
                on_release:
                    root.keycheck()
            keyinput:
                id: kinput
                hint_text: "Login password"

<MainMenu>:
    AnchorLayout:
        anchor_y: 'center'
        BoxLayout:
            orientation: 'vertical'
            size_hint: 0.5, 0.5
            pos_hint: {'center_x': 0.5, 'center_y': 0.5}
            spacing: dp(10)
            OptionalLabel:
                text: 'You have logged in'

Thanks for attention and help!感谢关注和帮助!

The Manager in your kv code is actually a different manager than the one in your keycheck function (you have instantiated a new object of your Manager class).您的 kv 代码中的 Manager 实际上与您的 keycheck function 中的 Manager 不同(您已经实例化了 Manager 类的新 object)。 Try using self.manager and self.manager.current = "whatever" in your keycheck function instead of mgr = Manager().尝试在您的密钥检查 function 中使用 self.manager 和 self.manager.current = "whatever" 而不是 mgr = Manager()。 The manager attribute of a Screen should refer to it's ScreenManager. Screen 的 manager 属性应该引用它的 ScreenManager。 kivy.org/doc/stable/api-kivy.uix.screenmanager.html kivy.org/doc/stable/api-kivy.uix.screenmanager.html

Erik埃里克

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

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