Python程序设计之GUI(4)

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()来绑定事件处理函数

发布了78 篇原创文章 · 获赞 83 · 访问量 5399

猜你喜欢

转载自blog.csdn.net/qxyloveyy/article/details/104771848