1.概述
python中常用GUI的框架有Tkinter, pyqt, wxpython.
其中Tkinter适合微型图形界面的开发,
而pyqt, wxpython则适合中小型的图形界面开发。
其中PyQt可以直接使用Qt Designer生成界面,比较便利。
而wxpython需要用代码去控制界面的生成。
如果没有特殊需求,一般写GUI的话,这三个选择一个就可以。
下面仅给出wxpython的使用demo.
2.Demo
# coding=gbk
import wx
class myframe(wx.Frame):
def __init__(self):
#main window
wx.Frame.__init__(self,None,-1,"面板模板",size=(400,500),style=
wx.MINIMIZE_BOX | wx.RESIZE_BORDER |
wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX)
panel=wx.Panel(self)
# setup statictext 静态文本
# setup checkbox statictext
self.checkboxlabel=wx.StaticText(panel,-1,'checkbox',(50,20),(80,20))
self.checkboxlabel.SetBackgroundColour("white")
self.checkboxlabel.SetForegroundColour('red')
#setup listbox statictext
self.listboxlabel=wx.StaticText(panel,-1,'listbox',(50,110),(80,20))
self.listboxlabel.SetBackgroundColour("white")
self.listboxlabel.SetForegroundColour('red')
#setup choice statictext
self.choicelabel=wx.StaticText(panel,-1,'choice',(50,200),(80,20))
self.choicelabel.SetBackgroundColour("white")
self.choicelabel.SetForegroundColour('red')
#setup slider statictext
self.sliderlabel=wx.StaticText(panel,-1,'slider',(200,180),(80,20))
self.sliderlabel.SetBackgroundColour("white")
self.sliderlabel.SetForegroundColour('red')
#setup ctrltext statictext
self.ctrllabel=wx.StaticText(panel,-1,'textctrl',(200,240),(80,20))
self.ctrllabel.SetBackgroundColour("white")
self.ctrllabel.SetForegroundColour('red')
# textctrl 动态文本
self.text=wx.TextCtrl(panel,-1,"write",(200,260))
self.text.SetInsertionPoint(0)
# slider 滑块框
self.slider=wx.Slider(panel,-1,20,1,100,pos=(200,200),size=(100,30))
# setup choice 选择框
mylist1=['xin','gui','meng','sing','boy','girl','guy']
self.choice=wx.Choice(panel,-1,(50,220),choices=mylist1)
self.choice.SetSelection(1)
# setup listbox 列表框
mylist=['xin','gui','meng','sing','boy','girl']
self.listbox=wx.ListBox(panel,-1,(50,130),(80,60),mylist,wx.LB_SINGLE)
self.listbox.SetSelection(1)
# setup radiobox 单选框
list1=["wang","shu","wan"]
list2=["boy","girl"]
self.radiobox1=wx.RadioBox(panel,-1,"radiobox",(200,20),wx.DefaultSize,
list1,3,wx.RA_SPECIFY_COLS)
self.radiobox2=wx.RadioBox(panel,-1,"radiobox",(200,100),wx.DefaultSize,
list2,2,wx.RA_SPECIFY_ROWS)
# setup checkbox 复选框
self.check1=wx.CheckBox(panel,-1,"wang",(50,40),(150,20))
self.check2=wx.CheckBox(panel,-1,"shu",(50,60),(150,20))
self.check3=wx.CheckBox(panel,-1,"wan",(50,80),(150,20))
# use button to creat ok and cancel option 按扭
self.ok=wx.Button(panel,-1,"确定",pos=(50,400))
self.cancel=wx.Button(panel,-1,"取消",pos=(250,400))
self.Bind(wx.EVT_BUTTON,self.Onokclick,self.ok)
self.Bind(wx.EVT_BUTTON,self.Oncancelclick,self.cancel)
def Onokclick(self,event):
print "check1", self.check1.GetValue()
print "check2", self.check2.GetValue()
print "check3", self.check3.GetValue()
print "slider", self.slider.GetValue()
print "textctrl", self.text.GetValue()
print "choice", self.choice.GetStringSelection()
def Oncancelclick(self,event):
print "radiobox1",self.radiobox1.GetStringSelection()
print "radiobox2",self.radiobox2.GetStringSelection()
print "listbox",self.listbox.GetStringSelection()
if __name__=='__main__':
app=wx.App()
frame=myframe()
frame.Show()
app.MainLoop()