今天主要完善昨天简单UI窗体的上面的事件,文件打开,文件保存,窗口关闭三个事件。
需要准备的内容为:
- 引用 import os
- 定义三个事件(fileopen,savefile,closeWindows)
- 绑定事件
- 简单设置了一下文本框的背景
- 重点介绍一下,FileDialog
1)引用 import os
import os
主要使用它的os.getcwd()方法;查了一下这个方法是
getcwd()函数:取得当前的工作目录(working directory)
将当前工作目录赋给,FileDialogs对话框。
2)定义三个事件
def OnfileOpen(event):
def onsavefile(event):
def closeWindows(event):#定义关闭窗口事件
这里对于命名方法,需要重新学习一下,一般是使用动词+名词,第一个文件打开典型中国程序员,哈哈;
3)绑定事件
open_button = wx.Button(panel,label = "打开")
open_button.Bind(wx.EVT_BUTTON,OnfileOpen) # 绑定打开文件事件到open_button按钮上
save_button = wx.Button(panel,label = "保存")
save_button.Bind(wx.EVT_BUTTON,onsavefile)
close_button =wx.Button(panel,label ='Close') #增加关闭按钮;
close_button.Bind(wx.EVT_BUTTON,closeWindows) #绑定关闭事件
4)设置文本框的背景,by the way
content_text.SetBackgroundColour("While")
5)重点介绍一下,FileDialog
file_wildcard的介绍,大家一看应该是知道是windows的特点,需要默认打开文件扩展名。
style 是一个关键参数从官方的内容里面有以下几个:

'''
打开开文件对话框
'''
file_wildcard = "TXT files(*.txt)|*.txt|All files(*.*)|*.*"
dlg = wx.FileDialog(frame, "Open file file...",
os.getcwd(),
style =wx.FD_OPEN,
wildcard = file_wildcard)
#print(dlg.GetPath())
if dlg.ShowModal() == wx.ID_OK:
path = dlg.GetPath()
path_text.SetValue(path)
content_text.SetBackgroundColour("While")
#print(path)
#path = path_text.GetValue()
with open(path,"r",encoding="utf-8") as f: # encoding参数是为了在打开文件时将编码转为utf8
content_text.SetValue(f.read())
dlg.Destroy()
最后丢出所有代码,这些代码可以运行于win7,python3.7以上的系统
#coding:utf-8
import wx
import os
import datetime
def OnfileOpen(event):
'''
打开开文件对话框
'''
file_wildcard = "TXT files(*.txt)|*.txt|All files(*.*)|*.*"
dlg = wx.FileDialog(frame, "Open file file...",
os.getcwd(),
style =wx.FD_OPEN,
wildcard = file_wildcard)
#print(dlg.GetPath())
if dlg.ShowModal() == wx.ID_OK:
path = dlg.GetPath()
path_text.SetValue(path)
content_text.SetBackgroundColour("While")
#print(path)
#path = path_text.GetValue()
with open(path,"r",encoding="utf-8") as f: # encoding参数是为了在打开文件时将编码转为utf8
content_text.SetValue(f.read())
dlg.Destroy()
def onsavefile(event):
'''
文件保存
:param event:
:return:
'''
file_wildcard = "TXT files(*.txt)|*.txt|All files(*.*)|*.*"
dlg = wx.FileDialog(frame, "Open file file...",
os.getcwd(),
style =wx.FD_SAVE,
wildcard = file_wildcard)
if dlg.ShowModal() == wx.ID_OK:
path =dlg.Path
content_text.SaveFile(path)
dlg.Destroy()
def closeWindows(event):#定义关闭窗口事件
frame.Close()
def OnPaintMotion( event):
#设置状态栏1内容
print(str(event.GetPosition()))
str1= str(event.GetPosition())
statusbar.SetStatusText("鼠标位置:" +str1 , 0)
nowdate = datetime.datetime.now().strftime('%Y-%m-%d')
nowtime = datetime.datetime.now().strftime('%H:%M:%S')
#设置状态栏2内容
statusbar.SetStatusText("当前日期:"+nowdate,1)
#设置状态栏3内容
statusbar.SetStatusText("当前时间:"+nowtime , 2)
event.Skip()
app = wx.App()
frame = wx.Frame(None,title = "Gui Test Editor",pos = (1000,200),size = (800,400))
panel = wx.Panel(frame)
path_text = wx.TextCtrl(panel)
open_button = wx.Button(panel,label = "打开")
open_button.Bind(wx.EVT_BUTTON,OnfileOpen) # 绑定打开文件事件到open_button按钮上
save_button = wx.Button(panel,label = "保存")
save_button.Bind(wx.EVT_BUTTON,onsavefile)
close_button =wx.Button(panel,label ='Close') #增加关闭按钮;
close_button.Bind(wx.EVT_BUTTON,closeWindows) #绑定关闭事件
content_text= wx.TextCtrl(panel,style = wx.TE_MULTILINE)
# wx.TE_MULTILINE可以实现以滚动条方式多行显示文本,若不加此功能文本文档显示为一行
box = wx.BoxSizer() # 不带参数表示默认实例化一个水平尺寸器
box.Add(path_text,proportion = 10,flag = wx.EXPAND|wx.ALL,border = 3) # 添加组件
#proportion:相对比例
#flag:填充的样式和方向,wx.EXPAND为完整填充,wx.ALL为填充的方向
#border:边框
box.Add(open_button,proportion = 2,flag = wx.EXPAND|wx.ALL,border = 3) # 添加组件
box.Add(save_button,proportion = 2,flag = wx.EXPAND|wx.ALL,border = 3) # 添加组件
box.Add(close_button,proportion = 2,flag = wx.EXPAND|wx.ALL,border = 3) # 添加组件
v_box = wx.BoxSizer(wx.VERTICAL) # wx.VERTICAL参数表示实例化一个垂直尺寸器
v_box.Add(box,proportion = 1,flag = wx.EXPAND|wx.ALL,border = 3) # 添加组件
v_box.Add(content_text,proportion = 5,flag = wx.EXPAND|wx.ALL,border = 3) # 添加组件
panel.SetSizer(v_box) # 设置主尺寸器
#增加状态栏
statusbar = frame.CreateStatusBar()
statusbar.SetFieldsCount(3)
statusbar.SetStatusWidths([-1,-2,-3])
statusbar.SetStatusText("Welcome to Wxpyon",0)
statusbar.SetStatusText("Welcome to Wxpyon,X:",1)
statusbar.SetStatusText("Welcome to Wxpyon,Y:",2)
path_text.Bind(wx.EVT_MOTION,OnPaintMotion)
panel.Bind(wx.EVT_MOTION,OnPaintMotion)
content_text.Bind(wx.EVT_MOTION,OnPaintMotion)
frame.Show()
app.MainLoop()
Python 3.7初学者,欢迎交流!求推荐命名规范及其他学习材料;
我好像离自动点赞,还有点远。
争取下周搞定简书,自动给朋友点赞的功能 。