首页 > 解决方案 > 如何在 python 中使用 tkinter 实现 mvc

问题描述

from Tkinter import *

class Model_Adder(object):
    def __init__(self):
        a=int(self.entry.get())
        b=int(self.entry1.get())
        selfsum=a+b


class View_Add(object):
        def loadView(self):
                quitButton = Button(self.frame,text = 'Quit', command= 
                        self.vc.quitButtonPressed).grid(row = 0,column = 0)
                addButton = Button(self.frame,text = "Add", command = 
                        self.vc.addButtonPressed).grid(row = 0, column = 1)
                entry = Entry(self.frame,textvariable = self.entry_text).grid(row 
                    = 1, column = 0, columnspan = 3, sticky = EW)
                label = Label(self.frame,textvariable = 
                    self.vc.label_text).grid(row = 2, column = 0, columnspan 
                    = 3, sticky = EW)
    def __init__(self,vc):
            self.frame=Frame()
    self.frame
    self.entry = Entry(self.window)
    self.entry1=Entry(self.window)
        self.label1=Label(self.window,text='sum is')
    label2.configure(self.window,text=sum)
    self.label1.grid(row=3,column=0,columnspan=3,sticky=E)
            self.label2.grid(row=3,column=5,columnspan=3,sticky=E)
    self.entry.grid(row=1, column=0, columnspan=3, sticky=W+E)
    self.entry1.grid(row=2, column=0, columnspan=3, sticky=W+E) 
    self.button.grid(row=4, column=0)
            self.button1.grid(row=4, column=0)

class contr(object): def init (self,model): self.model=model self v=View_Adder() v.button = Button(window, text="add", command=a.add) v.button1=Button (window, text="quit", command=window.quit) self.label2(window,text='0')

def main():
        root=tk.Tk()
        a=Adder()
        controller=contr()
        view=View_Add(root,controller
        root.title('Test')

标签: pythonpython-3.xmodel-view-controllertkinter

解决方案


推荐阅读