wxPython 记事本

#-*- coding:gbk -*-
#file:Simple.py
import wx#导入模块
import os
import time
app=wx.App()#实例化应用程序对象
frame=wx.Frame(None,-1,title="editor",size=(600,400))#创建框架
panel=wx.Panel(frame,-1)#创建画板
openbutton=wx.Button(panel,label="open",size=(40,20))#创建open按钮
savebutton=wx.Button(panel,label="save",size=(40,20))#创建save按钮
savebutton.Enable(False)#按钮显示禁用状态
filename=wx.TextCtrl(panel,-1)#创建文本框
contents=wx.TextCtrl(panel,-1,style=wx.TE_MULTILINE|wx.HSCROLL)#创建文本框,可上下拉,可左右拉
mytime=wx.TextCtrl(panel,-1,size=(80,20))#创建文本款
#mytime.Show(False)
times=wx.Button(panel,label="time",size=(40,20))#创建按钮
def opens(self):#创建函数opens
    if filename.GetValue():#如果有输入
        if os.path.exists(filename.GetValue()):#如果输入文件存在
            if os.path.isdir(filename.GetValue()):#如果输入的是目录
                contents.SetValue("Can't open the folder")#设置文本框内容
            else:
                frame.SetTitle(filename.GetValue())#设置框架标题
                savebutton.Enable(True)#save按钮显示启用状态
                self.file=open(filename.GetValue(),'r')#以只读方式打开文件
                contents.SetValue(self.file.read())#读取文件内容到文本框
                self.file.close()#关闭文件
        else:
            contents.SetValue("No file find")#提示信息
            frame.SetTitle("editor")#
    else:
        contents.SetValue("Please enter the file path")
        frame.SetTitle("editor")
def saves(self):
    if filename.GetValue():
        if os.path.exists(filename.GetValue()):
            if os.path.isdir(filename.GetValue()):
                contents.SetValue("Can't save the folder")
            else:
                self.file=open(filename.GetValue(),'w')
                self.file.write(contents.GetValue())
                self.file.close()
        else:
            contents.SetValue("No file saved,the file not find")
            frame.SetTitle("editor")
    else:
        contents.SetValue("NO file saved")
        frame.SetTitle("editor")
openbutton.Bind(wx.EVT_BUTTON,opens)#按钮事件,执行opens函数
savebutton.Bind(wx.EVT_BUTTON,saves)#按钮事件,执行saves函数
bj=wx.BoxSizer()#实例化布局,默认水平布局
bj.Add(filename,proportion=1,flag=wx.EXPAND|wx.ALL,border=2)#添加filename文本框,proportion=1,wx.EXPAND可扩展,边框设置为2
bj.Add(openbutton,proportion=0,flag=wx.LEFT|wx.ALL,border=2)#添加按钮,proportion=0不可扩展
bj.Add(savebutton,proportion=0,flag=wx.LEFT|wx.ALL,border=2)
bj.Add(times,proportion=0,flag=wx.LEFT|wx.ALL,border=2)
bj.Add(mytime,proportion=1,flag=wx.EXPAND|wx.ALL,border=2)
nbj=wx.BoxSizer(wx.VERTICAL)#垂直布局
nbj.Add(bj,proportion=0,flag=wx.EXPAND|wx.TOP|wx.ALL,border=2)
nbj.Add(contents,proportion=1,flag=wx.EXPAND|wx.BOTTOM|wx.ALL,border=2)
panel.SetSizer(nbj)#画板,定义主板的布局
def timenow(self):
    mytime.SetValue(time.ctime())
times.Bind(wx.EVT_UPDATE_UI,timenow)
frame.Show()#显示框架
app.MainLoop()#进入wxPython,事件循环
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值