wx 文件编辑框

时间:2023-03-09 02:34:24
wx 文件编辑框
# -*- coding: utf- -*-
import wx
import os
class my_frame(wx.Frame):
"""This is a simple text editor"""
def __init__(self,parent, title):
wx.Frame.__init__(self, parent, title=title,size=(,))
self.control = wx.TextCtrl(self, -,"请先打开要修改的文件", style=wx.TE_MULTILINE,)
self.Show(True)
self.CreateStatusBar()#创建窗口底部的状态栏 filemenu = wx.Menu()
menu_open = filemenu.Append(wx.ID_OPEN, "打开文件", " ")
menu_save = filemenu.Append(wx.ID_SAVE, "保存修改",)
menu_exit = filemenu.Append(wx.ID_EXIT, "Exit", "Termanate the program")
filemenu.AppendSeparator()
menu_about = filemenu.Append(wx.ID_ABOUT, "About", "Information about this program")#设置菜单的内容 menuBar = wx.MenuBar()
menuBar.Append(filemenu, "选项")
self.SetMenuBar(menuBar)#创建菜单条
self.Show(True) self.Bind(wx.EVT_MENU, self.on_open, menu_open)
self.Bind(wx.EVT_MENU, self.on_about, menu_about)
self.Bind(wx.EVT_MENU, self.on_exit, menu_exit)#把出现的事件,同需要处理的函数连接起来
self.Bind(wx.EVT_MENU, self.on_save, menu_save) def on_about(self,e):#about按钮的处理函数
dlg = wx.MessageDialog(self,"A samll text editor", "About sample Editor",wx.OK)#创建一个对话框,有一个ok的按钮
dlg.ShowModal()#显示对话框
dlg.Destroy()#完成后,销毁它。 def on_exit(self,e):
self.Close(True) def on_open(self,e):
"""open a file""" dlg = wx.FileDialog(self,
message="Choose a file",
defaultDir="",
defaultFile="",
wildcard="*.*",
style=wx.FD_OPEN | wx.FD_CHANGE_DIR)#调用一个函数打开对话框 if dlg.ShowModal() == wx.ID_OK:
self.filename = dlg.GetFilename()
self.dirname = dlg.GetDirectory()
self.address = os.path.join(self.dirname,self.filename)
f = open(self.address,"r")
file = (f.read()) #python3 不需要解码
f.close()
self.control.Clear()
self.control.AppendText(file)#把打开的文件内容显示在多行文本框内
dlg.Destroy() def on_save(self, e):
date = (self.control.GetValue()) #python3 不需要编码
f = open(self.address, 'w')
f.write(date)
f.close()#把文本框内的数据写入并关闭文件
dlg = wx.MessageDialog(self, "文件已经成功保存", "消息提示", wx.OK)
dlg.ShowModal()
dlg.Destroy()
self.control.Clear()
self.control.AppendText('欢迎使用此软件,作者即刻') app = wx.App(False)
frame = my_frame(None, '迷你文本编辑器')
app.MainLoop()