使用线程和tkinter时的RuntimeError

时间:2018-03-30 23:57:56

标签: python multithreading python-3.x tkinter runtime-error

我正在创建一个运行多个线程的程序,每个线程更新一个变量,然后使用tkinter显示该值。

唯一的问题是,每当我尝试更新显示时,我都会得到RuntimeError

Exception in thread Thread-x:
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/threading.py", line 916, in _bootstrap_inner
    self.run()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "program.py", line 15, in body
    update()
  File "program.py", line 11, in update
    display.config({"text" : "x = {0}".format(x)})
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py", line 1479, in configure
    return self._configure('configure', cnf, kw)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py", line 1470, in _configure
    self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
RuntimeError: main thread is not in main loop


我试图修复错误的一些解决方案是:

  • 使显示对象成为函数的全局(使用global
  • 创建单独的功能以更新显示

但是,这些解决方案都没有奏效(RuntimeError仍然存在)。

以下是我的计划:

import tkinter, time, threading

window = tkinter.Tk()
x = 0
display = tkinter.Label(window)
display.pack()

def update():
    global x
    x += 1
    display.config({"text" : "x = {0}".format(x)}) #It says the error is on this line

def body():
    time.sleep(3)
    update()
    body()

def start_threads():
    for i in range(5):
        thread = threading.Thread(target=body)
        thread.start(); thread.join()

start = tkinter.Button(window, text="Start", command=start_threads)
start.pack()

我不知道如何修复RuntimeError,所以我们将不胜感激。

2 个答案:

答案 0 :(得分:1)

这实际上是由于你的睡眠功能,这会冻结你不能做的tkinter的主线程。

以下是一些可行的代码:

import tkinter

x = 0
repeat = 0

def start_counter():
    global x, repeat
    repeat+=1
    x += 1
    display.config({"text" : "x = {0}".format(x)})

    if repeat < 5:
        #3000 because 1000 in a second
        window.after(3000, start_counter)


window = tkinter.Tk()
display = tkinter.Label(window)
display.pack()
start = tkinter.Button(window, text="Start", command=start_counter)
start.pack()
window.mainloop()

注意我如何使用“window.after(3000,function)”。这告诉tkinter在3秒后做某事,因此不会冻结主线程。如果你想在显示数字1之前让它睡觉,你需要改变一些东西,在这种情况下,我很乐意为你更新我的代码:)

答案 1 :(得分:0)

经过一些实验和Piero Bird提出的想法后,我提出了这个解决方案:

import tkinter, threading


def start_counter():

    for i in range(1):
         bot = threading.Thread(target=add_one)
         bot.start(); bot.join()
    temp_window = tkinter.Tk()
    temp_window.withdraw()
    window.after(100, start_counter)

def add_one():
    global count
    count += 1

if __name__ == "__main__":
    temp = 0
    count = 0
    window = tkinter.Tk()
    window.minsize(width=500, height=500)
    display = tkinter.Label(window)
    display.pack()
    start = tkinter.Button(window, text="Start", command=start_counter)
    start.pack()

    def update():
        global temp, first
        if count != temp:
            display.config({"text":"x = {0}".format(count)})
            temp = count

        window.after(1, update)


    window.after(1, update)