今天,我看到了电脑桌面上的.txt文件, 于是随手打开了,当我看到记事本这个软件时突然陷入了沉思,Python是否可以写一个记事本(当然可以,并十分可以)?于是说干就干,开始写代码吧。
我们先观察一下Windows自带记事本,
可以看到主要构成结构有文本编辑区和菜单栏, Python tkinter的Text与Menu 可以完成文本编辑与菜单栏,
代码:
import sys
import tkinter
from tkinter import filedialog, messagebox
import pyperclip
baocunfangshi = 'UTF-8'
root = tkinter.Tk()
root.geometry('680x650+300+0')
root.title('记事本')
root.resizable(False, False)
gun = tkinter.Scrollbar()
gun.pack(side=tkinter.RIGHT, fill=tkinter.Y)
gun2 = tkinter.Scrollbar(root, orient=tkinter.HORIZONTAL)
gun2.pack(side=tkinter.BOTTOM, fill=tkinter.X)
men = tkinter.Menu(root)
font_big_little = 13
def bao():
text = d.get(0.0, 'end')
path = filedialog.asksaveasfilename(title='保存(请自己写后缀名)')
with open(f'{path}', 'w', encoding=baocunfangshi) as f:
f.write(text)
d.delete(0.0, 'end')
messagebox.showinfo('记事本', '文件保存成功!')
def ex():
csgo = tkinter.Tk()
csgo.geometry('250x250')
tkinter.Message(csgo, text='你想保存此\n 文件吗 \n', width=120, font=('楷体', 15)).pack()
def no():
sys.exit()
def sava():
csgo.withdraw()
bao()
tkinter.Button(csgo, width=20, height=2, text='save', command=sava).pack()
tkinter.Button(csgo, width=20, height=2, text='close', command=no).pack()
csgo.title('记事本')
csgo.resizable(False, False)
csgo.mainloop()
def open_file():
global geshi
p = filedialog.askopenfilename(title='打开')
d.delete(0.0, 'end')
try:
with open(p, 'r', encoding='UTF-8') as f:
d.insert(0.0, f.read())
geshi.config(text='目前文件编码:UTF-8')
except:
with open(p, 'r', encoding='GBK') as f:
d.insert(0.0, f.read())
geshi.config(text='目前文件编码:GBK')
def hei_ye():
d.config(fg='#007EFF', bg='#2B2D30', insertbackground='#FFFBFD', selectforeground='#FF1821',
selectbackground='#FFE53D')
def bai_t():
d.config(bg='#FFAB51', fg='#000000', insertbackground='#FFFBFD', selectbackground='#D575FF',
selectforeground='#2696FF')
def exit_sys():
sys.exit()
def suo():
d.config(state='disabled')
def kai():
d.config(state='normal')
def baocunbianmaxingshi_UTF():
global baocunfangshi
global geshi
baocunfangshi = 'UTF-8'
geshi.config(text=f'目前文件编码:{baocunfangshi}')
def baocunbianmaxingshi_GBK():
global baocunfangshi
global geshi
baocunfangshi = 'GBK'
geshi.config(text=f'目前文件编码:{baocunfangshi}')
def zhan_tie():
d.insert('end', pyperclip.paste())
def Ctrl_c():
text = d.get(0.0, 'end')
pyperclip.copy(text=text)
def backspace():
d.delete(0.0, 'end')
def Ctrl_x():
text = d.get(0.0, 'end')
pyperclip.copy(text=text)
d.delete(0.0, 'end')
men1 = tkinter.Menu(men, tearoff=0)
men1.add_cascade(label='保存', command=bao, font=('楷体', 16))
men1.add_cascade(label='打开', command=open_file, font=('楷体', 15))
men1.add_cascade(label='粘贴', command=zhan_tie, font=('楷体', 14))
men1.add_cascade(label='复制', command=Ctrl_c, font=('楷体', 13))
men1.add_cascade(label='剪切', command=Ctrl_x, font=('楷体', 12))
men1.add_cascade(label='清空记事本', command=backspace, font=('楷体', 11))
men2 = tkinter.Menu(men, tearoff=0)
men2.add_cascade(label='黑夜模式', command=hei_ye, font=('楷体', 16))
men2.add_cascade(label='白天模式', command=bai_t, font=('楷体', 15))
men3 = tkinter.Menu(men, tearoff=0)
men3.add_cascade(label='锁定文本', command=suo, font=('楷体', 16))
men3.add_cascade(label='解锁文本', command=kai, font=('楷体', 15))
men4 = tkinter.Menu(men, tearoff=0)
men4.add_cascade(label='默认文件编码为UTF-8', font=('楷体', 16))
men4.add_cascade(label='改为UTF-8编码保存', command=baocunbianmaxingshi_UTF, font=('楷体', 15))
men4.add_cascade(label='改为GBK编码保存', command=baocunbianmaxingshi_GBK, font=('楷体', 15))
men5 = tkinter.Menu(men, tearoff=0)
def 新宋体():
d.config(font=('新宋体', font_big_little))
def Microsoft_YaHei_UI():
d.config(font=('Microsoft YaHei UI', font_big_little))
def 楷体():
d.config(font=('楷体', font_big_little))
def 宋体():
d.config(font=('宋体', font_big_little))
def 微软雅黑():
d.config(font=('微软雅黑', font_big_little))
men5.add_cascade(label='宋体', command=宋体, font=('宋体', 16))
men5.add_cascade(label='楷体(推荐)', command=楷体, font=('楷体', 15))
men5.add_cascade(label='微软雅黑(大小不兼容)', command=微软雅黑, font=('微软雅黑', 14))
men5.add_cascade(label='Microsoft YaHei UI(大小不兼容)', command=Microsoft_YaHei_UI, font=('Microsoft YaHei UI', 13))
men5.add_cascade(label='新宋体', command=新宋体, font=('新宋体', 12))
men.add_cascade(label='文件', menu=men1)
men.add_cascade(label='记事本颜色设置', menu=men2)
men.add_cascade(label='字体', menu=men5)
men.add_cascade(label='退出', command=exit_sys)
men.add_cascade(label='文本状态', menu=men3)
men.add_cascade(label='保存编码格式', menu=men4)
d = tkinter.Text(root, width=73, height=36, fg='#007EFF', bg='#2B2D30', font=('楷体', font_big_little),
insertbackground='#FFFBFD',
selectforeground='#FF1821', selectbackground='#FFE53D', tabs=4, wrap='none')
d.pack()
d.config(yscrollcommand=gun.set, xscrollcommand=gun2.set)
gun.config(command=d.yview)
gun2.config(command=d.xview)
geshi = tkinter.Label(root, text=f'目前文件编码:{baocunfangshi}', font=('楷体', 12))
geshi.pack()
root.protocol("WM_DELETE_WINDOW", ex)
root.config(menu=men)
root.mainloop()
运行:
记事本可以打开.txt, .xml, .html, .py(并非运行文件)等文件 ,我们这里来用记事本打开记事本的源代码
剩下的一些功能就需要自己发掘了, 好了,这篇文章就到这了,点个赞再走呗,拜拜ヾ(•ω•`)o