9.更多组件

来源:互联网 发布:flash网页制作软件 编辑:程序博客网 时间:2024/05/22 00:16

你可以在wxPython示例和帮助里发现完整的控件组件,这里只是列举一常最常用到的:

wxButton一个按钮:显示一个文本,你可以点击。举例来说,这里是一个“清理”按钮(如清理一个文本):

[python] view plaincopy
  1. clearButton = wx.Button(self, wx.ID_CLEAR, "Clear")  
  2. self.Bind(wx.EVT_BUTTON, self.OnClear, clearButton)  

wxTextCtrl输入文本:它有两个主要事件。EVT_TEXT--文本变化时被调用。EVT_CHAR--一个键被按下时被调用。

[python] view plaincopy
  1. textField = wx.TextCtrl(self)  
  2. self.Bind(wx.EVT_TEXT, self.OnChange, textField)  
  3. self.Bind(wx.EVT_CHAR, self.OnKeyPress, textField)  
如果用户按下“Clear”按钮,就会调用EVT_TEXT事件,却不会调用EVT_CHAR


wxComboBox组合框:非常类似于wxTextCtrl,但所产生的wxTextCtrl除了事件,wxComboBox有EVT_COMBOBOX事件。

wxCheckBox复选框:让用户选择true / false。

wxRadioBox选项列表

我们仔细这个例子:


[python] view plaincopy
  1. ''''' 
  2. Created on 2012-6-30 
  3.  
  4.  
  5. @author: Administrator 
  6. '''  
  7. import wx  
  8. class ExamplePanel(wx.Panel):  
  9.     def __init__(self,parent):  
  10.         wx.Panel.__init__(self,parent)  
  11.         self.quote = wx.StaticText(self,label="Your quote:",pos=(20,30))  
  12.         self.logger = wx.TextCtrl(self,pos=(300,20),size=(200,300),style=wx.TE_MULTILINE|wx.TE_READONLY)  
  13.           
  14.         self.button = wx.Button(self,label="Save",pos=(200,325))  
  15.         self.Bind(wx.EVT_BUTTON, self.OnClick, self.button)  
  16.           
  17.         self.lblname = wx.StaticText(self,label="Your name:",pos=(20,60))  
  18.         self.editname = wx.TextCtrl(self,value="Enter your name",pos=(150,60),size=(140,-1))  
  19.         self.Bind(wx.EVT_TEXT, self.EvtText, self.editname)  
  20.         self.Bind(wx.EVT_CHAR, self.EvtChar, self.editname)  
  21.           
  22.         self.sampleList = ['frients','advertising','web search','Yellow pages']  
  23.         self.lblhear = wx.StaticText(self,label="How did you hear fraom us?",pos=(20,90))  
  24.         self.edithear = wx.ComboBox(self,pos=(150,110),size=(95,-1),choices=self.sampleList,style=wx.CB_DROPDOWN)  
  25.         self.Bind(wx.EVT_COMBOBOX, self.EvtCombobox, self.edithear)  
  26.         self.Bind(wx.EVT_TEXT, self.EvtText, self.edithear)  
  27.           
  28.         self.insure = wx.CheckBox(self,label="Do you want Insured shipment?",pos=(20,180))  
  29.         self.Bind(wx.EVT_CHECKBOX, self.EvtCheckbox, self.insure)  
  30.           
  31.         radioList = ['blue','red','yellow','orange','green','purple','navy blue','black','gray']  
  32.         rb = wx.RadioBox(self,label="what color do you like?",pos=(20,210),choices=radioList,  
  33.                          majorDimension=3,style=wx.RA_SPECIFY_COLS)  
  34.         self.Bind(wx.EVT_RADIOBOX, self.EvtRadiobox, rb)  
  35.           
  36.     def OnClick(self,e):  
  37.         self.logger.AppendText("Click on object with Id %d\n" % e.GetId())  
  38.     def EvtText(self,e):  
  39.         self.logger.AppendText("EvtText:%s\n" % e.GetString())  
  40.     def EvtChar(self,e):  
  41.         self.logger.AppendText("EvtChar:%d\n" % e.GetKeyCode())  
  42.         e.Skip()  
  43.     def EvtCombobox(self,e):  
  44.         self.logger.AppendText("EvtCombox:%s\n" % e.GetString())  
  45.     def EvtCheckbox(self,e):  
  46.         self.logger.AppendText("EvtCheckBox:%d\n" % e.Checked())  
  47.     def EvtRadiobox(self,e):  
  48.         self.logger.AppendText("EvtRadiobox:%d\n" % e.GetInt())  
  49.           
  50. app = wx.App(False)  
  51. frame = wx.Frame(None)  
  52. panel = ExamplePanel(frame)  
  53. frame.Show()  
  54. app.MainLoop() 
0 0
原创粉丝点击