0

我只是在学习 MVC 并且我正在使用 Tkinter 来这样做,但是当我试图从一个类中的一个类中配置一个按钮时,我不断收到这个错误

Traceback (most recent call last):
  File "Controller.py", line 22, in <module>
    controller = Controller(root)
  File "Controller.py", line 10, in __init__
    self.view.addWidgets.btn.config(command=self.addShow)
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1205, in configure
    return self._configure('configure', cnf, kw)
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1196, in _configure
    self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: invalid command name ".35986680.35987112.35991848"

如果您无法从错误中确定问题,我可以发布我的代码,但它们位于两个单独的文件中,尽管这些文件只有 40 行长。

细节:

Button is in Frame class
Frame class is in View class
View class is in Controller class
function being called by Button command is in Controller class
    error happens when i configure button from Controller class
    self.view.addWidgets.btn.config(command=self.addShow)

编辑

我对代码进行了条带化​​。

控制器.py

from View import *
from Model import *
from Tkinter import *

class Controller:
    def __init__(self,root):
        self.model = Model()
        self.view = View(root)

        self.view.addWidgets.btn.config(command=self.addShow)

    def addShow(self):
        print 'Working'


root=Tk()
root.withdraw()
controller = Controller(root)
root.mainloop()

视图.py

from Tkinter import *
from MultiListbox import *
from AddFrame import *  

class View(Toplevel):
    def __init__(self, master):
        Toplevel.__init__(self, master)
        self.title('Show Preserver')

        self.protocol(self.protocol('WM_DELETE_WINDOW', master.destroy))#When i delete this i dont get the error but it still dosent work

        self.addFrame=Frame(self,bg='black')
        self.addWidgets=AddFrame(self.addFrame)

        self.addFrame.grid(row=0)

        self.mainloop()

添加框架.py

#Add Frame
from Tkinter import *

class AddFrame(Frame):

    def __init__(self,master):

        self.frame = Frame(master,bg='black')
        self.frame.grid(row=0,column=0,sticky=W+E)

        self.btn = Button(self.frame,text='Add',bg='black',fg='yellow',activebackground='yellow',activeforeground='black', width=2,command=None)     
        self.btn.grid(row=0,column=6,sticky=E)
4

1 回答 1

1

该错误invalid command name ".35986680.35987112.35991848"来自为 Tkinter 提供支持的底层 Tcl 解释器。.35986680.35987112.35991848是小部件的低级标识符,它还表示用于与该小部件交互的代理命令。由于 Tcl 说“无效的命令名称”,这几乎总是意味着低级小部件已被破坏,因为当小部件被销毁时命令消失了。

查看您的代码,它似乎.35986680.35987112.35991848代表self.view.AddWidgets.btn. 您确定该小部件在您尝试配置它时仍然存在吗?

[时间过去了,现在问题有一些代码......]

你打mainloop了两次电话,这不是 Tkinter 的设计用途。您需要删除其中之一。

于 2012-07-26T11:16:38.790 回答