欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Getting Started with WxPython

程序员文章站 2022-07-11 08:29:27
...

第一个HelloWorld程序

#!/usr/bin/env python
import wx

app = 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()
 


Getting Started with WxPython

接下来看看增加一些文件菜单:

'''
Created on Apr 24, 2010

@author: Leyond
'''
import wx
import os
class 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()

 
Getting Started with WxPython

 

更多请参考:  http://wiki.wxpython.org/Getting%20Started