1、开始
接触Python 也有一段时间了,o.o ,断断续续加起来没几天。
一般新学习一门新语言,除了必先输出一个 Hello World 外,都会以模拟 Windows 记事本来写一个结合自己想法的记事本小程序。
o.o ,玩了这么久是该写点什么。
2、构建菜单栏
o.o ,浪了 浪了,仿着 记事本 构建了下菜单 ,还没绑定事件。
搭建了下 代码的整体框架方向,完成了菜单栏的布局,接下来就是绑定事件了。
效果图:
该阶段时滴代码:
#coding:utf-8
#acthor:Twobox import wx class Mywin(wx.Frame):
def __init__(self, parent, title):
super(Mywin, self).__init__(parent, title = title)
self.InitUI() def InitUI(self):
self.InitUIMenuBar()
self.SetSize((400,600))
self.Center()
self.Show() def InitUIMenuBar(self):
menuBar = wx.MenuBar() # 构造 文件 菜单
fileMenu = wx.Menu() newItem = wx.MenuItem(fileMenu, id=wx.ID_NEW, text="新建(N)\tCtrl+N", kind=wx.ITEM_NORMAL)
openItem = wx.MenuItem(fileMenu, id=wx.ID_OPEN, text="打开(O)...\tCtrl+O", kind=wx.ITEM_NORMAL)
saveItem = wx.MenuItem(fileMenu, id=wx.ID_SAVE, text="保存(S)\tCtrl+S", kind=wx.ITEM_NORMAL)
saveasItem = wx.MenuItem(fileMenu, id=wx.ID_SAVEAS, text="另存为(A)...", kind=wx.ITEM_NORMAL) fileMenu.AppendItem(newItem)
fileMenu.AppendItem(openItem)
fileMenu.AppendItem(saveItem)
fileMenu.AppendItem(saveasItem)
fileMenu.AppendSeparator() exitItem = wx.MenuItem(fileMenu, id=wx.ID_EXIT, text="退出(X)", kind=wx.ITEM_NORMAL)
fileMenu.AppendItem(exitItem) menuBar.Append(fileMenu, title="文件(F)") # 构建 编辑 菜单
deitMenu = wx.Menu() deitMenu.Append(id=21, item="撤销(U)\tCtrl+Z", kind=wx.ITEM_NORMAL)
deitMenu.AppendSeparator()
deitMenu.Append(id=wx.ID_CUT, item="剪切(T)\tCtrl+X", kind=wx.ITEM_NORMAL)
deitMenu.Append(id=wx.ID_COPY, item="复制(C)\tCtrl+C", kind=wx.ITEM_NORMAL)
deitMenu.Append(id=wx.ID_PASTE, item="粘贴(P)\tCtrl+V", kind=wx.ITEM_NORMAL)
deitMenu.Append(id=wx.ID_DELETE, item="删除(L)\tDel", kind=wx.ITEM_NORMAL)
deitMenu.AppendSeparator()
deitMenu.Append(id=wx.ID_FIND, item="查找(F)...\tCtrl+F", kind=wx.ITEM_NORMAL)
deitMenu.Append(id=27, item="查找下一个(N)\tF3")
deitMenu.Append(id=wx.ID_REPLACE, item="替换(R)...\tCtrl+H")
deitMenu.Append(id=29, item="转到(G)\tCtrl+G")
deitMenu.AppendSeparator()
deitMenu.Append(id=wx.ID_SELECTALL, item="全选(A)\tCtrl+A")
deitMenu.Append(id=211, item="时间/日期(D)\tF5") menuBar.Append(deitMenu, title="编辑(E)") #构建 格式 菜单
formatMenu = wx.Menu() formatMenu.Append(id = 31, item = "自动换行(W)", kind = wx.ITEM_CHECK)
formatMenu.Append(id = 32, item = "字体(F)...") menuBar.Append(formatMenu, title = "格式(O)") #构建 查看 菜单
seeMenu = wx.Menu() seeMenu.Append(id = 41, item = "状态栏(S)", kind = wx.ITEM_CHECK) menuBar.Append(seeMenu, title = "查看(V)") #构建 帮助 菜单
helpMenu = wx.Menu() helpMenu.Append(id = wx.ID_HELP, item = "查看帮助(H)")
helpMenu.Append(id = wx.ID_ABOUT, item = "关于记事本(A)") menuBar.Append(helpMenu, title = "帮助(H)") self.SetMenuBar(menuBar) def main():
print("GO...")
app = wx.App()
Mywin(None, "无标题 - 记事本")
app.MainLoop()
exit(0) if __name__ == "__main__":
main()
3、完成 编辑框与状态栏
本还想把一些时间也完成一下,但一直在就纠结 (ˉ▽ˉ;)...
干脆这下把界面都完成以下 ,后面再一点一点完成事件算啦~~。
效果图:
该阶段时滴代码:
#coding:utf-8
#acthor:Twobox import wx class Mywin(wx.Frame):
def __init__(self, parent, title):
super(Mywin, self).__init__(parent, title = title)
self.InitUI() def InitUI(self):
self.initUIMenuBar() # 初始化 菜单栏
self.initUIStatusBar() # 初始化 状态栏
self.initUIMainWindow() # 构建 窗口面板
self.adjustmentWin() # 调整 窗口框体参数 def initUIMainWindow(self):
panel = wx.Panel(self)
vbox = wx.BoxSizer(wx.VERTICAL) self.textBox = wx.TextCtrl(panel, id = -1, style = wx.TE_MULTILINE)
# self.textBox.Bind(wx.EVT_TEXT, self.eventTextCtrl)
vbox.Add(self.textBox, proportion = 1, flag = wx.EXPAND|wx.ALL, border = 0) panel.SetSizer(vbox) def initUIStatusBar(self):
# 实例化一个 状态栏 对象
self.statusBar = wx.StatusBar(parent = self, id = -1)
self.statusBar.SetFieldsCount(2)
self.statusBar.SetStatusWidths([-3.9, -1])
self.statusBar.SetStatusText(" 第 1 行 , 第 1 列", 1)
self.SetStatusBar(self.statusBar)
self.statusBar.Show(True) def adjustmentWin(self):
self.SetSize((400, 600))
self.Center()
self.Show() def initUIMenuBar(self):
menuBar = wx.MenuBar() # 构造 文件 菜单
fileMenu = wx.Menu() newItem = wx.MenuItem(fileMenu, id=wx.ID_NEW, text="新建(N)\tCtrl+N", kind=wx.ITEM_NORMAL)
openItem = wx.MenuItem(fileMenu, id=wx.ID_OPEN, text="打开(O)...\tCtrl+O", kind=wx.ITEM_NORMAL)
saveItem = wx.MenuItem(fileMenu, id=wx.ID_SAVE, text="保存(S)\tCtrl+S", kind=wx.ITEM_NORMAL)
saveasItem = wx.MenuItem(fileMenu, id=wx.ID_SAVEAS, text="另存为(A)...", kind=wx.ITEM_NORMAL) fileMenu.AppendItem(newItem)
fileMenu.AppendItem(openItem)
fileMenu.AppendItem(saveItem)
fileMenu.AppendItem(saveasItem)
fileMenu.AppendSeparator() exitItem = wx.MenuItem(fileMenu, id=wx.ID_EXIT, text="退出(X)", kind=wx.ITEM_NORMAL)
fileMenu.AppendItem(exitItem) menuBar.Append(fileMenu, title="文件(&F)") # 构建 编辑 菜单
deitMenu = wx.Menu() deitMenu.Append(id=21, item="撤销(U)\tCtrl+Z", kind=wx.ITEM_NORMAL)
deitMenu.AppendSeparator()
deitMenu.Append(id=wx.ID_CUT, item="剪切(T)\tCtrl+X", kind=wx.ITEM_NORMAL)
deitMenu.Append(id=wx.ID_COPY, item="复制(C)\tCtrl+C", kind=wx.ITEM_NORMAL)
deitMenu.Append(id=wx.ID_PASTE, item="粘贴(P)\tCtrl+V", kind=wx.ITEM_NORMAL)
deitMenu.Append(id=wx.ID_DELETE, item="删除(L)\tDel", kind=wx.ITEM_NORMAL)
deitMenu.AppendSeparator()
deitMenu.Append(id=wx.ID_FIND, item="查找(F)...\tCtrl+F", kind=wx.ITEM_NORMAL)
deitMenu.Append(id=27, item="查找下一个(N)\tF3")
deitMenu.Append(id=wx.ID_REPLACE, item="替换(R)...\tCtrl+H")
deitMenu.Append(id=29, item="转到(G)\tCtrl+G")
deitMenu.AppendSeparator()
deitMenu.Append(id=wx.ID_SELECTALL, item="全选(A)\tCtrl+A")
deitMenu.Append(id=211, item="时间/日期(D)\tF5") menuBar.Append(deitMenu, title="编辑(&E)") #构建 格式 菜单
formatMenu = wx.Menu() formatMenu.Append(id = 31, item = "自动换行(W)", kind = wx.ITEM_CHECK)
formatMenu.Append(id = 32, item = "字体(F)...") menuBar.Append(formatMenu, title = "格式(&O)") #构建 查看 菜单
seeMenu = wx.Menu() seeMenu.Append(id = 41, item = "状态栏(&S)", kind = wx.ITEM_CHECK) menuBar.Append(seeMenu, title = "查看(&V)") #构建 帮助 菜单
helpMenu = wx.Menu() helpMenu.Append(id = wx.ID_HELP, item = "查看帮助(H)")
helpMenu.Append(id = wx.ID_ABOUT, item = "关于记事本(A)") menuBar.Append(helpMenu, title = "帮助(&H)") self.SetMenuBar(menuBar) # 以下是 绑定的事件
# def eventTextCtrl(self, event):
# print(self.textBox.Get) def main():
print("GO...")
app = wx.App()
Mywin(None, "无标题 - 记事本")
app.MainLoop()
exit(0) if __name__ == "__main__":
main()
4、完成了一小部分功能
1、完成 帮助菜单 下的 查看帮助项 与 关于记事本项
查看帮助项:为单击后跳转帮助页面,win10是跳转到微软官网,我就跳转到咱博客啦~
关于记事本项:显示 记事本与作者相关信息的一个 确定消息窗口。
2、检查框 确定是否显示 状态栏
3、注册 菜单热键,即按下 Alt+字母下有下划线的字母,实现键盘点击
该阶段时滴代码:
# encoding: utf-8
# acthor: Twobox import wx
import webbrowser class Mywin(wx.Frame):
"""Author: Twobox"""
about = """
作者:Twobox
扣扣:200562421
源码共享博客:http://www.cnblogs.com/Twobox/
"""
def __init__(self, parent, title):
super(Mywin, self).__init__(parent, title = title)
self.InitUI() def InitUI(self):
self.initUIMenuBar() # 初始化 菜单栏
self.initUIStatusBar() # 初始化 状态栏
self.initUIMainWindow() # 构建 窗口面板
self.adjustmentWin() # 调整 窗口框体参数 def initUIMainWindow(self):
panel = wx.Panel(self)
vbox = wx.BoxSizer(wx.VERTICAL) self.textBox = wx.TextCtrl(panel, id = -1, style = wx.TE_MULTILINE)
# self.textBox.Bind(wx.EVT_TEXT, self.eventTextCtrl)
vbox.Add(self.textBox, proportion = 1, flag = wx.EXPAND|wx.ALL, border = 0) panel.SetSizer(vbox) def initUIStatusBar(self):
# 实例化一个 状态栏 对象
self.statusBar = wx.StatusBar(parent = self, id = -1)
self.statusBar.SetFieldsCount(2)
self.statusBar.SetStatusWidths([-3.9, -1])
self.statusBar.SetStatusText(" 第 1 行 , 第 1 列", 1)
self.SetStatusBar(self.statusBar)
self.statusBar.Show(True) def adjustmentWin(self):
self.SetSize((400, 600))
self.Center()
self.Show() def initUIMenuBar(self):
menuBar = wx.MenuBar() # 构造 文件 菜单
fileMenu = wx.Menu() newItem = wx.MenuItem(fileMenu, id=wx.ID_NEW, text="新建(&N)\tCtrl+N", kind=wx.ITEM_NORMAL)
openItem = wx.MenuItem(fileMenu, id=wx.ID_OPEN, text="打开(&O)...\tCtrl+O", kind=wx.ITEM_NORMAL)
saveItem = wx.MenuItem(fileMenu, id=wx.ID_SAVE, text="保存(&S)\tCtrl+S", kind=wx.ITEM_NORMAL)
saveasItem = wx.MenuItem(fileMenu, id=wx.ID_SAVEAS, text="另存为(&A)...", kind=wx.ITEM_NORMAL) fileMenu.AppendItem(newItem)
fileMenu.AppendItem(openItem)
fileMenu.AppendItem(saveItem)
fileMenu.AppendItem(saveasItem)
fileMenu.AppendSeparator() exitItem = wx.MenuItem(fileMenu, id=wx.ID_EXIT, text="退出(&X)", kind=wx.ITEM_NORMAL)
fileMenu.AppendItem(exitItem) menuBar.Append(fileMenu, title="文件(&F)") # 构建 编辑 菜单
deitMenu = wx.Menu() deitMenu.Append(id=21, item="撤销(&U)\tCtrl+Z", kind=wx.ITEM_NORMAL)
deitMenu.AppendSeparator()
deitMenu.Append(id=wx.ID_CUT, item="剪切(&T)\tCtrl+X", kind=wx.ITEM_NORMAL)
deitMenu.Append(id=wx.ID_COPY, item="复制(&C)\tCtrl+C", kind=wx.ITEM_NORMAL)
deitMenu.Append(id=wx.ID_PASTE, item="粘贴(&P)\tCtrl+V", kind=wx.ITEM_NORMAL)
deitMenu.Append(id=wx.ID_DELETE, item="删除(&L)\tDel", kind=wx.ITEM_NORMAL)
deitMenu.AppendSeparator()
deitMenu.Append(id=wx.ID_FIND, item="查找(&F)...\tCtrl+F", kind=wx.ITEM_NORMAL)
deitMenu.Append(id=27, item="查找下一个(&N)\tF3")
deitMenu.Append(id=wx.ID_REPLACE, item="替换(&R)...\tCtrl+H")
deitMenu.Append(id=29, item="转到(&G)\tCtrl+G")
deitMenu.AppendSeparator()
deitMenu.Append(id=wx.ID_SELECTALL, item="全选(&A)\tCtrl+A")
deitMenu.Append(id=211, item="时间/日期(&D)\tF5") menuBar.Append(deitMenu, title="编辑(&E)") #构建 格式 菜单
formatMenu = wx.Menu() formatMenu.Append(id = 31, item = "自动换行(&W)", kind = wx.ITEM_CHECK)
formatMenu.Append(id = 32, item = "字体(&F)...") menuBar.Append(formatMenu, title = "格式(&O)") #构建 查看 菜单
seeMenu = wx.Menu()
seeMenu.Append(id = 41, item = "状态栏(&S)", kind = wx.ITEM_CHECK).Check(True) # 置为选中状态 menuBar.Append(seeMenu, title = "查看(&V)") #构建 帮助 菜单
helpMenu = wx.Menu() helpMenu.Append(id = wx.ID_HELP, item = "查看帮助(&H)")
helpMenu.Append(id = wx.ID_ABOUT, item = "关于记事本(&A)") menuBar.Append(helpMenu, title = "帮助(&H)")
menuBar.Bind(wx.EVT_MENU, self.eventMenuBar)
self.SetMenuBar(menuBar) # 以下是 被绑定的事件
def eventMenuBar(self, event):
"""传过来的 event 对象, 好像就是 发生事件的那个组件的对象,如果真是这样就灰常好理解了。"""
id = event.GetId()
if id == wx.ID_ABOUT:
msgDialog = wx.MessageDialog(parent = None, message = self.about, caption = '关于"记事本"', style = wx.OK)
msgDialog.ShowModal() elif id == wx.ID_HELP:
url = 'http://www.cnblogs.com/Twobox/'
webbrowser.open(url) elif id == wx.ID_EXIT:
self.Close() elif id == 41:
if event.IsChecked():
self.statusBar.Show(True)
self.SetSize((self.GetSize()[0], (self.GetSize()[1] + 1)))
else:
self.statusBar.Show(False)
self.SetSize((self.GetSize()[0], (self.GetSize()[1] - 1))) def eventTextCtrl(self, event):
print(event.GetString()) def main():
print("GO...")
app = wx.App()
Mywin(None, "无标题 - 记事本")
app.MainLoop()
exit(0) if __name__ == "__main__":
main()
5、继续实现菜单栏中的相关功能
1、实现文件操作的全部功能
包括:新建、打开、保存、另存为、退出。
并且 支持 快捷键,例如 Ctrl + S 一键保存。
支持 打开新文件时 窗口标题改更为:文件名 + 记事本 的格式。
完善 相互间的逻辑关系 例如:
新建一个文本时:需要判断上一个文本是否保存,保存的话可以提示是否保存。
打开一个文本时也需要做上述的判断。
保存时:如果是新建的文本,弹出对话框选择路径保存,如果是打开文本,直接保存即可。
······
2、实现 编辑菜单 下的部分功能
包括:剪切、复制、粘贴、删除、全选。
因为我们编辑的时候一般不是去点击菜单去使用这些功能。
所以 全部实现 快捷键操作 例如:
Ctrl + C:复制
Ctrl + V:粘贴
Ctrl + A:全选
·······
3、阶段 小结一下
大部分功能已经实现了。
这次的更新 实现了文本操作的几乎所有的基本操作,已经能够正常来使用了。
接下来就是 完善:
状态栏显示消息更新
编辑菜单下的查找相关功能
格式中的字体设置相关设置功能
完成这 3 个大方向后,这个 高仿 WIN10 记事本任务就基本结束啦。。。
往后就是 记事本的小细节的优化,即小小滴更新。
该阶段代码时滴代码:
# encoding: utf-8
# acthor: Twobox import wx
import webbrowser class Mywin(wx.Frame):
"""Author: Twobox"""
about = """
作者:Twobox
扣扣:200562421
源码共享博客:http://www.cnblogs.com/Twobox/
"""
def __init__(self, parent, title):
super(Mywin, self).__init__(parent, title = title) self.textBoxIsChange = False self.fileIsOpen = False
self.fileName = "无标题"
self.filePath = "" self.wildcard = "文本文件 (*.txt)|*.txt" self.InitUI() def InitUI(self):
self.initUIMenuBar() # 初始化 菜单栏
self.initUIStatusBar() # 初始化 状态栏
self.initUIMainWindow() # 构建 窗口面板
self.adjustmentWin() # 调整 窗口框体参数 def initUIMainWindow(self):
panel = wx.Panel(self)
vbox = wx.BoxSizer(wx.VERTICAL) self.textBox = wx.TextCtrl(panel, id = -1, style = wx.TE_MULTILINE)
self.textBox.Bind(wx.EVT_TEXT, self.eventTextCtrl)
vbox.Add(self.textBox, proportion = 1, flag = wx.EXPAND|wx.ALL, border = 0) panel.SetSizer(vbox) def initUIStatusBar(self):
# 实例化一个 状态栏 对象
self.statusBar = wx.StatusBar(parent = self, id = -1)
self.statusBar.SetFieldsCount(2)
self.statusBar.SetStatusWidths([-3.9, -1])
self.statusBar.SetStatusText(" 第 1 行 , 第 1 列", 1)
self.SetStatusBar(self.statusBar)
self.statusBar.Show(True) def adjustmentWin(self):
self.SetTitle(self.fileName + " - 记事本")
self.SetSize((400, 600))
self.Center()
self.Show() def initUIMenuBar(self):
menuBar = wx.MenuBar() # 构造 文件 菜单
fileMenu = wx.Menu() newItem = wx.MenuItem(fileMenu, id=wx.ID_NEW, text="新建(&N)\tCtrl+N", kind=wx.ITEM_NORMAL)
openItem = wx.MenuItem(fileMenu, id=wx.ID_OPEN, text="打开(&O)...\tCtrl+O", kind=wx.ITEM_NORMAL)
saveItem = wx.MenuItem(fileMenu, id=wx.ID_SAVE, text="保存(&S)\tCtrl+S", kind=wx.ITEM_NORMAL)
saveasItem = wx.MenuItem(fileMenu, id=wx.ID_SAVEAS, text="另存为(&A)...", kind=wx.ITEM_NORMAL) fileMenu.AppendItem(newItem)
fileMenu.AppendItem(openItem)
fileMenu.AppendItem(saveItem)
fileMenu.AppendItem(saveasItem)
fileMenu.AppendSeparator() exitItem = wx.MenuItem(fileMenu, id=wx.ID_EXIT, text="退出(&X)", kind=wx.ITEM_NORMAL)
fileMenu.AppendItem(exitItem) menuBar.Append(fileMenu, title="文件(&F)") # 构建 编辑 菜单
deitMenu = wx.Menu() deitMenu.Append(id=21, item="撤销(&U)\tCtrl+Z", kind=wx.ITEM_NORMAL)
deitMenu.AppendSeparator()
deitMenu.Append(id=wx.ID_CUT, item="剪切(&T)\tCtrl+X", kind=wx.ITEM_NORMAL)
deitMenu.Append(id=wx.ID_COPY, item="复制(&C)\tCtrl+C", kind=wx.ITEM_NORMAL)
deitMenu.Append(id=wx.ID_PASTE, item="粘贴(&P)\tCtrl+V", kind=wx.ITEM_NORMAL)
deitMenu.Append(id=wx.ID_DELETE, item="删除(&L)\tDel", kind=wx.ITEM_NORMAL)
deitMenu.AppendSeparator()
deitMenu.Append(id=wx.ID_FIND, item="查找(&F)...\tCtrl+F", kind=wx.ITEM_NORMAL)
deitMenu.Append(id=27, item="查找下一个(&N)\tF3")
deitMenu.Append(id=wx.ID_REPLACE, item="替换(&R)...\tCtrl+H")
deitMenu.Append(id=29, item="转到(&G)\tCtrl+G")
deitMenu.AppendSeparator()
deitMenu.Append(id=wx.ID_SELECTALL, item="全选(&A)\tCtrl+A")
deitMenu.Append(id=211, item="时间/日期(&D)\tF5") menuBar.Append(deitMenu, title="编辑(&E)") #构建 格式 菜单
formatMenu = wx.Menu() formatMenu.Append(id = 31, item = "自动换行(&W)", kind = wx.ITEM_CHECK)
formatMenu.Append(id = 32, item = "字体(&F)...") menuBar.Append(formatMenu, title = "格式(&O)") #构建 查看 菜单
seeMenu = wx.Menu()
seeMenu.Append(id = 41, item = "状态栏(&S)", kind = wx.ITEM_CHECK).Check(True) # 置为选中状态 menuBar.Append(seeMenu, title = "查看(&V)") #构建 帮助 菜单
helpMenu = wx.Menu() helpMenu.Append(id = wx.ID_HELP, item = "查看帮助(&H)")
helpMenu.Append(id = wx.ID_ABOUT, item = "关于记事本(&A)") menuBar.Append(helpMenu, title = "帮助(&H)")
menuBar.Bind(wx.EVT_MENU, self.eventMenuBar)
self.SetMenuBar(menuBar) # 以下是 被绑定的事件
def eventMenuBar(self, event):
"""传过来的 event 对象, 好像就是 发生事件的那个组件的对象,如果真是这样就灰常好理解了。
id == 31 目前尚有 BUG。
id == ID_DELETE 未搞 new\打开 文件的时候 未修改标题栏
"""
id = event.GetId()
if id == wx.ID_ABOUT:
msgDialog = wx.MessageDialog(parent = None, message = self.about, caption = '关于"记事本"', style = wx.OK)
msgDialog.ShowModal() elif id == wx.ID_HELP:
url = 'http://www.cnblogs.com/Twobox/'
webbrowser.open(url) elif id == wx.ID_EXIT: self.Close() elif id == 41:
if event.IsChecked():
self.statusBar.Show(True)
self.SetSize((self.GetSize()[0], (self.GetSize()[1] + 1)))
else:
self.statusBar.Show(False)
self.SetSize((self.GetSize()[0], (self.GetSize()[1] - 1))) elif id == 31:
if not event.IsChecked():
self.textBox.SetWindowStyleFlag(style=wx.TE_MULTILINE | wx.HSCROLL)
self.statusBar.Show(True)
self.SetSize((self.GetSize()[0], (self.GetSize()[1] + 1))) else:
self.textBox.SetWindowStyleFlag(style=wx.TE_MULTILINE)
self.statusBar.Show(False)
self.SetSize((self.GetSize()[0], (self.GetSize()[1] - 1))) elif id == wx.ID_SELECTALL: self.textBox.SelectAll() elif id == wx.ID_CUT: self.textBox.Cut() elif id == wx.ID_COPY: self.textBox.Copy() elif id == wx.ID_PASTE: self.textBox.Paste() elif id == wx.ID_NEW:
if self.textBoxIsChange:
if self.textBox.GetValue() == "" and self.fileIsOpen == False:
pass
else:
dlg = wx.MessageDialog(self, "是否将更改保存到 " + self.fileName + "?", "记事本", wx.CANCEL | wx.YES_NO)
answer = dlg.ShowModal()
if answer == wx.ID_OK:
self.saveFile()
elif answer == wx.ID_CANCEL:
return None
else:
pass
self.fileIsOpen = False
self.filePath = ""
self.fileName = "无标题"
self.textBox.SetValue("")
self.textBoxIsChange = False elif id == wx.ID_SAVE: self.saveFile() elif id == wx.ID_OPEN:
if self.textBoxIsChange:
dlg = wx.MessageDialog(self, "是否将更改保存到 " + self.fileName + "?", "记事本", wx.CANCEL | wx.YES_NO)
answer = dlg.ShowModal()
if answer == wx.ID_NO:
self.openFile()
elif answer == wx.ID_CANCEL:
pass
else:
self.saveFile()
else:
self.openFile() elif id == wx.ID_SAVEAS:
self.fileIsOpen = False
if self.saveFile() == False:
self.fileIsOpen = True def eventTextCtrl(self, event):
self.textBoxIsChange = True def saveFile(self):
if not self.fileIsOpen:
wildcard = self.wildcard
dlg = wx.FileDialog(self, "保存", wildcard=wildcard, style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT)
if dlg.ShowModal() == wx.ID_OK:
filePath = dlg.GetPath()
else:
return False
else:
filePath = self.filePath
with open(filePath, "w") as f:
f.write(self.textBox.GetValue())
self.textBoxIsChange = False
return True def openFile(self):
wildcard = self.wildcard
dlg = wx.FileDialog(self, "打开", style=wx.FD_OPEN, wildcard=wildcard)
if dlg.ShowModal() == wx.ID_OK:
filePath = dlg.GetPath()
with open(filePath, "r") as f:
self.textBox.SetValue(f.read())
self.fileIsOpen = True
self.filePath = filePath
self.fileName = f.name
self.textBoxIsChange = False
self.changeTitle()
else:
pass def changeTitle(self):
self.SetTitle(self.fileName + " - 记事本") def main():
print("GO...")
app = wx.App()
Mywin(None, "")
app.MainLoop()
exit(0) if __name__ == "__main__":
main()
6、完成了 字体样式选择 与 字体颜色选择
·到了这里 防WIN10记事本 差不多到这里就要结束了。
还是有几个地方没有去完善了。
比如:
文本查找功能
状态栏中显示 当前光标的 位置
效果图:
本阶段代码:
# encoding: utf-8
# acthor: Twobox import wx
import webbrowser class Mywin(wx.Frame):
"""Author: Twobox"""
about = """
作者:Twobox
扣扣:200562421
源码共享博客:http://www.cnblogs.com/Twobox/
"""
def __init__(self, parent, title):
super(Mywin, self).__init__(parent, title = title) self.textBoxIsChange = False self.fileIsOpen = False
self.fileName = "无标题"
self.filePath = "" self.wildcard = "文本文件 (*.txt)|*.txt" self.InitUI() def InitUI(self):
self.initUIMenuBar() # 初始化 菜单栏
self.initUIStatusBar() # 初始化 状态栏
self.initUIMainWindow() # 构建 窗口面板
self.adjustmentWin() # 调整 窗口框体参数 def initUIMainWindow(self):
panel = wx.Panel(self)
vbox = wx.BoxSizer(wx.VERTICAL) self.textBox = wx.TextCtrl(panel, id = -1, style = wx.TE_MULTILINE)
self.textBox.Bind(wx.EVT_TEXT, self.eventTextCtrl)
vbox.Add(self.textBox, proportion = 1, flag = wx.EXPAND|wx.ALL, border = 0) panel.SetSizer(vbox) def initUIStatusBar(self):
# 实例化一个 状态栏 对象
self.statusBar = wx.StatusBar(parent = self, id = -1)
self.statusBar.SetFieldsCount(2)
self.statusBar.SetStatusWidths([-3.9, -1])
self.statusBar.SetStatusText(" 第 1 行 , 第 1 列", 1)
self.SetStatusBar(self.statusBar)
self.statusBar.Show(True) def adjustmentWin(self):
self.SetTitle(self.fileName + " - 记事本")
self.SetSize((400, 600))
self.Center()
self.Show() def initUIMenuBar(self):
menuBar = wx.MenuBar() # 构造 文件 菜单
fileMenu = wx.Menu() newItem = wx.MenuItem(fileMenu, id=wx.ID_NEW, text="新建(&N)\tCtrl+N", kind=wx.ITEM_NORMAL)
openItem = wx.MenuItem(fileMenu, id=wx.ID_OPEN, text="打开(&O)...\tCtrl+O", kind=wx.ITEM_NORMAL)
saveItem = wx.MenuItem(fileMenu, id=wx.ID_SAVE, text="保存(&S)\tCtrl+S", kind=wx.ITEM_NORMAL)
saveasItem = wx.MenuItem(fileMenu, id=wx.ID_SAVEAS, text="另存为(&A)...", kind=wx.ITEM_NORMAL) fileMenu.AppendItem(newItem)
fileMenu.AppendItem(openItem)
fileMenu.AppendItem(saveItem)
fileMenu.AppendItem(saveasItem)
fileMenu.AppendSeparator() exitItem = wx.MenuItem(fileMenu, id=wx.ID_EXIT, text="退出(&X)", kind=wx.ITEM_NORMAL)
fileMenu.AppendItem(exitItem) menuBar.Append(fileMenu, title="文件(&F)") # 构建 编辑 菜单
deitMenu = wx.Menu() deitMenu.Append(id=21, item="撤销(&U)\tCtrl+Z", kind=wx.ITEM_NORMAL)
deitMenu.AppendSeparator()
deitMenu.Append(id=wx.ID_CUT, item="剪切(&T)\tCtrl+X", kind=wx.ITEM_NORMAL)
deitMenu.Append(id=wx.ID_COPY, item="复制(&C)\tCtrl+C", kind=wx.ITEM_NORMAL)
deitMenu.Append(id=wx.ID_PASTE, item="粘贴(&P)\tCtrl+V", kind=wx.ITEM_NORMAL)
deitMenu.Append(id=wx.ID_DELETE, item="删除(&L)\tDel", kind=wx.ITEM_NORMAL)
deitMenu.AppendSeparator()
deitMenu.Append(id=wx.ID_FIND, item="查找(&F)...\tCtrl+F", kind=wx.ITEM_NORMAL)
deitMenu.Append(id=27, item="查找下一个(&N)\tF3")
deitMenu.Append(id=wx.ID_REPLACE, item="替换(&R)...\tCtrl+H")
deitMenu.Append(id=29, item="转到(&G)\tCtrl+G")
deitMenu.AppendSeparator()
deitMenu.Append(id=wx.ID_SELECTALL, item="全选(&A)\tCtrl+A")
deitMenu.Append(id=211, item="时间/日期(&D)\tF5") menuBar.Append(deitMenu, title="编辑(&E)") #构建 格式 菜单
formatMenu = wx.Menu() formatMenu.Append(id = 31, item = "自动换行(&W)", kind = wx.ITEM_CHECK)
formatMenu.Append(id = wx.ID_SELECT_FONT, item = "字体(&F)...") menuBar.Append(formatMenu, title = "格式(&O)") #构建 查看 菜单
seeMenu = wx.Menu()
seeMenu.Append(id = 41, item = "状态栏(&S)", kind = wx.ITEM_CHECK).Check(True) # 置为选中状态 menuBar.Append(seeMenu, title = "查看(&V)") #构建 帮助 菜单
helpMenu = wx.Menu() helpMenu.Append(id = wx.ID_HELP, item = "查看帮助(&H)")
helpMenu.Append(id = wx.ID_ABOUT, item = "关于记事本(&A)") menuBar.Append(helpMenu, title = "帮助(&H)")
menuBar.Bind(wx.EVT_MENU, self.eventMenuBar)
self.SetMenuBar(menuBar) # 以下是 被绑定的事件
def eventMenuBar(self, event):
"""传过来的 event 对象, 好像就是 发生事件的那个组件的对象,如果真是这样就灰常好理解了。
id == 31 目前尚有 BUG。
id == ID_DELETE 未搞 new\打开 文件的时候 未修改标题栏
"""
id = event.GetId()
if id == wx.ID_ABOUT:
msgDialog = wx.MessageDialog(parent = None, message = self.about, caption = '关于"记事本"', style = wx.OK)
msgDialog.ShowModal() elif id == wx.ID_HELP:
url = 'http://www.cnblogs.com/Twobox/'
webbrowser.open(url) elif id == wx.ID_EXIT: self.Close() elif id == 41:
if event.IsChecked():
self.statusBar.Show(True)
self.SetSize((self.GetSize()[0], (self.GetSize()[1] + 1)))
else:
self.statusBar.Show(False)
self.SetSize((self.GetSize()[0], (self.GetSize()[1] - 1))) elif id == 31:
if not event.IsChecked():
self.textBox.SetWindowStyleFlag(style=wx.TE_MULTILINE | wx.HSCROLL)
self.statusBar.Show(True)
self.SetSize((self.GetSize()[0], (self.GetSize()[1] + 1))) else:
self.textBox.SetWindowStyleFlag(style=wx.TE_MULTILINE)
self.statusBar.Show(False)
self.SetSize((self.GetSize()[0], (self.GetSize()[1] - 1))) elif id == wx.ID_SELECTALL: self.textBox.SelectAll() elif id == wx.ID_CUT: self.textBox.Cut() elif id == wx.ID_COPY: self.textBox.Copy() elif id == wx.ID_PASTE: self.textBox.Paste() elif id == wx.ID_NEW:
if self.textBoxIsChange:
if self.textBox.GetValue() == "" and self.fileIsOpen == False:
pass
else:
dlg = wx.MessageDialog(self, "是否将更改保存到 " + self.fileName + "?", "记事本", wx.CANCEL | wx.YES_NO)
answer = dlg.ShowModal()
if answer == wx.ID_OK:
self.saveFile()
elif answer == wx.ID_CANCEL:
return None
else:
pass
self.fileIsOpen = False
self.filePath = ""
self.fileName = "无标题"
self.textBox.SetValue("")
self.textBoxIsChange = False elif id == wx.ID_SAVE: self.saveFile() elif id == wx.ID_OPEN:
if self.textBoxIsChange:
dlg = wx.MessageDialog(self, "是否将更改保存到 " + self.fileName + "?", "记事本", wx.CANCEL | wx.YES_NO)
answer = dlg.ShowModal()
if answer == wx.ID_NO:
self.openFile()
elif answer == wx.ID_CANCEL:
pass
else:
self.saveFile()
else:
self.openFile() elif id == wx.ID_SAVEAS:
self.fileIsOpen = False
if self.saveFile() == False:
self.fileIsOpen = True elif id == wx.ID_SELECT_FONT:
dlg = wx.FontDialog(self, wx.FontData()) if dlg.ShowModal() == wx.ID_OK:
data = dlg.GetFontData()
Font = data.GetChosenFont()
colour = data.GetColour()
self.textBox.SetFont(Font)
self.textBox.SetForegroundColour(colour) # 设置字体颜色
# self.textBox.SetBackgroundColour(colour) # 设置背景颜色
# self.textBox.SetOwnBackgroundColour(colour) # 不是要清楚 目前看出来的效果是背景颜色 dlg.Destroy() def eventTextCtrl(self, event):
self.textBoxIsChange = True def saveFile(self):
if not self.fileIsOpen:
wildcard = self.wildcard
dlg = wx.FileDialog(self, "保存", wildcard=wildcard, style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT)
if dlg.ShowModal() == wx.ID_OK:
filePath = dlg.GetPath()
else:
return False
else:
filePath = self.filePath
with open(filePath, "w") as f:
f.write(self.textBox.GetValue())
self.textBoxIsChange = False
return True def openFile(self):
wildcard = self.wildcard
dlg = wx.FileDialog(self, "打开", style=wx.FD_OPEN, wildcard=wildcard)
if dlg.ShowModal() == wx.ID_OK:
filePath = dlg.GetPath()
with open(filePath, "r") as f:
self.textBox.SetValue(f.read())
self.fileIsOpen = True
self.filePath = filePath
self.fileName = f.name
self.textBoxIsChange = False
self.changeTitle()
else:
pass def changeTitle(self):
self.SetTitle(self.fileName + " - 记事本") def main():
print("GO...")
app = wx.App()
Mywin(None, "")
app.MainLoop()
exit(0) if __name__ == "__main__":
main()
7、
end...
2017-08-20 22:22:28 -> 2017-08-20 22:22:37 -> 2017-08-23 20:37:37 -> 2017-08-27 17:19:09 -> 2017-08-30 11:19:14