wxpython 例子

#encoding=utf-8
#!/usr/bin/python
# Copyright (c) 2001-2004 Twisted Matrix Laboratories.
# See LICENSE for details.

import os
import sys
import wx
ID_OPEN = 101
ID_EXIT = 110
ID_SAVE = 111
ID_BUTTON = 112

class MainWindow(wx.Frame):
    """ We simply derive a new class of Frame. """
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, size=(500,100))
        self.control = wx.TextCtrl(self, 1, style=wx.TE_MULTILINE)
        self.CreateStatusBar()
        filemenu  = wx.Menu()
        filemenu.Append(ID_OPEN,"打开文件","open file")
        filemenu.AppendSeparator()
        filemenu.Append(ID_SAVE,"保存文件"," save file")
        filemenu.AppendSeparator()
        filemenu.Append(ID_EXIT,"退出","exit")
        menuBar = wx.MenuBar()
        menuBar.Append(filemenu,"文件")
        self.SetMenuBar(menuBar)
        wx.EVT_MENU(self,ID_OPEN,self.open)
        wx.EVT_MENU(self,ID_EXIT,self.exit)
        wx.EVT_MENU(self,ID_SAVE,self.save)
        self.sizer2 = wx.BoxSizer(wx.HORIZONTAL)
        self.buttons = []
       
#        for i in range(0,6):
#            self.buttons.append(wx.Button(self,ID_BUTTON+i,"Button &"+'i'))   
#            self.sizer2.Add(self.buttons[i],1,wx.EXPAND)
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.control,1,wx.EXPAND)
        self.sizer.Add(self.sizer2,0,wx.EXPAND)
        self.SetSizer(self.sizer)
        self.SetAutoLayout(1)
        self.sizer.Fit(self)
       
        self.Show(True)
       
    def exit(self,e):
        '''用户退出窗口'''
        self.Close(True)
       
    def open(self,e):
        '''打开文件 '''
        self.dirname = ''
        dlg = wx.FileDialog(self,"chose 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()
       
    def save(self,e):
        '''保存文件'''
        try:
            f = open(os.path.join(self.dirname,self.filename),'w')
        except AttributeError:
            print '文件不存在'
            sys.exit(0)
           
        content = self.control.GetValue()
        try:
            f.write(content)
        except UnboundLocalError:
            print '文件不存在'
            sys.exit(0)
        finally:
            f.close()
                   
   
app = wx.PySimpleApp()
frame=MainWindow(None,-1, 'Small editor')
app.MainLoop()


直接运行就可以生成一个窗口,有打开和关闭的功能,其他的功能没有完善

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值