wxPython学习笔记二[原创]
在笔记一中定义了一个框架型的窗口,在笔记二中我们计划搞一个普通的form,在这个form里面我们可以放自己要的控件,而且可以有事件可以驱动。这是源码:
import wx
class Form1(wx.Panel):
def __init__(self, parent, id, size=()):
wx.Panel.__init__(self, parent, id, size=(1,1))
self.quote = wx.StaticText(self, -1, "Your quote :",wx.Point(20, 30))
self.editquote = wx.TextCtrl(self, 20, "Enter your quote here", wx.Point(160, 30), wx.Size(140,-1))
# A multiline TextCtrl - This is here to show how the events work in this program, don't pay too much attention to it
self.logger = wx.TextCtrl(self,5, "",wx.Point(320,20), wx.Size(200,300),wx.TE_MULTILINE | wx.TE_READONLY)
# A button
self.button =wx.Button(self, 10, "Save", wx.Point(200, 325))
wx.EVT_BUTTON(self, 10, self.OnClick)
# the edit control - one line version.
self.lblname = wx.StaticText(self, -1, "Your name :",wx.Point(20,60))
self.editname = wx.TextCtrl(self, 20, "Enter your name here", wx.Point(160, 60), wx.Size(140,-1))
wx.EVT_TEXT(self, 20, self.EvtText)
wx.EVT_CHAR(self.editname, self.EvtChar)
# the combobox Control
self.sampleList = ['friends', 'advertising', 'web search', 'Yellow Pages']
self.lblhear = wx.StaticText(self,-1,"How did you hear about us?",wx.Point(20, 100))
self.edithear=wx.ComboBox(self, 30, "", wx.Point(160, 100), wx.Size(95, -1),
self.sampleList, wx.CB_DROPDOWN)
wx.EVT_COMBOBOX(self, 40, self.EvtComboBox)
wx.EVT_TEXT(self, 40, self.EvtText)
# Checkbox
self.insure = wx.CheckBox(self, 40, "Do you want Insured Shipment ?",wx.Point(20,180))
wx.EVT_CHECKBOX(self, 40, self.EvtCheckBox)
# Radio Boxes
self.radioList = ['blue', 'red', 'yellow', 'orange', 'green', 'purple',
'navy blue', 'black', 'gray']
rb = wx.RadioBox(self, 50, "What color would you like ?", wx.Point(20, 210), wx.DefaultSize,
self.radioList, 3, wx.RA_SPECIFY_COLS)
wx.EVT_RADIOBOX(self, 50, self.EvtRadioBox)
def EvtRadioBox(self, event):
self.logger.AppendText('EvtRadioBox: %d\n' % event.GetInt())
def EvtComboBox(self, event):
self.logger.AppendText('EvtComboBox: %s\n' % event.GetString())
def OnClick(self,event):
self.logger.AppendText(" Click on object with Id %d\n" %event.GetId())
def EvtText(self, event):
self.logger.AppendText('EvtText: %s\n' % event.GetString())
def EvtChar(self, event):
self.logger.AppendText('EvtChar: %d\n' % event.GetKeyCode())
event.Skip()
def EvtCheckBox(self, event):
self.logger.AppendText('EvtCheckBox: %d\n
注意,这里首先是声明了一个Form1类,这个Form1类是以panel做为基类的,我找了wxPython API没找着form类,我想,一般的form程序应该大多数都以panel做为基类吧。
其它一些控件的加入,我就不用说了,他们有相应的构造函数。事件的处理也是大同小异。总之我们看到,用wxPython写程序还是挺不错的,结构比较清楚,功能也不弱,而且他的类库和帮助文件做的也不错,条理清晰,在编程工作中可以进行实战项目了。