在MVC模式中,Tkinter和Python中从tk.toplevel类向控制器发送条目值时出现问题
我已经实现了MVC模式。我有一个主窗口,其中有一个菜单项“文件”,一个输入wiget和按钮1。单击文件,然后单击otherwin选项,将打开一个新窗口。第二个窗口还有一个输入wiget和一个按钮。view.py中有两个类,即view和genTool。在MVC模式中,Tkinter和Python中从tk.toplevel类向控制器发送条目值时出现问题,python,tkinter,toplevel,Python,Tkinter,Toplevel,我已经实现了MVC模式。我有一个主窗口,其中有一个菜单项“文件”,一个输入wiget和按钮1。单击文件,然后单击otherwin选项,将打开一个新窗口。第二个窗口还有一个输入wiget和一个按钮。view.py中有两个类,即view和genTool。 当我单击主窗口上的按钮1时,我可以访问controller.py方法。但是在第二个窗口中,我无法访问controller.py方法。[通过@Reblochon Masque的实施建议解决问题] 第二个问题是从控制器类中genTool类的条目wige
当我单击主窗口上的按钮1时,我可以访问controller.py方法。但是在第二个窗口中,我无法访问controller.py方法。[通过@Reblochon Masque的实施建议解决问题] 第二个问题是从控制器类中genTool类的条目wiget接收数据。 请提出解决方案。下面是最低可行的代码 controller.py-将此文件作为python3 controller.py运行 [由@stovfl建议的编辑]
from model import Model
from view import View
class Controller:
def __init__(self):
self.model= Model()
self.view= View(self)
self.genTool=None
def main(self):
self.view.main()
#@stovfl
def on_button_click(self, caption):
print(f' Inside controller data for {caption} recieved')
if self.view.gen_tool is not None:
self.gen_tool=genTool()
self.model.value1=self.view.value_var1
result= self.model.recogniseButton(caption)
self.view.value_var1.set(result)
# old varient
#def on_button_click(self, caption):
#print(f' Inside controller data for {caption} recieved')
#self.model.value1=self.view.value_var1
#self.model.value2= self.view.value_var2 # **Donot get the value #here**
#print(self.model.value1.get())
#result= self.model.recogniseButton(caption)
#self.view.value_var1.set(result)
if __name__ == "__main__":
app = Controller()
app.main()
view.py
[编辑]
import tkinter as tk
from tkinter import ttk
class genTool(tk.Toplevel):
BUTTON_CAPTION =['Button2']
def __init__(self, parent,controller):
super().__init__(parent)
self._make_entry()
self._make_buttons()
self.controller=controller
def _make_entry(self):
self.value_var2=tk.StringVar()
self.ent =tk.Entry(self, textvariable= self.value_var2)
self.ent.grid(row=2,column=1)
def _make_buttons(self):
for caption in self.BUTTON_CAPTION:
btn=tk.Button(
self, text= caption, command =(
lambda button= caption: self.controller.on_button_click(button)
)
)
btn.grid(row=3,column=2)
class View(tk.Tk):
PAD =10
BUTTON_CAPTIONS =['Button1']
# self is place holder for object name
def __init__(self, controller):
super().__init__()
self.title("Application")
self.controller = controller
self.value_var2=tk.StringVar()
self._make_main_frame()
self._make_entry()
self._make_buttons()
def main(self):
self.mainloop()
def _make_entry(self):
self.value_var1=tk.StringVar()
self.ent =tk.Entry(self, textvariable= self.value_var1)
self.ent.pack()
def _make_buttons(self):
for caption in self.BUTTON_CAPTIONS:
btn=tk.Button(
self, text= caption, command =(
lambda button= caption: self.controller.on_button_click(button)
)
)
btn.pack()
def open_window(self):
about = genTool(self,self.controller)
about.grab_set()
def _make_main_frame(self):
menu = tk.Menu(self)
menu = tk.Menu(self)
file_menu = tk.Menu(menu, tearoff=0)
file_menu.add_command(label="otherwin",command=self.open_window)
menu.add_cascade(label="File", menu=file_menu)
self.config(menu=menu)
Model.py
class Model:
def __init__(self):
self.value1 =''
self.value2 =''
def recogniseButton(self, caption):
if caption == 'Button1':
print('Inside the Model Button1 registered. ')
self.value= ''
return self.value
if caption == 'Button2':
print("Inside the Model Button2 registered. This button is toplevel")
self.value= ''
return self.value
class genTool
未引用self.controller
@ReblochonMasque感谢您的回复。我编辑了代码并在genTool类中添加了self.controller=controller。我可以从第二个窗口接收按钮。但我仍然无法获得在第二个窗口的条目wiget中输入的数字。问题在于控制器中的self.model.value2=self.view.value\u var2行。py@ReblochonMasque我理解这个问题,但不理解解决办法。在controller.py中的self.model.value2=self.view.value_var2行中。我应该做self.model.value2=self.genTool.value\u var2。但这是错误的。我需要在controller.py的init中执行self.genTool=genTool(??)之类的操作。但我不知道应该在括号内传递什么论点。你能帮我一下吗。