Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x python 3上的tkinter错误(RuntimeError:从不同的装置调用Tcl)_Python 3.x_Tkinter_Tcl_Python Multithreading - Fatal编程技术网

Python 3.x python 3上的tkinter错误(RuntimeError:从不同的装置调用Tcl)

Python 3.x python 3上的tkinter错误(RuntimeError:从不同的装置调用Tcl),python-3.x,tkinter,tcl,python-multithreading,Python 3.x,Tkinter,Tcl,Python Multithreading,下面的代码在python3.5上不起作用(运行时错误:从不同的装置调用Tcl) 但它在Python2.7上运行良好 很难知道问题的原因以及如何解决它 import tkinter import threading class MyTkApp(threading.Thread): def __init__(self): self.root=tkinter.Tk() self.s = tkinter.StringVar() self.s.se

下面的代码在python3.5上不起作用(运行时错误:从不同的装置调用Tcl) 但它在Python2.7上运行良好 很难知道问题的原因以及如何解决它

import tkinter
import threading

class MyTkApp(threading.Thread):
    def __init__(self):
        self.root=tkinter.Tk()
        self.s = tkinter.StringVar()
        self.s.set('Foo')
        l = tkinter.Label(self.root,textvariable=self.s)
        l.pack()
        threading.Thread.__init__(self)

    def run(self):
        self.root.mainloop()


app = MyTkApp()
app.start()
您只能从单个线程,特别是主线程访问tkinter(除非您确实非常勇敢)。当所有其他线程希望GUI更新发生时,它们需要向主线程发送消息;有很多机制可以在线程之间发送消息

线程规则是这样的,因为底层库大量使用特定于线程的数据(以避免需要全局解释器锁之类的东西)。你真的不能从另一个线程更新GUI;当你尝试这样做时,系统肯定会崩溃