错误:在'u update_shadow self.\u shadow=App.get_running_应用程序().主题_四舍五入

2024-04-27 04:12:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在Kivy和KivyMD中为登录功能构建一个GUI。我的GUI可以很好地使用Kivy代码,但是当我将一个按钮更改为MDRaisedButton时,我得到了以下两个错误(完整的错误代码如下所示):

  1. AttributeError:“NoneType”对象没有“theme”属性
  2. 第118行,在更新阴影中 自我。阴影=App.get_running_应用程序().主题_cls.quad_阴影在

我很想知道是否有人能找出问题所在,并向我解释。谢谢您!在

我花了8个小时把整个应用程序从一个空白页面重新构建成一个新页面,在编辑之间运行代码,看看我是否能找出哪里出了问题。不幸的是,我想不出解决这个问题的办法。在

在框架设计.py在

class LoginWindow(Screen):
    email = ObjectProperty(None)
    password = ObjectProperty(None)

    def loginBtn(self):
        if db.validate(self.email.text, self.password.text):
            MainWindow.current = self.email.text
            self.reset()
            sm.current = "main"
        else:
            invalidLogin()

    def createBtn(self):
        self.reset()
        sm.current = "create"

    def reset(self):
        self.email.text = ""
        self.password.text = ""

class WindowManager(ScreenManager):
    pass

sm = WindowManager()

screens = [LoginWindow(name="login"), CreateAccountWindow(name="create"), MainWindow(name="main")]
for screen in screens:
    sm.add_widget(screen)

sm.current = "login"

class FrameWork(App):
    theme_cls = ThemeManager()
    theme_cls.primary_palette = "DeepPurple"
    title = "Frame Work"

    def build(self):
        return sm

设计千伏:

^{pr2}$ ^{3}$

Tags: textnameselfemaildefguipasswordcurrent
1条回答
网友
1楼 · 发布于 2024-04-27 04:12:38

问题的根本原因

问题是因为在实例化WindowManager对象时,sm = WindowManager()没有定义{}。在

解决方案

解决方案是在实例化theme_cls = ThemeManager()之后实例化WindowManager()。在

