本文实例讲述了基于wxpython实现的windows gui程序。分享给大家供大家参考。具体如下:
# using a wx.frame, wx.menubar, wx.menu, wx.panel, wx.statictext, wx.button,
# and a wx.boxsizer to show a rudimentary wxpython windows gui application
# wxpython package from: http://prdownloads.sourceforge.net/wxpython/
# i downloaded: wxpython2.5-win32-ansi-2.5.3.1-py23.exe
# if you have not already done so install the python compiler first
# i used python-2.3.4.exe (the windows installer package for python23)
# from http://www.python.org/2.3.4/
# tested with python23 vegaseat 24jan2005
import wx
class frame1(wx.frame):
# create a simple windows frame (sometimes called form)
# pos=(ulcx,ulcy) size=(width,height) in pixels
def __init__(self, parent, title):
wx.frame.__init__(self, parent, -1, title, pos=(150, 150), size=(350, 250))
# create a menubar at the top of the user frame
menubar = wx.menubar()
# create a menu …
menu = wx.menu()
# … add an item to the menu
# \talt-x creates an accelerator for exit (alt + x keys)
# the third parameter is an optional hint that shows up in
# the statusbar when the cursor moves across this menu item
menu.append(wx.id_exit, “e&xit\talt-x”, “exit the program”)
# bind the menu event to an event handler, share quitbtn event
self.bind(wx.evt_menu, self.onquitbutton, id=wx.id_exit)
# put the menu on the menubar
menubar.append(menu, “&file”)
self.setmenubar(menubar)
# create a status bar at the bottom of the frame
self.createstatusbar()
# now create a panel (between menubar and statusbar) …
panel = wx.panel(self)
# … put some controls on the panel
text = wx.statictext(panel, -1, “hello world!”)
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”)
# bind the button events to event handlers
self.bind(wx.evt_button, self.onquitbutton, quitbtn)
self.bind(wx.evt_button, self.onmessbutton, messbtn)
# use a sizer to layout the controls, stacked vertically
# with a 10 pixel border around each
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()
def onquitbutton(self, evt):
# event handler for the quit button click or exit menu item
print “see you later alligator! (goes to stdout window)”
wx.sleep(1) # 1 second to look at message
self.close()
def onmessbutton(self, evt):
# event handler for the message button click
self.setstatustext(‘101 different ways to spell “spam”‘)
class wxpyapp(wx.app):
def oninit(self):
# set the title too
frame = frame1(none, “wxpython gui 2”)
self.settopwindow(frame)
frame.show(true)
return true
# get it going …
app = wxpyapp(redirect=true)
app.mainloop()
希望本文所述对大家的python程序设计有所帮助。