我正在尝试使用tkinter构建一个简单的GUI来读取CSV,然后运行一些文本挖掘功能。我很难理解tkinter的回调函数。下面的代码是使用这个ToyMVC代码创建的。
我能够弹出GUI,但在用户按下按钮后,无法使用用户从左侧窗口小部件输入的文本更新最右侧的Entry窗口小部件(变量名=finalPathEntry)。
我非常感谢任何关于如何前进以及我做错了什么的建议。
最佳,
import tkinter as tk
class Observable:
def __init__(self, initialValue=None):
self.data = initialValue
self.callbacks = {}
def addCallback(self, func):
self.callbacks[func] = 1
def delCallback(self, func):
del self.callback[func]
def _docallbacks(self):
for func in self.callbacks:
func(self.data)
def set(self, data):
self.data = data
self._docallbacks()
def get(self):
return self.data
def unset(self):
self.data = None
class Model:
def __init__(self):
self.csvPath = Observable("")
def addPath(self, value):
self.csvPath.set(self.csvPath.get())
class View(tk.Toplevel):
def __init__(self, master):
tk.Toplevel.__init__(self, master)
self.protocol('WM_DELETE_WINDOW', self.master.destroy)
tk.Label(self, text='Please input path to CSV').pack(side='left')
self.pathEntry = tk.Entry(self, width=50)
self.pathEntry.pack(side='left')
self.addButton = tk.Button(self, text='Read CSV', width=8)
self.addButton.pack(side='left')
self.path = tk.StringVar()
self.finalPathEntry = tk.Entry(self, width = 30, textvariable = self.path)
self.finalPathEntry.pack(side='left')
def setPath(self, value):
self.path.set(str(value))
class Controller:
def __init__(self, root):
self.model = Model()
self.model.csvPath.addCallback(self.pathChanged)
self.view1 = View(root)
self.view1.minsize(width = 500, height = 500)
self.view1.addButton.config(command=self.addPath)
self.pathChanged(self.model.csvPath.get())
def addPath(self):
self.model.addPath(self.view1.pathEntry.get())
def pathChanged(self, val):
self.view1.setPath(val)
if __name__ == '__main__':
root = tk.Tk()
root.withdraw()
app = Controller(root)
root.mainloop()
编辑:使用下面的答案,我已经修复了我的代码。以下是更新的工作片段:
import tkinter as tk
class Observable:
def __init__(self, initialValue = None):
self.data = initialValue
self.callbacks = {}
def addCallback(self, func):
self.callbacks[func] = 1
def delCallback(self, func):
del self.callback[func]
def _docallbacks(self):
for func in self.callbacks:
func(self.data)
def set(self, data):
self.data = data
self._docallbacks()
def get(self):
return self.data
def unset(self):
self.data = None
class Model:
def __init__(self):
self.csvPath = Observable("")
def addPath(self, value):
self.csvPath.set(self.csvPath.get())
class View(tk.Toplevel):
def __init__(self, master):
tk.Toplevel.__init__(self, master)
self.protocol('WM_DELETE_WINDOW', self.master.destroy)
tk.Label(self, text='Please input path to CSV').pack(side='left')
self.pathEntry = tk.Entry(self, width=50)
self.pathEntry.pack(side='left')
self.addButton = tk.Button(self, text='Read CSV', width=8)
self.addButton.pack(side='left')
self.path = tk.StringVar()
self.finalPathEntry = tk.Entry(self, width = 30, textvariable = self.path)
self.finalPathEntry.pack(side='left')
def setPath(self, value):
self.path.set(str(value))
class Controller:
def __init__(self, root):
self.model = Model()
self.model.csvPath.addCallback(self.pathChanged)
self.view1 = View(root)
self.view1.minsize(width = 500, height = 500)
self.view1.addButton.config(command = self.addPath)
self.pathChanged(self.model.csvPath.get())
def addPath(self):
self.model.addPath(self.view1.pathEntry.get())
def pathChanged(self, val):
self.view1.setPath(val)
if __name__ == '__main__':
root = tk.Tk()
root.withdraw()
app = Controller(root)
root.mainloop()
这段代码有一点似乎很明显:从开始的函数
def addcallback(self, func):
直到
def unset(self):
不是Observable类的一部分。因此,"self"参数并不是您所期望的。您应该缩进这些函数。