py文件

  • return sm替换为returnWindowManager()`
  • 删除下面几行代码并在kv文件中实现add_widget()(又名instantiate children

    sm = WindowManager()
    
    screens = [LoginWindow(name="login"), CreateAccountWindow(name="create"), MainWindow(name="main")]
    for screen in screens:
        sm.add_widget(screen)
    
    sm.current = "login"
    

Snippets-py文件

^{pr2}$

kv文件

  • 实例化屏幕(CreateAccountWindow,和MainWindow)作为类规则的子级,<WindowManager>:

代码片段-kv文件

<WindowManager>:
    LoginWindow:
        name: "login"
    CreateAccountWindow:
        name: "create"
    MainWindow:
        name: "main"

示例

下面的示例是一个使用Kivy(屏幕管理器、屏幕、标签、弹出窗口、FloatLayout、GridLayout)和KivyMD(例如emanager、MDLabel、MDTextField、MDRaisedButton、MDRectangleFlatButton)的应用程序模型。在

在主.py在

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.label import Label
from kivy.uix.popup import Popup
from kivy.properties import ObjectProperty

from kivymd.theming import ThemeManager

from database import DataBase as db

Builder.load_string("""
#:import MDLabel kivymd.label.MDLabel
#:import MDTextField kivymd.textfields.MDTextField
#:import MDRaisedButton kivymd.button.MDRaisedButton
#:import MDRectangleFlatButton kivymd.button.MDRectangleFlatButton

<WindowManager>:
    LoginWindow:
        name: "login"
    CreateAccountWindow:
        name: "create"
    MainWindow:
        name: "main"

<LoginWindow>:        

    namee: namee
    email: email
    password: passw
    bikebrand: bikebrand

    FloatLayout:

        FloatLayout:
            size: root.width, root.height/2

            MDLabel:
                text: "Create an Account"
                size_hint: 0.8, 0.2
                pos_hint: {"x":0.1, "top":1}
                font_size: (root.width**2 + root.height**2) / 14**4

            MDLabel:
                size_hint: 0.26,0.15
                pos_hint: {"x":0, "top":0.8}
                text: "Name: "
                font_size: (root.width**2 + root.height**2) / 14**4

            MDTextField:
                helper_text: "Name"
                helper_text_mode: "on_focus"
                pos_hint: {"x":0.3, "top":0.76}
                size_hint: 0.5, 0.08
                id: namee
                multiline: False
                font_size: (root.width**2 + root.height**2) / 14**4

            MDLabel:
                size_hint: 0.26,0.15
                pos_hint: {"x":0, "top":0.7}
                text: "Email: "
                font_size: (root.width**2 + root.height**2) / 14**4

            MDTextField:
                helper_text: "e.g. john.doe@gmail.com"
                helper_text_mode: "on_focus"
                pos_hint: {"x":0.3, "top":0.66}
                size_hint: 0.5, 0.08
                id: email
                multiline: False
                font_size: (root.width**2 + root.height**2) / 14**4

            MDLabel:
                size_hint: 0.2,0.15
                pos_hint: {"x":0, "top":0.6}
                text: "Password: "
                font_size: (root.width**2 + root.height**2) / 14**4

            MDTextField:            
                helper_text: "Must contain Symbols:( e.g. @#$% ), Numbers:( e.g. 123456 ), Lowercase Characters:( e.g. abcdefgh ) and Uppercase Characters:( e.g. ABCDEFGH )"
                helper_text_mode: "on_focus"
                pos_hint: {"x":0.3, "top":0.56}
                size_hint: 0.5, 0.08
                id: passw
                multiline: False
                password: True
                font_size: (root.width**2 + root.height**2) / 14**4

            MDLabel:
                size_hint: 0.26,0.15
                pos_hint: {"x":0, "top":0.5}
                text: "Bike: "
                font_size: (root.width**2 + root.height**2) / 14**4

            Spinner:
                id: bikebrand
                on_text:
                    print("kv: bikebrand.text={}".format(bikebrand.text))
                text: "<Select>"
                values: ['Brand A', 'Brand B', 'Brand C', 'Brand D', 'Brand E', 'Brand F', 'Brand G', 'Brand H', 'Brand I']
                # background_color: 1,1,1,1
                color: 0,0,0,1
                color_down: 0,0,0,1
                size_hint: 0.5, 0.08
                pos_hint: {"x":0.3, "top":0.46}

            MDRectangleFlatButton:
                pos_hint:{"x":0.3,"y":0.25}
                size_hint: 0.5, 0.1
                font_size: (root.width**2 + root.height**2) / 15**4
                text: "Already have an Account? Log In"
                background_color: 0.68, 0.8, 0.5, 0.18
                color: 0,0,0,1
                on_release:
                    root.manager.transition.direction = "left"
                    root.loginBtn()

            MDRaisedButton:
                pos_hint:{"x":0.2,"y":0.05}
                size_hint: 0.6, 0.2
                font_size: (root.width**2 + root.height**2) / 13**4
                text: "Create Account"
                on_release:
                    root.manager.transition.direction = "up"
                    root.createBtn()

<CreateAccountWindow>:
    GridLayout:
        cols: 1

        MDRaisedButton:
            text: 'Goto MainWindow'
            on_release: root.manager.current = 'main'

        MDLabel:
            text: 'Create Account Window'
            font_size: sp(50)

<MainWindow>:
    GridLayout:
        cols: 1

        MDRaisedButton:
            text: 'Goto LoginWindow'
            on_release: root.manager.current = 'login'

        MDLabel:
            text: 'Main Window'
            font_size: sp(50)

""")


def invalidLogin():
    pop = Popup(title='Invalid Login',
                content=Label(text='Invalid username or password.'),
                size_hint=(None, None), size=(400, 400))
    pop.open()


def invalidForm():
    pop = Popup(title='Invalid Form',
                content=Label(text='Please fill in all inputs with valid information.'),
                size_hint=(None, None), size=(400, 400))

    pop.open()


class LoginWindow(Screen):
    email = ObjectProperty(None)
    password = ObjectProperty(None)

    def loginBtn(self):
        self._shadow = App.get_running_app().theme_cls.quad_shadow
        print(f"\nloginBtn: self._shadow={self._shadow}")

        if db.validate(self.email.text, self.password.text):
            MainWindow.current = self.email.text
            self.reset()
            self.manager.current = "main"
        else:
            invalidLogin()

    def createBtn(self):
        self.reset()
        self.manager.current = "create"

    def reset(self):
        self.email.text = ""
        self.password.text = ""


class CreateAccountWindow(Screen):
    pass


class MainWindow(Screen):
    pass


class WindowManager(ScreenManager):
    pass


class FrameWork(App):
    theme_cls = ThemeManager()
    theme_cls.primary_palette = "DeepPurple"
    title = "Frame Work"

    def build(self):
        return WindowManager()


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

输出

App statrtupCreate Account Window

相关问题 更多 >