Getting Started with WxPython
第一个HelloWorld程序#!/usr/bin/env pythonimport wxapp = wx.App(False)# Create a new app, don't redirect stdout/stderr to a window.frame = wx.Frame(None, wx.ID_ANY, "Hello World") # A Frame is a top-level window.frame.Show(True) # Show the frame.app.MainLoop()
http://dl.iteye.com/upload/attachment/239472/1f242162-c067-3f08-8d0a-0f858b0b30b4.jpg
接下来看看增加一些文件菜单:
'''Created on Apr 24, 2010@author: Leyond'''import wximport osclass MyFrame(wx.Frame): """we simply dirive a new calss of wx""" def __init__(self,parent, title): wx.Frame.__init__(self,parent,title = title,size=(200,100)) self.control = wx.TextCtrl(self,style = wx.wx.TE_MULTILINE) self.CreateStatusBar() filemenu = wx.Menu() menuOpen = filemenu.Append(wx.ID_OPEN, "&Open"," Open a file to edit") menuItem = filemenu.Append(wx.ID_ABORT,"&About","Information about this program") menuExit = filemenu.Append(wx.ID_EXIT,"E&xit"," Terminate the program") self.Bind(wx.EVT_MENU, self.OnAbout, menuItem) self.Bind(wx.EVT_MENU, self.OnExit, menuExit) self.Bind(wx.EVT_MENU, self.OnOpen, menuOpen) filemenu.AppendSeparator() menuBar =wx.MenuBar() menuBar.Append(filemenu,"&File") self.SetMenuBar(menuBar) self.Show(True) def OnAbout(self,event): dlg = wx.MessageDialog(self,"A small text editon","About the editor") dlg.ShowModal() dlg.Destroy() def OnExit(self,e): self.Close() def OnOpen(self,e): """ Open a file""" self.dirname = '' dlg = wx.FileDialog(self, "Choose a file", self.dirname, "", "*.*", wx.OPEN) if dlg.ShowModal() == wx.ID_OK: self.filename = dlg.GetFilename() self.dirname = dlg.GetDirectory() f = open(os.path.join(self.dirname, self.filename), 'r') self.control.SetValue(f.read()) f.close() dlg.Destroy() app = wx.App(False)frame = MyFrame(None,'Small editor')app.MainLoop()
http://dl.iteye.com/upload/attachment/239474/8b8f2ac6-b465-3437-9cc8-0f64fa7da011.jpg
更多请参考: http://wiki.wxpython.org/Getting%20Started
页:
[1]