我已经实现了MVC模式。我有一个主窗口,其中有一个菜单项“文件”,一个输入wiget和按钮1。单击文件,然后单击otherwin选项,将打开一个新窗口。第二个窗口还有一个条目wiget和一个按钮。教室里有两个班视图.py即视图和genTool。
当我单击主窗口上的按钮1时,我可以访问控制器.py方法。但是在第二个窗口我不能进入控制器.py方法。[通过@Reblochon Masque的实施建议解决问题]
第二个问题是从控制器类的genTool类的入口wiget接收数据。
请提出解决方案。下面是最低可行的代码。你知道吗
你知道吗控制器.py-以python3运行此文件控制器.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()
你知道吗视图.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)
你知道吗型号.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
目前没有回答
相关问题 更多 >
编程相关推荐