Python 3.x 持续获取错误:RuntimeError:主线程不在主循环中
我试图在tkinter中运行一个帧(包括代码),但我一直收到这个错误。 我不知道问题是什么…有人能帮忙吗? 尝试将线程放在代码中的几个不同位置,但仍然不起作用Python 3.x 持续获取错误:RuntimeError:主线程不在主循环中,python-3.x,tkinter,python-multithreading,Python 3.x,Tkinter,Python Multithreading,我试图在tkinter中运行一个帧(包括代码),但我一直收到这个错误。 我不知道问题是什么…有人能帮忙吗? 尝试将线程放在代码中的几个不同位置,但仍然不起作用 class playing(Tk): global songName global length def __init__(self): super(playing, self).__init__() self.title('My Private Radio')
class playing(Tk):
global songName
global length
def __init__(self):
super(playing, self).__init__()
self.title('My Private Radio')
self.minsize(300,300)
self.resizable(False,False)
def countdown(T, ctime_label):
t = 0
while(t <= T):
min1 , sec1 = divmod(t , 60)
min1 = round(min1)
sec1 = round(sec1)
timeformat = '{:02d}:{:02d}'.format(min1,sec1)
ctime_label['text'] ="total length" + "-" + timeformat
time.sleep(1)
t = t+1
self.destroy()
player_frame()
label_2 = Label(text="now playing:",width=20,font=("bold", 10))
label_2.place(x=-40,y=30)
label_3 = Label(text=songName,width=20,font=("bold", 10))
label_3.place(x=-50,y=50)
min, secs = divmod(length, 60)
min = round(min)
secs = round(secs)
time_format = '{:02d}:{:02d}'.format(min,secs)
time_label = Label(text="total time" + "-" + time_format,width=20,font=("bold", 10))
ctime_label = Label(text="current time" + "-" + "*:*",width=20,font=("bold", 10))
ctime_label.place(x=10,y=150)
time_label.place(x=10,y=170)
T = min * 60 + secs
t1 = threading.Thread(target=countdown, args=(T,ctime_label))
t1.start()
您是否只是创建了
playing
类的实例而没有调用mainloop()
?它是许多其他类的一部分……主循环在屏幕的末尾,因为我不知道如何创建playing()
和调用mainloop()
,因此,我根据您发布的代码创建了一个示例程序,它将准确地生成您发布的错误。但是如果你取消注释那些注释行,程序就会运行。你是否只是创建了playing
类的实例而没有调用mainloop()
?它是许多其他类的一部分……主循环在屏幕的末尾,因为我不知道如何创建playing()
和调用mainloop()
,因此,我根据您发布的代码创建了一个示例程序,它将准确地生成您发布的错误。但是,如果您取消注释这些注释行,程序就会工作。
Exception in thread Thread-1:
Traceback (most recent call last):
File "C:\Users\benii\AppData\Local\Programs\Python\Python37-32\lib\threading.py", line 917, in _bootstrap_inner
self.run()
File "C:\Users\benii\AppData\Local\Programs\Python\Python37-32\lib\threading.py", line 865, in run
self._target(*self._args, **self._kwargs)
File "C:/Users/benii/Desktop/school stuff/GUIclass (1).py", line 226, in countdown
ctime_label['text'] ="total length" + "-" + timeformat
File "C:\Users\benii\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1492, in __setitem__
self.configure({key: value})
File "C:\Users\benii\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1485, in configure
return self._configure('configure', cnf, kw)
File "C:\Users\benii\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1476, in _configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
RuntimeError: main thread is not in main loop