用wxpython编个类似日记的事件记录工具脚本
用wxpython编个类似日记的事件记录工具脚本,能简单的记录并保存。
环境:Python 3.9.2,Pycharm2021.1社区版。
脚本:
import os, time, wx
class MainWindow(wx.Frame):
def __init__(self, path_file_name_):
wx.Frame.__init__(self, None, title="事件记录面板")
self.path_filename = path_file_name_
# 创建面板
panel = wx.Panel(self)
# 在Panel上添加按键并绑定单击事件
load_button = wx.Button(panel, label='打开')
load_button.Bind(wx.EVT_BUTTON, self.load)
save_button = wx.Button(panel, label='保存')
self.Bind(wx.EVT_BUTTON, self.save, save_button) # (另一种使用方式)
datetime_button = wx.Button(panel, label='添加日期及时间')
datetime_button.Bind(wx.EVT_BUTTON, self.datetime)
# 在Panel上添加文件名框
self.filename = wx.TextCtrl(panel)
self.filename.AppendText(self.path_filename) # 追加文件名
# 在Panel上添加内容框
self.contents = wx.TextCtrl(panel, style=wx.TE_MULTILINE | # 多行显示
wx.TE_PROCESS_ENTER) # 允许回车键触发
self.font = wx.Font(14, wx.SWISS, wx.NORMAL, wx.BOLD) # 字体
self.contents.SetFont(self.font) # 设置内容框字体
box = wx.BoxSizer()
box.Add(self.filename, proportion=1, flag=wx.EXPAND)
box.Add(load_button, proportion=0, flag=wx.LEFT, border=5)
box.Add(save_button, proportion=0, flag=wx.LEFT, border=5)
box.Add(datetime_button, proportion=0, flag=wx.LEFT, border=5)
vbox = wx.BoxSizer(wx.VERTICAL)
vbox.Add(box, proportion=0, flag=wx.EXPAND | wx.ALL, border=5)
vbox.Add(self.contents, proportion=1, flag=wx.EXPAND | wx.LEFT |
wx.BOTTOM | wx.RIGHT, border=5)
panel.SetSizer(vbox)
# 内容框文字有改动触发操作
self.content_change = 0 # 内容框文字有改动标志
self.contents.Bind(wx.EVT_TEXT, self.content_change_trigger,
self.contents) # 内容框文字有改动触发操作
self.contents.Bind(wx.EVT_TEXT_ENTER, self.on_enter) # 回车键按下触发操作
# self.Center() # 窗口放屏幕中心
self.Move(500, 280); self.SetSize(800, 600) # 移动窗体及设定窗体尺寸
self.Show() # 显示
def datetime(self, event):
self.contents.WriteText(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) +
' :\n ') # 加入记录时间信息并换行
self.contents.SetFont(self.font) # 设置字体
self.content_change = 0 # 清标志
def on_enter(self, event): # 回车键按下触发操作
self.contents.WriteText('\n ') # 在当前的插入点舔加换行符及4空格
def content_change_trigger(self, event): # 内容框文字有改动触发操作
self.content_change = 1 # 内容框文字有改动标志置位
def fore_openfile(self): # 预先打开文件
self.create_empty_file() # 如果文件不存在则创建文件
file = open(self.path_filename, "r", encoding="utf-8") # 指定为utf8编码,避免写文件时出现编码错误
self.contents.SetValue(file.read())
file.close()
self.contents.AppendText(time.strftime("%Y-%m-%d %H:%M:%S",
time.localtime()) + ' :\n ') # 加入记录时间信息并换行
self.contents.SetFont(self.font)
self.content_change = 0 # 清内容框文字有变化标志
def load(self, event):
if not os.path.isfile(self.filename.GetValue()): # 文件不存在
wx.MessageBox("文件不存在!", "信息提示!", wx.OK | wx.ICON_INFORMATION)
else:
if not self.content_change: # 内容框文字有变化标志未置位
file = open(self.filename.GetValue(), "r", encoding="utf-8")
self.contents.SetValue(file.read())
file.close()
self.content_change = 0 # 清标志,读文件后,该标志会置位
else:
msg = wx.MessageBox("文件已修改,是否需保存?", "信息提示!", wx.OK | wx.CANCEL)
if msg == wx.OK:
self.save(self)
else:
file = open(self.filename.GetValue(), "r", encoding="utf-8")
self.contents.SetValue(file.read())
file.close()
self.content_change = 0 # 清标志,读文件后,该标志会置位
def save(self, event):
file = open(self.filename.GetValue(), 'w', encoding="utf-8")
file.write(self.contents.GetValue())
file.close()
self.content_change = 0 # 清内容框文字有变化标志
def create_empty_file(self): # The file is created if it does not exist
"""建立备份文件及显示、记录信息"""
if not os.path.isfile(self.path_filename): # 文件不存在
file = open(self.path_filename, 'w', encoding="utf-8") # 如果文件不存在则创建文件
file.close()
if __name__ == "__main__":
path_file_name = '事件记录.txt'
app = wx.App()
frame = MainWindow(path_file_name)
frame.fore_openfile() # 预先打开文件
app.MainLoop()