【问题标题】:tkinter error on python 3 (RuntimeError: Calling Tcl from different appartment)python 3 上的 tkinter 错误(RuntimeError: Calling Tcl from different appartment)
【发布时间】:2017-10-30 05:08:30
【问题描述】:

以下代码不适用于 python3.5 (RuntimeError: Calling Tcl from different appartment) 但它在 python 2.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()

【问题讨论】:

    标签: python-3.x tkinter tcl python-multithreading


    【解决方案1】:

    必须只能从一个线程访问 tkinter,特别是主线程(除非你真的非常勇敢)。所有其他线程在希望进行 GUI 更新时都需要向主线程发送消息;在线程之间发送消息有很多机制。

    线程规则是这样的,因为底层库广泛使用线程特定的数据(为了避免需要像全局解释器锁这样的东西)。你真的不能从另一个线程更新 GUI;当您尝试这样做时,系统肯定会爆炸。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-01
      • 1970-01-01
      • 2018-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多