不断出错:RuntimeError:主线程不在主循环中

时间:2019-06-17 06:01:25

标签: 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')
        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()

完整文档:

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

0 个答案:

没有答案