【问题标题】:The home_switch function keeps returning that there is no screen with name "home"home_switch 函数不断返回没有名称为“home”的屏幕
【发布时间】:2021-11-05 19:08:07
【问题描述】:
from kivy.lang import Builder
from kivymd.app import MDApp
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.floatlayout import FloatLayout
from kivy.core.window import Window
from kivy.config import Config
from kivy.utils import platform
from kivy.properties import ObjectProperty
import sqlite3
from kivymd.uix.fitimage import FitImage

Window.size = (360, 780)

class Start_Screen(Screen):
    pass

class Sign_Up(Screen):

    def signup(self):
        signup_password = self.signup_password.text
        signup_password_re = self.signup_password_re.text

        if signup_password != "" and signup_password_re != "":
            if signup_password == signup_password_re:
                print ("correct")
                sm.home_switch()

            else:
                print ("incorrect")
        else:
            print ("empty")

class Log_In(Screen):
    pass

class Home(Screen):
    pass

class WindowManager(ScreenManager):

    def home_switch(self, *args):
        self.current = "home"

sm = WindowManager()

class MainApp(MDApp):

    def build(self):
        self.theme_cls.primary_palette = "Yellow"

        return Builder.load_file('Vocate.kv')

MainApp().run()

这是.kv文件

WindowManager:

    Start_Screen:
        name: "start"

    Sign_Up:
        name: "sign up"

    Log_In:
        name: "log in"
    Home:
        name: "home"



<Sign_Up>:
    name: "sign up"
    id: signup
    signup_phone: signup_phone
    signup_password: signup_password
    signup_password_re: signup_password_re

    MDFloatLayout:
        Image:
            source: 'images/Vocate.png'
            size_hint: 0.6, 0.6
            pos_hint: {"center_x": 0.2, "center_y": 0.95}

        FloatLayout:
            cols: 1

            MDTextField:
                id: signup_phone
                hint_text: "Phone Number"
                required: True
                font_size: 20
                size_hint: 0.9, 0.09
                pos_hint: {"center_x": 0.5, "center_y": 0.725}
                multiline: False

                helper_text: "This Field is Required"
                helper_text_mode: "on_error"

            MDTextField:
                id: signup_password
                hint_text: "Password"
                required: True
                password: True
                font_size: 20
                size_hint: 0.9, 0.09
                pos_hint: {"center_x": 0.5, "center_y": 0.625}
                multiline: False

                helper_text: "This Field is Required"
                helper_text_mode: "on_error"

            MDTextField:
                id: signup_password_re
                hint_text: "Password"
                required: True
                password: True
                font_size: 20
                size_hint: 0.9, 0.09
                pos_hint: {"center_x": 0.5, "center_y": 0.525}
                multiline: False

                helper_text: "This Field is Required"
                helper_text_mode: "on_error"

            MDFillRoundFlatButton:
                theme_text_color: "Custom"
                md_bg_color: 250/255, 205/255, 62/255, 1
                font_size: 20
                text_color: 254.99/255, 255/255, 255/255, 1
                size_hint: 0.9, 0.06
                pos_hint: {"center_x": 0.5, "center_y": 0.425}
                text: "Sign Up"
                bold: True

                on_release:
                    root.signup()

            MDFlatButton:

                text: "Go Back"
                bold: True
                theme_text_color: "Custom"
                text_color: 250/255, 205/255, 62/255, 1
                font_size: 20
                size_hint: 0.9, 0.06
                pos_hint: {"center_x": 0.5, "center_y": 0.34}

                on_release:
                    app.root.current = "start"
                    root.manager.transition.direction = "right"

<Home>:
    name: 'home'

    MDFloatLayout:
        cols: 1
        MDBoxLayout:
            orientation: "vertical"

            MDBottomNavigation:

                MDBottomNavigationItem:
                    name: "home screen"
                    icon: "home"

                    MDBoxLayout:
                        orientation: "vertical"

                        MDToolbar:
                            title: "Home"
                            md_bg_color: 250/255, 205/255, 62/255, 1
                            specific_text_color: 255/255, 255/255, 255/255, 1

                        MDLabel:
                            text: "test 1"
                            halign: "center"

                MDBottomNavigationItem:
                    name: "courses"
                    icon: "book-open-variant"

                    MDFloatLayout:
                        cols: 2

                        MDBoxLayout:
                            orientation: "horizontal"

                            MDBoxLayout:
                                orientation: "vertical"

                                MDToolbar:
                                    md_bg_color: 250/255, 205/255, 62/255, 1

                                FitImage:
                                    source: "images/coding.png"

                                FitImage:
                                    source: "images/python.png"

                                FitImage:
                                    source: "images/gsuite.png"

                                FitImage:
                                    source: "images/english.png"

                                FitImage:
                                    source: "images/vocate.png"

                                FitImage:
                                    source: "images/vocate.png"

                            MDBoxLayout:
                                orientation: "vertical"

                                MDToolbar:
                                    md_bg_color: 250/255, 205/255, 62/255, 1

                                MDLabel:
                                    text: "Coding - 1"

                                MDLabel:
                                    text: "Coding - 2"

                                MDLabel:
                                    text: "Gsuite"

                                MDLabel:
                                    text: "English"

                                MDLabel:
                                    text: "Woodworking"

                                MDLabel:
                                    text: "Customer Service"

                        MDBoxLayout:
                            orientation: "vertical"

                            MDToolbar:
                                title: "Courses"
                                md_bg_color: 250/255, 205/255, 62/255, 1
                                specific_text_color: 255/255, 255/255, 255/255, 1

                            Button:
                                background_color: 0, 0, 0, 0

                            Button:
                                background_color: 0, 0, 0, 0

                            Button:
                                background_color: 0, 0, 0, 0

                            Button:
                                background_color: 0, 0, 0, 0

                            Button:
                                background_color: 0, 0, 0, 0

                            Button:
                                background_color: 0, 0, 0, 0

                MDBottomNavigationItem:
                    name: "more"
                    icon: "dots-horizontal"

                    MDBoxLayout:
                        orientation: "vertical"

                        MDToolbar:
                            title: "More"
                            md_bg_color: 250/255, 205/255, 62/255, 1
                            specific_text_color: 255/255, 255/255, 255/255, 1

                        MDLabel:
                            text: "test 3"
                            halign: "center"

我尝试将“WindowManager:”更改为“”,但它返回一个空白屏幕。我见过一些与此类似的问题,说要删除构建器并重命名文件,所以我尝试了“Main.kv”和“MainApp.kv”,都返回相同的空白屏幕。

提前致谢!

*注意:我已经删除了一些代码,以便更清晰,所以一些屏幕和其他不会干扰这里的东西不包括在内

【问题讨论】:

    标签: python-3.x kivy kivymd


    【解决方案1】:

    主要问题是sm = WindowManager() 在加载Vocate.kv 文件之前创建WindowManager。因此,sm 没有 Screens

    解决此问题的方法是完全消除sm = WindowManager() 行,并使用Screenmanager 属性访问WindowManager。像这样:

    class Sign_Up(Screen):
    
        def signup(self):
            signup_password = self.signup_password.text
            signup_password_re = self.signup_password_re.text
    
            if signup_password != "" and signup_password_re != "":
                if signup_password == signup_password_re:
                    print ("correct")
                    self.manager.home_switch()  # use manager property
    
                else:
                    print ("incorrect")
            else:
                print ("empty")
    

    【讨论】:

    • 啊,非常感谢,我为此头疼了一段时间,但现在可以了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-17
    • 1970-01-01
    • 1970-01-01
    • 2013-10-14
    相关资源
    最近更新 更多