免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1659 | 回复: 0
打印 上一主题 下一主题

wxPython学习之 MVC设计界面 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-02-08 20:31 |只看该作者 |倒序浏览
#! /usr/bin/env python#coding=utf-8import wx
class AbstractModel(object):
        def __init__(self):                self.listeners = []
        def addListener(self, listenerFunc):                self.listeners.append(listenerFunc)
        def removeListener(self, listenerFunc):                self.listeners.remove(listenerFunc)                #此方法很关键,可以在model改变时候更新View        def update(self):                for eachFunc in self.listeners:                        eachFunc(self)
class SimpleName(AbstractModel):    '''定义的Model类'''    def __init__(self, first="", last=""):        AbstractModel.__init__(self)        self.set(first, last)
    def set(self, first, last):        self.first = first        self.last = last        self.update()   #1 更新
class ModelExample(wx.Frame):
    def __init__(self, parent, id):        wx.Frame.__init__(self, parent, id, 'Flintstones',                size=(340, 200))        panel = wx.Panel(self)        panel.SetBackgroundColour("White")        self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)        self.textFields = {}        self.createTextFields(panel)        #-------------------------------        #2 创建模型        self.model = SimpleName()        #绑定需要通知视图的方法        self.model.addListener(self.OnUpdate)        #-------------------------------        self.createButtonBar(panel)
    def buttonData(self):        return (("Fredify", self.OnFred),                ("Wilmafy", self.OnWilma),                ("Barnify", self.OnBarney),                ("Bettify", self.OnBetty))
    def createButtonBar(self, panel, yPos = 0):        xPos = 0        for eachLabel, eachHandler in self.buttonData():            pos = (xPos, yPos)            button = self.buildOneButton(panel, eachLabel, eachHandler, pos)            xPos += button.GetSize().width
    def buildOneButton(self, parent, label, handler, pos=(0,0)):        button = wx.Button(parent, -1, label, pos)        self.Bind(wx.EVT_BUTTON, handler, button)        return button
    def textFieldData(self):        return (("First Name", (10, 50)),                ("Last Name", (10, 80)))
    def createTextFields(self, panel):        for eachLabel, eachPos in self.textFieldData():            self.createCaptionedText(panel, eachLabel, eachPos)
    def createCaptionedText(self, panel, label, pos):        static = wx.StaticText(panel, wx.NewId(), label, pos)        static.SetBackgroundColour("White")        textPos = (pos[0] + 75, pos[1])        self.textFields[label] = wx.TextCtrl(panel, wx.NewId(),                "", size=(100, -1), pos=textPos,                style=wx.TE_READONLY)
    def OnUpdate(self, model): #3 设置文本域        self.textFields["First Name"].SetValue(model.first)        self.textFields["Last Name"].SetValue(model.last)    #-------------------------------------------    #4 响应按钮敲击的处理器    def OnFred(self, event):        self.model.set("Fred", "Flintstone")
    def OnBarney(self, event):        self.model.set("Barney", "Rubble")
    def OnWilma(self, event):        self.model.set("Wilma", "Flintstone")
    def OnBetty(self, event):        self.model.set("Betty", "Rubble")    #---------------------------------------------    def OnCloseWindow(self, event):        self.Destroy()
if __name__ == '__main__':    app = wx.PySimpleApp()    frame = ModelExample(parent=None, id=-1)    frame.Show()    app.MainLoop()
               
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/55091/showart_2180331.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP