[英]How to fix Window object has no attribute _stop_thread when looping in thread using Event?
I am writing a program that automates a certain task while the user is away. 我正在编写一个程序,该程序可以在用户不在时自动执行某些任务。 The program has a GUI with Tkinter so that the user can start and stop execution at any time. 该程序具有带Tkinter的GUI,因此用户可以随时启动和停止执行。 To control the execution of repetitive tasks I am using a loop which executes when a threading event is not set. 为了控制重复任务的执行,我使用了一个循环,该循环在未设置线程事件时执行。 The constructor of the Window class shown below initializes the threading event in a variable called stop_thread
but when I use it in a while loop inside my threaded function to determine whether to execute the task I get an AttributeError saying the the Window class does not have stop_thread. 下面显示的Window类的构造函数在名为stop_thread
的变量中初始化线程事件,但是当我在线程函数内部的while循环中使用它来确定是否执行任务时,我得到了AttributeError,说Window类没有stop_thread 。
My current version of Python is 3.7.4 and I have tested the program in both IDLE and PyCharm both give the same error. 我当前的Python版本是3.7.4,并且我已经在IDLE和PyCharm中测试了该程序,并给出了相同的错误。
I have uploaded part of my code below 我已经在下面上传了部分代码
from tkinter import *
import random
import time
from threading import Thread,Event
class Window(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
self.init_window()
self.backgroundThread=Thread(name='bgTask',
target=self.s_i())
self.stop_thread = Event()
def init_window(self):
self.master.title = "Test"
self.grid()
text = Label(self, text="Test")
text.config(font=('Arial', 20, 'bold'))
text.grid(row=0, column=0, columnspan=3, sticky="nsew")
start=Button(self,text="Start",command=lambda:self.start())
start.grid(row=1,column=0,sticky="nsew")
stop = Button(self, text="Stop", command=lambda: self.stop())
stop.grid(row=1, column=2,sticky="nsew")
for i in range(3):
self.columnconfigure(i,weight=1)
def s_i(self):
print('Test has started')
while not self.stop_thread.isSet():
time.sleep(15)
#do something
print("Hi")
time.sleep(0.1)
#do something
print("Hi")
time.sleep(0.1)
#do something
print("Hi")
time.sleep(0.1)
#do something
print("Hi")
delay = round(random.uniform(1, 10), 2)
sleep_time = delay * 60
time.sleep(sleep_time)
def start(self):
self.backgroundThread.start()
def stop(self):
self.stop_thread.set()
root = Tk()
root.columnconfigure(0,weight=1)
app = Window(root)
root.mainloop()
I expect the code to run and when I hit start in the window the task will run repetitively until I hit stop. 我希望代码能够运行,并且当我在窗口中单击“开始”时,任务将重复运行,直到单击“停止”为止。 Instead I am getting the following error: 相反,我收到以下错误:
Traceback (most recent call last):
File "/Users/Steven/Desktop/Projects/Python/SITG/SITG.py", line 63, in <module>
app = Window(root)
File "/Users/Steven/Desktop/Projects/Python/SITG/SITG.py", line 12, in __init__
self.backgroundThread = Thread(name='bgTask',target=self.s_i())
File "/Users/Steven/Desktop/Projects/Python/SITG/SITG.py", line 32, in s_i
while not self.stop_thread.isSet():
AttributeError: 'Window' object has no attribute 'stop_thread'
Your problem is in the __init__()
function: 您的问题出在__init__()
函数中:
self.backgroundThread=Thread(name='bgTask',
target=self.s_i())
You want target=self.si
, identifying self.si
as a function to be called later . 您需要target=self.si
,将self.si
标识为稍后要调用的函数。
But instead you have target=self.si()
, meaning the function is actually called now , and at that point self.stop_thread
has not been defined yet. 但是相反,您具有target=self.si()
,这意味着该函数实际上现在已被调用,并且此时self.stop_thread
尚未定义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.