【发布时间】:2017-08-21 14:03:57
【问题描述】:
我想使用线程和 tkinter (python 3.6) 实现 GUI。
当我运行 GUIExecution.py 时,出现以下错误。
"RuntimeError: Calling Tcl from different appartment" on self.root.mainloop() in base_gui_class.py
我是在一个类的基础上实现的,三个代码文件如下。
可执行文件是 GUIExecution.py。
我花了很多时间试图修复错误,但我还没有能够修复它。
请多多指教。
另外,如果我在 python2 环境中运行以下代码,它可以正常工作而不会出错。
GUIExecution.py
from base_gui_class import *
from base_class import *
speed = 1000
height = 500
width = 700
base_model = base_class()
gui = base_gui_class(base_model, speed, height, width)
base_model.visualize()
base_class.py
class base_class():
genes = []
dicLocations = {}
gui = ''
best = ''
time = 0
def __init__(self):
pass
def visualize(self):
if self.gui != '':
self.gui.start()
def registerGUI(self, gui):
self.gui = gui
base_gui_class.py
import threading
import tkinter as tk
import math
import threading
import time
class base_gui_class(threading.Thread):
root = ''
canvas = ''
speed = 0
base_model = ''
def __init__(self, base_model, speed, h, w):
threading.Thread.__init__(self)
self.base_model = base_model
base_model.registerGUI(self)
self.root = tk.Tk()
self.canvas = tk.Canvas(self.root, height=h, width=w)
self.canvas.pack()
self.root.title("Test")
self.speed = 1 / speed
def run(self):
self.root.mainloop()
def update(self):
time.sleep(self.speed)
width = int(self.canvas.cget("width"))
height = int(self.canvas.cget("height"))
self.canvas.create_rectangle(0, 0, width, height, fill='white')
def stop(self):
self.root.quit()
【问题讨论】:
标签: multithreading python-3.x tkinter tcl