Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
602 views
in Technique[技术] by (71.8m points)

python - 在MVC模式中,问题是将tk.toplevel类的Entry值发送到Tkinter和Python中的控制器(In MVC pattern Problem Sending Entry value from tk.toplevel class to controller in Tkinter and Python)

I have implemented a MVC pattern .

(我已经实现了MVC模式。)

I have a main window which has a menu item "File", an Entry wiget and Button1.

(我有一个主窗口,其中有一个菜单项“文件”,一个条目项和Button1。)

On clicking the File and then otherwin option a new window opens up.

(单击文件,然后单击其他选项时,将打开一个新窗口。)

This second window also has an Entry wiget and a button.

(第二个窗口也有一个Entry wiget和一个按钮。)

There are two classes in the view.py namely View and genTool.

(view.py中有两个类,即View和genTool。)
When i click the Button 1 on main window i can access the controller.py methods.

(当我单击主窗口上的按钮1时,我可以访问controller.py方法。)

But on the second window I cannot access the controller.py methods.

(但是在第二个窗口中,我无法访问controller.py方法。)

I think i am doing something wrong in creating the objects of class.

(我认为我在创建类的对象时做错了。)

Please suggest solution.

(请提出解决方案。)

Below is minimum viable code.

(以下是最小可行代码。)

controller.py - Run this file as python3 controller.py

(controller.py-作为python3 controller.py运行此文件)

from model import Model
from view import View
from view import genTool
class Controller:
    def __init__(self):
        self.model= Model()
        self.view= View(self)

    def main(self):

        self.view.main()
    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
        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

(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()


    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.value_var2=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







  ask by Deep translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...