码迷,mamicode.com
首页 > 编程语言 > 详细

wxPython入门练习代码 四

时间:2016-09-02 00:15:42      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:

自定义Model文件abtractmodel.py:

 1 class AbstractModel(object):
 2 
 3     def __init__(self):
 4         self.listeners = []
 5 
 6     def addListener(self, listenerFunc):
 7         self.listeners.append(listenerFunc)
 8 
 9     def removeListener(self, listenerFunc):
10         self.listeners.remove(listenerFunc)
11 
12     def update(self):
13         for eachFunc in self.listeners:
14             eachFunc(self)

实例调用MVC文件ModelExample.py:

 1 #!usr/bin/env python
 2 
 3 import wx
 4 import abstractmodel
 5 
 6 
 7 class SimpleName(abstractmodel.AbstractModel):
 8     def __init__(self,first="",last=""):
 9         abstractmodel.AbstractModel.__init__(self)
10         self.set(first,last)
11         
12     def set(self,first,last):
13         self.first = first
14         self.last = last
15         self.update()
16         
17 class ModelExample(wx.Frame):
18     def __init__(self,parent,id):
19         wx.Frame.__init__(self,parent,id,Flintstones,size=(340,200))
20         panel = wx.Panel(self)
21         panel.SetBackgroundColour("White")
22         self.Bind(wx.EVT_CLOSE,self.OnCloseWindow)
23         self.textFields = {}
24         self.createTextFields(panel)
25         self.model = SimpleName()
26         self.model.addListener(self.OnUpdate)
27         self.createButtonBar(panel)
28         
29     def buttonData(self):
30         return(("Fredify",self.OnFred),
31                 ("Wilmafy",self.OnWilma),
32                 ("Barnify",self.OnBarney),
33                 ("Bettify",self.OnBetty))
34                 
35     def createButtonBar(self,panel,yPos=0):
36         xPos=0
37         for eachLabel,eachHandler in self.buttonData():
38             pos = (xPos,yPos)
39             button =self.buildOneButton(panel,eachLabel,eachHandler,pos)
40             xPos += button.GetSize().width
41             
42     def buildOneButton(self,parent,label,handler,pos=(0,0)):
43         button = wx.Button(parent,-1,label,pos)
44         self.Bind(wx.EVT_BUTTON,handler,button)
45         return button
46         
47     def textFieldData(self):
48         return (("First Name",(10,50)),("Last Name",(10,80)))
49         
50     def createTextFields(self,panel):
51         for eachLabel,eachPos in self.textFieldData():
52             self.createCaptionedText(panel,eachLabel,eachPos)
53             
54     def createCaptionedText(self,panel,label,pos):
55         static = wx.StaticText(panel,wx.NewId(),label,pos)
56         static.SetBackgroundColour("White")
57         textPos = (pos[0]+75,pos[1])
58         self.textFields[label] = wx.TextCtrl(panel,wx.NewId(),
59                 "",size=(100,-1),pos=textPos,
60                 style=wx.TE_READONLY)
61                 
62     def OnUpdate(self,model):
63         self.textFields["First Name"].SetValue(model.first)
64         self.textFields["Last Name"].SetValue(model.last)
65     
66     def OnFred(self,event):
67         self.model.set("Fred","Flintstones")
68         
69     def OnBarney(self,event):
70         self.model.set("Barney","Rubble")
71         
72     def OnWilma(self,event):
73         self.model.set("Wilma","Flintstones")
74         
75     def OnBetty(self,event):
76         self.model.set("Betty","Rubble")
77         
78     def OnCloseWindow(self,event):
79         self.Destroy()
80         
81 if __name__ == __main__:
82     app = wx.PySimpleApp()
83     frame = ModelExample(parent=None,id=-1)
84     frame.Show()
85     app.MainLoop()
86     

技术分享

 

wxPython入门练习代码 四

标签:

原文地址:http://www.cnblogs.com/ljfy-yjw/p/5831664.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!