2

我有一个跨多个模块的应用程序,其中包括一个主模块、一个 UI、一个处理函数和一个保存全局变量的模块。我在 tkinter 抛出错误消息时遇到问题,但我不明白为什么,我认为路由原因可能与 tkinter 无关(可能与线程有关?)。

表现出相同问题的应用程序的简化如下:

我的主程序

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()

我的函数.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"

我对如何理解这些错误消息感到困惑。我试过用谷歌搜索消息代码,但到目前为止还没有发现任何有用的东西。

非常感谢任何帮助。

4

1 回答 1

3

错误消息提到线程。在堆栈跟踪中,您似乎正在更改变量的状态。如果这是真的,并且您试图从创建小部件的线程以外的线程更改小部件的状态,那就是问题所在。除了创建小部件的线程之外,您不能从任何线程调用小部件方法。

于 2013-06-25T20:37:40.127 回答