【发布时间】: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