我的应用程序分布在几个模块中,这些模块由主模块,UI,处理函数和用于保存全局变量的模块组成。我遇到tkinter抛出错误消息的问题,但我不明白为什么,我认为路由原因可能与tkinter无关(可能与线程有关)。
显示相同问题的应用程序的简化如下:
mymain.py
import math
import random
from Tkinter import *
from ttk import *
import tkMessageBox
import myui
import myfunction
import myglobals
root = Tk()
myglobals.ui = myui.UI(root)
root.mainloop()
myfunction.py
import thread
import random
import myglobals
import myui
def myfunc():
multiprint(str(random.random()))
thread.start_new_thread(myfunc,())
def multiprint(*args):
msg = ' '.join([str(arg) for arg in args])
print msg
if myglobals.ui:
myglobals.ui.writeToLog(msg)
myui.py
from Tkinter import *
from ttk import *
import tkMessageBox
import types
import time
import random
import myfunction
class UI:
def __init__(self, master):
self.master = master
#top frame
self.topframe = Frame(self.master)
self.topframe.pack(fill=X)
self.button = Button(self.topframe, text="Start", command=self.startgame)
self.button.pack(side=LEFT)
#event frame
self.logframe = Frame(self.master)
self.logframe.pack(fill=X)
self.logframetitle = Label(self.logframe, text="Event log:")
self.logframetitle.pack(fill=X)
self.scrollbar = Scrollbar(self.master)
self.scrollbar.pack(side=RIGHT, fill=Y)
self.log = Text(self.master, state='disabled', height=24, wrap='none', yscrollcommand=self.scrollbar.set)
self.log.pack(fill=X)
self.scrollbar.config(command=self.log.yview)
def startgame(self):
myfunction.myfunc()
def writeToLog(self,msg):
numlines = self.log.index('end').split('.')[0] #self.log.index('end - 1 line') gives the start of the last line of text
print 'The log is', numlines, 'long'
self.log['state'] = 'normal'
if self.log.index('end-1c')!='1.0':
self.log.insert('end', '\n')
time.sleep(0.1)
self.log.insert('end', msg)
self.log['state'] = 'disabled'
self.log.see('end')
def main():
root = Tk()
ui = UI(root)
root.mainloop()
if __name__ == '__main__':
main()
myglobals.py 是一个包含变量的空白文件。
我收到的错误消息包括以下内容:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1470, in __call__
return self.func(*args)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 2860, in set
self.tk.call((self._w, 'set') + args)
TclError: invalid command name "512.14"
和
Unhandled exception in thread started by <function myfunc at 0x02940A30>
Traceback (most recent call last):
File "C:\myfunction.py", line 20, in myfunc
multiprint(str(random.random()))
File "C:\myfunction.py", line 29, in multiprint
myglobals.ui.writeToLog(msg)
File "C:\myui.py", line 67, in writeToLog
self.log['state'] = 'disabled'
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1269, in __setitem__
self.configure({key: value})
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1262, in configure
return self._configure('configure', cnf, kw)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1253, in _configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: invalid command name ".43240984"
我对如何理解这些错误消息感到困惑。我已经尝试使用Google搜索消息代码,但到目前为止还没有找到任何帮助。
非常感谢任何帮助。
答案 0 :(得分:3)
错误消息提及线程。在堆栈跟踪中,您看起来正在改变变量的状态。如果这是真的,并且您试图从创建窗口小部件之外的线程更改窗口小部件的状态,那就是问题所在。除了创建窗口小部件的线程之外,您无法从任何线程调用窗口小部件方法。