【问题标题】:AttributeError: 'NoneType' object has no attribute 'text' kivyAttributeError: 'NoneType' 对象没有属性 'text' kivy
【发布时间】:2018-02-13 20:11:08
【问题描述】:

我正在尝试从ABC.kvfile 中的文本框中提取值。我在url 发现了一个类似的问题。我尝试应用相同的解决方案,但错误不断弹出。我不知道问题的根源在哪里。有人可以解释一下我面临的问题吗?

ABC.py

    from kivy.app import App
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from kivy.uix.popup import Popup
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition

class LoginScreen(BoxLayout):
    username_input = ObjectProperty()
    passwd_input = ObjectProperty()

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


        boxp = BoxLayout(orientation='vertical')
        labs = Label(text='')
        self.boxp = BoxLayout(orientation='vertical')
        self.labs = Label(text='')
        self.boxp.add_widget(self.labs)

        self.box = BoxLayout()
        self.box.orientation = 'horizontal'
        btn5 = Button(text="ok", size_hint=(0.5, 0.5))
        btn5.bind(on_release=self.checkin())
        self.box.add_widget(btn5)
        self.box.add_widget(Button(text='cancel', size_hint=(0.5, 0.5), on_release=self.cancel_button_hit))
        self.boxp.add_widget(self.box)
        self.popup = Popup(title="LOGIN SUCCESSFUL",
                            content=self.boxp,
                            size=(200, 200),
                            size_hint=(0.3, 0.3),
                            auto_dismiss=True)

    def checkin(self):
        if self.username_input.text=="root" and self.passwd_input.text=="root":
            self.labs.text = self.username_input.text
            self.popup.open()

        else:
            self.popup.title='Failed Login'
            self.labs.text = 'Failed'
            self.popup.open()
    def cancel_button_hit(self, instance):
        self.popup.dismiss()

class AnotherScreen(Screen):
    pass

class ScreenManagement(ScreenManager):
    pass

class MainApp(App):
    def build(self):
        return LoginScreen()

MainApp().run()

ABC.kv

#: import FadeTransition kivy.uix.screenmanager.FadeTransition
ScreenManagement:
    transition: FadeTransition()
    LoginScreen:
    AnotherScreen:

<LoginScreen>:
    orientation: "vertical"
    username_input: username
    passwd_input: passwd
    padding: 10
    spacing: 10
    name: 'login'
    BoxLayout:
        orientation: "vertical"
        Label:
            text: "Username"

        TextInput:
            id: username

        Label:
            text: "password"

        TextInput:
            id: passwd
            password: True

        Button:
            on_release: root.checkin()
            text: 'login'
            font_size: 50

<AnotherScreen>:
    name: 'other'

    Button:
        on_release: app.root.current = 'main'
        text: 'back to the home screen'
        font_size: 50

追溯:

Traceback (most recent call last):
   File "C:/Users/Admin/PycharmProjects/guihm/ABC.py", line 59, in <module>
     MainApp().run()
   File "C:\Python34\lib\site-packages\kivy\app.py", line 802, in run
     root = self.build()
   File "C:/Users/Admin/PycharmProjects/guihm/ABC.py", line 57, in build
     return LoginScreen()
   File "C:/Users/Admin/PycharmProjects/guihm/ABC.py", line 27, in __init__
     btn5.bind(on_release=self.checkin())
   File "C:/Users/Admin/PycharmProjects/guihm/ABC.py", line 38, in checkin
     if self.username_input.text=="root" and self.passwd_input.text=="root":
 AttributeError: 'NoneType' object has no attribute 'text'

【问题讨论】:

    标签: python kivy kivy-language


    【解决方案1】:

    当用户点击popup 中的btn5 时,没有必要再次调用checkin 方法。您已经在ABC.kv 文件中的登录按钮的定义中调用了它。首先,从btn5.bind(on_release=self.checkin()) 行中删除checkin 之后的括号。我建议调用不同的方法来处理btn5on_release 操作。

    btn5.bind(on_release=self.success)
    

    定义一个名为success的方法

    def success(self,instance):
        self.popup1.dismiss()   
    

    还将 .kv 文件中的 ScreenManagement 类括在尖括号内。

    【讨论】:

      【解决方案2】:

      username_input 和 password_input 缺少 self。删除自我。在他们里面

      【讨论】:

      • 在进行必要的更改后,这就是我现在得到的... self.username_input = ObjectProperty() NameError: name 'self' is not defined
      • 编辑我的答案试试看
      猜你喜欢
      • 1970-01-01
      • 2021-10-16
      • 1970-01-01
      • 1970-01-01
      • 2019-01-10
      • 1970-01-01
      • 2020-11-28
      • 1970-01-01
      • 2021-08-27
      相关资源
      最近更新 更多