我只是在学习 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)