1.按鈕
①普通按鈕,原來實作事件的簡單處理
a)聲明如下
#普通按鈕,左邊添加靜态文本資訊
wx.StaticText(parent=self.panel,label='Generally-Button:',pos=(20,20))
self.gButton=wx.Button(parent=self.panel,label='generally',pos=(150,20))
b)為按鈕綁定處理函數
#為普通按鈕綁定方法
self.Bind(wx.EVT_BUTTON,self.OngButton,self.gButton)
c)處理函數的實作
#綁定函數
def OngButton(self,event):
dlg=wx.MessageDialog(self,'Really Quit','Caution',style=wx.CANCEL|wx.OK|wx.ICON_QUESTION)
if dlg.ShowModal()==wx.ID_OK:
dlg.Destroy()
d)運作結果
②單選按鈕,用來實作對象的選擇
a)聲明格式
#單選按鈕
wx.StaticText(parent=self.panel,label='RadioButton:',pos=(20,110))
self.radioButtonsexM=wx.RadioButton(self.panel,-1,'Male',pos=(150,100))
self.radioButtonsexW=wx.RadioButton(self.panel,-1,'Femal',pos=(150,120))
b)運作結果
2.複選框
①聲明格式和綁定函數
#複選按鈕
wx.StaticText(parent=self.panel,label='CheckBox',pos=(20,200))
self.checkButton=wx.CheckBox(self.panel,-1,'Administrator',pos=(150,200))
②運作結果
3.組合框
①聲明格式和綁定函數
#組合框
#設定組合框的值
self.address={'first':['CQ','BJ','SH','SC'],'second':['HC','GZ','XJ','XZ']}
#聲名複合框
self.comBox1=wx.ComboBox(self.panel,value='Address',choices=list(self.address.keys()),pos=(300,20),size=(100,30))
self.Bind(wx.EVT_COMBOBOX,self.OnCombox1,self.comBox1)
self.comBox2=wx.ComboBox(self.panel,value='City',choices=[],pos=(300,50),size=(100,30))
self.Bind(wx.EVT_COMBOBOX,self.OnCombox2,self.comBox2)
②綁定函數實作
#組合框操作
def OnCombox1(self,event):
banji=self.comBox1.GetValue()
self.comBox2.Set(self.address[banji])
def OnCombox2(self,event):
wx.MessageBox(self.comBox2.GetValue())
③運作結果
4.清單框
①聲明格式和綁定函數
#清單框
self.li=['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']
self.listBox=wx.ListBox(self.panel,choices=self.li,pos=(300,100))
self.Bind(wx.EVT_LISTBOX,self.OnlistBox,self.listBox)
②綁定函數實作
#清單框操作
def OnlistBox(self,event):
s=self.listBox.GetStringSelection()
wx.MessageBox(s)
③運作結果
5.通過文本框來實作密碼的驗證
①聲明标簽、文本框和操作按鈕
a)标簽和文本框
#label
self.usernameLabel=wx.StaticText(self.panel,-1,'Username:',pos=(70,300))
self.passwordLabel=wx.StaticText(self.panel,-1,'Password:',pos=(70,330))
#文本框
self.textName=wx.TextCtrl(self.panel,-1,pos=(150,300),size=(160,20))
self.textPwd=wx.TextCtrl(self.panel,-1,pos=(150,330),size=(160,20),style=wx.TE_PASSWORD)
b)按鈕聲明
#普通送出按鈕
self.TextLabel=wx.TextCtrl(parent=self.panel,pos=(185,365))
self.summitButton=wx.Button(self.panel,-1,'Summit',pos=(100,400))
self.clearButton=wx.Button(self.panel,-1,'Clear',pos=(200,400))
self.OutButton=wx.Button(self.panel,-1,'Out',pos=(300,400))
self.Bind(wx.EVT_BUTTON,self.OnSummitButton,self.summitButton)
self.Bind(wx.EVT_BUTTON,self.OnClearButton,self.clearButton)
self.Bind(wx.EVT_BUTTON,self.OnQuitButton,self.OutButton)
②綁定函數的聲明,通過正規表達式來驗正使用者名和密碼輸入
#擷取使用者輸入的使用者名
def getName(self,event):
username=self.textName.GetValue()
str=re.findall(r'[A-Z]{6}\d{3,5}',username)
if len(str)==0:
print('username error!')
else:
self.username=username
return True
#擷取使用者輸入的密碼
def getPwd(self,event):
password=self.textPwd.GetValue()
#print(password)
str=re.findall(r'[a-zA-Z]{1,2}[@]\d+',password)
if len(str)==0:
print('password,error!')
else:
self.password=password
return True
#送出按鈕
def OnSummitButton(self,event):
if not self.getName(event):
self.TextLabel.SetValue('Username error!')
elif self.getPwd(event):
self.TextLabel.SetValue('Password error!')
else:
self.TextLabel.SetValue('Ok')
#清空按鈕
def OnClearButton(self,event):
self.radioButtonsexM.SetValue(False)
self.radioButtonsexW.SetValue(False)
self.checkButton.SetValue(False)
self.textName.SetValue('')
self.textPwd.SetValue('')
#退出按鈕
def OnQuitButton(self,event):
self.Destroy()
③運作結果
6.完整代碼
①代碼如下
import wx
import re
class myButton(wx.Frame):
def __init__(self,superior):
wx.Frame.__init__(self,parent=superior,title='My Button',size=(500,500))
#使用者名和密碼
self.username=''
self.password=''
self.panel=wx.Panel(self,-1)
self.panel.SetBackgroundColour('pink')
#普通按鈕,左邊添加靜态文本資訊
wx.StaticText(parent=self.panel,label='Generally-Button:',pos=(20,20))
self.gButton=wx.Button(parent=self.panel,label='generally',pos=(150,20))
#為普通按鈕綁定方法
self.Bind(wx.EVT_BUTTON,self.OngButton,self.gButton)
#特殊按鈕與複選框
#單選按鈕
wx.StaticText(parent=self.panel,label='RadioButton:',pos=(20,110))
self.radioButtonsexM=wx.RadioButton(self.panel,-1,'Male',pos=(150,100))
self.radioButtonsexW=wx.RadioButton(self.panel,-1,'Femal',pos=(150,120))
#複選按鈕
wx.StaticText(parent=self.panel,label='CheckBox',pos=(20,200))
self.checkButton=wx.CheckBox(self.panel,-1,'Administrator',pos=(150,200))
#label
self.usernameLabel=wx.StaticText(self.panel,-1,'Username:',pos=(70,300))
self.passwordLabel=wx.StaticText(self.panel,-1,'Password:',pos=(70,330))
#文本框
self.textName=wx.TextCtrl(self.panel,-1,pos=(150,300),size=(160,20))
self.textPwd=wx.TextCtrl(self.panel,-1,pos=(150,330),size=(160,20),style=wx.TE_PASSWORD)
#普通送出按鈕
self.TextLabel=wx.TextCtrl(parent=self.panel,pos=(185,365))
self.summitButton=wx.Button(self.panel,-1,'Summit',pos=(100,400))
self.clearButton=wx.Button(self.panel,-1,'Clear',pos=(200,400))
self.OutButton=wx.Button(self.panel,-1,'Out',pos=(300,400))
self.Bind(wx.EVT_BUTTON,self.OnSummitButton,self.summitButton)
self.Bind(wx.EVT_BUTTON,self.OnClearButton,self.clearButton)
self.Bind(wx.EVT_BUTTON,self.OnQuitButton,self.OutButton)
#組合框
#設定組合框的值
self.address={'first':['CQ','BJ','SH','SC'],'second':['HC','GZ','XJ','XZ']}
#聲名複合框
self.comBox1=wx.ComboBox(self.panel,value='Address',choices=list(self.address.keys()),pos=(300,20),size=(100,30))
self.Bind(wx.EVT_COMBOBOX,self.OnCombox1,self.comBox1)
self.comBox2=wx.ComboBox(self.panel,value='City',choices=[],pos=(300,50),size=(100,30))
self.Bind(wx.EVT_COMBOBOX,self.OnCombox2,self.comBox2)
#清單框
self.li=['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']
self.listBox=wx.ListBox(self.panel,choices=self.li,pos=(300,100))
self.Bind(wx.EVT_LISTBOX,self.OnlistBox,self.listBox)
#清單框操作
def OnlistBox(self,event):
s=self.listBox.GetStringSelection()
wx.MessageBox(s)
#組合框操作
def OnCombox1(self,event):
banji=self.comBox1.GetValue()
self.comBox2.Set(self.address[banji])
def OnCombox2(self,event):
wx.MessageBox(self.comBox2.GetValue())
#擷取使用者輸入的使用者名
def getName(self,event):
username=self.textName.GetValue()
str=re.findall(r'[A-Z]{6}\d{3,5}',username)
if len(str)==0:
print('username error!')
else:
self.username=username
return True
#擷取使用者輸入的密碼
def getPwd(self,event):
password=self.textPwd.GetValue()
#print(password)
str=re.findall(r'[a-zA-Z]{1,2}[@]\d+',password)
if len(str)==0:
print('password,error!')
else:
self.password=password
return True
def OnSummitButton(self,event):
if not self.getName(event):
self.TextLabel.SetValue('Username error!')
elif self.getPwd(event):
self.TextLabel.SetValue('Password error!')
else:
self.TextLabel.SetValue('Ok')
def OnClearButton(self,event):
self.radioButtonsexM.SetValue(False)
self.radioButtonsexW.SetValue(False)
self.checkButton.SetValue(False)
self.textName.SetValue('')
self.textPwd.SetValue('')
def OnQuitButton(self,event):
self.Destroy()
#綁定函數
def OngButton(self,event):
s=list(self.address.keys())
print(s)
dlg=wx.MessageDialog(self,'Really Quit','Caution',style=wx.CANCEL|wx.OK|wx.ICON_QUESTION)
if dlg.ShowModal()==wx.ID_OK:
dlg.Destroy()
if __name__=='__main__':
app=wx.App()
button=myButton(None)
button.Show()
app.MainLoop()
②運作結果
學習筆記
1.可以通過靜态文本資訊和按鈕來實作登入注冊界面,普通按鈕通過wx.EVT_BUTTON()來綁定事件處理函數;
2.單選按鈕常用來實作使用者在多個選項中的互斥選擇,在同一組内多個選項中隻能選擇一個,當選擇發生變化之後,之前選中的選項自動失效,可以通過GetValue()方法判斷按鈕是否被選中,使用SetValue(True|False)将按鈕狀态設定為選中或未選中狀态,通過wx.EVT_RADIOBOX()來綁定事件處理函數;
3.複選框常用來實作非互斥多選的功能,多個複選框之間的選擇互不影響,可以通過GetValue()方法判斷按鈕是否被選中,使用SetValue(True|False)将按鈕狀态設定為選中或未選中狀态,通過來wx.EVT_CHECKBOX()綁定事件處理函數;
4.組合框用來實作從固定的多個選項中選擇其一的操作,外觀與文本框類似,但是單擊下拉箭頭會彈出所有可選項,通過wx.EVT_COMBOBOX()來綁定事件處理函數;
5.清單框用來放置多個元素供使用者選擇,其中每個都是字元串,支援使用者單選或多選,通過wx.EVT_LISTBOX()來綁定事件處理函數