Python Tkinter对象没有属性 [英] Python tkinter- object has no attribute
问题描述
我正在尝试更改按钮的状态,但出现属性错误,这是整个Traeback的副本,以供参考:
I am trying to change the state of the button but I get an attribute error, here is a copy of the entire traeback for reference:
Traceback (most recent call last):
File "C:/Users/Test_Practice.py", line 53, in <module>
middle_buttons_class().Run_Button()
File "C:/Users/Test_Practice.py", line 14, in Run_Button
run_thread = threading.Thread(target=middle_buttons_class.Run_Robot_Files(self))
File "C:/Users/Test_Practice.py", line 23, in Run_Robot_Files
progress_bar().done_progress.config(state=tkinter.NORMAL)
AttributeError: 'progress_bar' object has no attribute 'done_progress'
我相信这可能是一个时髦的线程问题,但是我不确定,有人可以帮我吗?
I believe this may be a funky threading problem but I am not sure, can someone take a look for me?
class MiddleButtonsClass():
def run_button(self):
run_thread = threading.Thread(target=middle_buttons_class.Run_Robot_Files(self))
run_thread.daemon = True
run_thread.start()
def run_robot_files(self):
print("Tasks started")
progress_bar().progress_bar_thread()
progress_bar().done_progress.config(state=tkinter.NORMAL)
class ProgressBar():
def progress_bar_thread(self):
progress_bar_class = progress_bar()
progress_thread = threading.Thread(target=progress_bar_class.initialize_progress_bar())
progress_thread.daemon = True
progress_thread.start()
def initialize_progress_bar(self):
self.progress_window = tkinter.Toplevel()
self.progress_window.geometry("500x250")
self.progress_window.title("In Progress")
self.percentage_variable = tkinter.DoubleVar()
self.progressbar = tkinter.ttk.Progressbar(self.progress_window, style='text.Horizontal.TProgressbar',
variable=self.percentage_variable, maximum=500,
length=450, mode="determinate")
self.progressbar.pack(pady=100)
self.done_progress = tkinter.Button(self.progress_window, text="Done", state=tkinter.DISABLED,
command=None)
self.done_progress.pack()
if __name__ == "__main__":
master = tkinter.Tk()
master.title("Test Runner")
master.geometry("750x500")
middle_buttons_class().Run_Button()
master.mainloop()
推荐答案
所以您的问题是您试图从progress_bar()
的2个单独实例进行编辑.您应该做的是创建一个要使用的变量,然后从那里编辑类属性.我还将您的线程目标更改为使用self,而不是尝试创建另一个类的实例.
So your problem is you are trying to edit from 2 separate instance of progress_bar()
. What you should be doing is creating a variable to work with and then edit the class attributes from there. I also changed your thread target to use self instead of trying to create another instance of class.
这是对您的代码进行重新制作以实际生成顶层窗口的方法,我向该按钮添加了一个命令,向您显示进度条的简单更新.
Here is your code reworked to actually produce a top level window and I added a command to the button to show you a simple update of the progress bar.
这表示您的代码需要一些改进.
That said your code needs some refinement.
import tkinter
import threading
import tkinter.ttk as ttk
class middle_buttons_class():
def Run_Button(self):
run_thread = threading.Thread(target=self.Run_Robot_Files())
run_thread.daemon = True
run_thread.start()
def Run_Robot_Files(self):
print("Tasks started")
progress_bar_var = progress_bar()
progress_bar_var.progress_bar_thread()
progress_bar_var.done_progress.config(state=tkinter.NORMAL)
class progress_bar():
def progress_bar_thread(self):
progress_bar_class = self
progress_thread = threading.Thread(target=progress_bar_class.initialize_progress_bar())
progress_thread.daemon = True
progress_thread.start()
def initialize_progress_bar(self):
self.progress_window = tkinter.Toplevel()
self.progress_window.geometry("500x250")
self.progress_window.title("In Progress")
self.percentage_variable = tkinter.DoubleVar()
self.progressbar = tkinter.ttk.Progressbar(self.progress_window, style='text.Horizontal.TProgressbar',
variable=self.percentage_variable, maximum=500,length=450, mode="determinate")
self.progressbar.pack(pady=100)
self.done_progress = tkinter.Button(self.progress_window, text="Done", state=tkinter.DISABLED,command=self.update_progressbar)
self.done_progress.pack()
def update_progressbar(self):
self.percentage_variable.set(self.percentage_variable.get() + 10)
if __name__ == "__main__":
master = tkinter.Tk()
master.title("Test Runner")
master.geometry("750x500")
middle_buttons_class().Run_Button()
master.mainloop()
这篇关于Python Tkinter对象没有属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!