【问题标题】:AttributeError: 'Third_Window' object has no attribute 'history'AttributeError:“Third_Window”对象没有属性“历史”
【发布时间】:2021-01-18 18:45:00
【问题描述】:

我是初学者,我正在尝试做一个密码保护应用程序。在这我得到一个错误,如 AttributeError: 'Third_Window' object has no attribute 'history'。

我还想要如何标记文件内的文本

在 .py 文件中

from kivy.app import App
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.properties import ObjectProperty
from kivy.core.window import Window
from kivy.lang import Builder

Window.clearcolor = 0, 0, 1, 0


class Second_Window(Screen):
    pass


class Third_Window(Screen):
    def btn2(self):
        global history
        file1 = open('users_input.txt', 'r')
        Lines = file1.readlines()
        for line in Lines:
            print("{}".format(line.strip()))
            self.history = ""
            self.history += "{}".format(line.strip())
        print(history)


class Screen_Manager(ScreenManager):
    pass


class Main_Window(Screen):
    pass


presentation = Builder.load_file("password_saver.kv")


class Password_Saver(App):
    def build(self):
        return presentation


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

在.kv文件中

Screen_Manager:
    Main_Window:
    Second_Window:
    Third_Window:

<Main_Window>:
    name: 'main'

<Second_Window>:
    name: 'second'

<Third_Window>:
    name: 'third'

    GridLayout:
        cols: 1
        Label:
            text: root.history
        Button:
            text: "Go Back"
            on_release:
                root.btn2()
                app.root.current = 'main'

请帮我解决这个问题

谢谢!!

【问题讨论】:

  • 不确定,但可能与 self.history 未在 __init__ 中定义有关。是的,您确实执行了.btn2()method,但text: root.history 指令可能会在您初始化对象时尝试访问它。

标签: python-3.x kivy


【解决方案1】:

在 kivy 文件中,您使用的是 root.history。 root 在您的 kv 文件的那部分是第三个屏幕。要访问某些 frpm .py 文件,请使用 app. 而不是 root.。

【讨论】:

  • 我也使用了 app.history 然后它也显示错误为 NameError: name 'history' is not defined
  • 在 .py 文件的开头定义历史记录,然后调用 app.history
猜你喜欢
  • 2019-12-04
  • 1970-01-01
  • 1970-01-01
  • 2012-12-01
  • 2021-04-19
  • 2021-11-22
  • 1970-01-01
  • 1970-01-01
  • 2018-08-28
相关资源
最近更新 更多