基于wxpython实现的windows GUI程序实例
本文实例讲述了基于wxpython实现的windowsGUI程序。分享给大家供大家参考。具体如下:
#usingawx.Frame,wx.MenuBar,wx.Menu,wx.Panel,wx.StaticText,wx.Button,
#andawx.BoxSizertoshowarudimentarywxPythonWindowsGUIapplication
#wxPythonpackagefrom:http://prdownloads.sourceforge.net/wxpython/
#Idownloaded:wxPython2.5-win32-ansi-2.5.3.1-py23.exe
#ifyouhavenotalreadydonesoinstallthePythoncompilerfirst
#IusedPython-2.3.4.exe(theWindowsinstallerpackageforPython23)
#fromhttp://www.python.org/2.3.4/
#testedwithPython23vegaseat24jan2005
importwx
classFrame1(wx.Frame):
#createasimplewindowsframe(sometimescalledform)
#pos=(ulcX,ulcY)size=(width,height)inpixels
def__init__(self,parent,title):
wx.Frame.__init__(self,parent,-1,title,pos=(150,150),size=(350,250))
#createamenubaratthetopoftheuserframe
menuBar=wx.MenuBar()
#createamenu...
menu=wx.Menu()
#...addanitemtothemenu
#\tAlt-XcreatesanacceleratorforExit(Alt+xkeys)
#thethirdparameterisanoptionalhintthatshowsupin
#thestatusbarwhenthecursormovesacrossthismenuitem
menu.Append(wx.ID_EXIT,"E&xit\tAlt-X","Exittheprogram")
#bindthemenueventtoaneventhandler,shareQuitBtnevent
self.Bind(wx.EVT_MENU,self.OnQuitButton,id=wx.ID_EXIT)
#putthemenuonthemenubar
menuBar.Append(menu,"&File")
self.SetMenuBar(menuBar)
#createastatusbaratthebottomoftheframe
self.CreateStatusBar()
#nowcreateapanel(betweenmenubarandstatusbar)...
panel=wx.Panel(self)
#...putsomecontrolsonthepanel
text=wx.StaticText(panel,-1,"HelloWorld!")
text.SetFont(wx.Font(24,wx.SCRIPT,wx.NORMAL,wx.BOLD))
text.SetSize(text.GetBestSize())
quitBtn=wx.Button(panel,-1,"Quit")
messBtn=wx.Button(panel,-1,"Message")
#bindthebuttoneventstoeventhandlers
self.Bind(wx.EVT_BUTTON,self.OnQuitButton,quitBtn)
self.Bind(wx.EVT_BUTTON,self.OnMessButton,messBtn)
#useasizertolayoutthecontrols,stackedvertically
#witha10pixelborderaroundeach
sizer=wx.BoxSizer(wx.VERTICAL)
sizer.Add(text,0,wx.ALL,10)
sizer.Add(quitBtn,0,wx.ALL,10)
sizer.Add(messBtn,0,wx.ALL,10)
panel.SetSizer(sizer)
panel.Layout()
defOnQuitButton(self,evt):
#eventhandlerfortheQuitbuttonclickorExitmenuitem
print"Seeyoulateralligator!(goestostdoutwindow)"
wx.Sleep(1)#1secondtolookatmessage
self.Close()
defOnMessButton(self,evt):
#eventhandlerfortheMessagebuttonclick
self.SetStatusText('101DifferentWaystoSpell"Spam"')
classwxPyApp(wx.App):
defOnInit(self):
#setthetitletoo
frame=Frame1(None,"wxPythonGUI2")
self.SetTopWindow(frame)
frame.Show(True)
returnTrue
#getitgoing...
app=wxPyApp(redirect=True)
app.MainLoop()
希望本文所述对大家的Python程序设计有所帮助。