【Python黑科技】tkinter库实战制作一个记事本(保姆级图文+实现代码)

  • Post author:
  • Post category:python




欢迎关注

『Python黑科技』 系列

,持续更新中



欢迎关注

『Python黑科技』 系列

,持续更新中



实现效果

在这里插入图片描述




实现思路

具有记事本的基本功能,可以另存为txt文件,编辑,保存,查找,快捷键等

用来练手的

tkinter

小项目实例

弹窗提示

#调用author方法

aboutmenu.add_command(label=“作者”, command=author)

#绑定的弹窗提示函数Python学习萌新

def author():

showinfo(title=“作者”, message=“Python学习萌新”)

热键绑定

textPad.bind(“”, mysave)

textPad.bind(“”, mysave)

这样支持

ctrl+s

进行文件保存,有个问题是没有解决大小写

s

的问题,所以只能把大小写的

s

都绑定一遍····大家如果有好的方法也可以评论区讨论一下




实现代码

# @Time    : 2022/2/9 20:08
# @Author  : 南黎
# @FileName: 记事本.py

from tkinter import *
from tkinter.filedialog import *
from tkinter.messagebox import *
import os

filename = ""


def author():
    showinfo(title="作者", message="Python学习萌新")


def power():
    showinfo(title="版权信息", message="网易云的课堂练习")


def mynew():
    global top, filename, textPad
    top.title("未命名文件")
    filename = None
    textPad.delete(1.0, END)


def myopen():
    global filename
    filename = askopenfilename(defaultextension=".txt")
    if filename == "":
        filename = None
    else:
        top.title("记事本" + os.path.basename(filename))
        textPad.delete(1.0, END)
        f = open(filename, 'r')
        textPad.insert(1.0, f.read())
        f.close()


def mysave():
    global filename
    try:
        f = open(filename, 'w')
        msg = textPad.get(1.0, 'end')
        f.write(msg)
        f.close()
    except:
        mysaveas()


def mysaveas():
    global filename
    f = asksaveasfilename(initialfile="未命名.txt", defaultextension=".txt")
    filename = f
    fh = open(f, 'w')
    msg = textPad.get(1.0, END)
    fh.write(msg)
    fh.close()
    top.title("记事本 " + os.path.basename(f))


def cut():
    global textPad
    textPad.event_generate("<<Cut>>")


def copy():
    global textPad
    textPad.event_generate("<<Copy>>")


def paste():
    global textPad
    textPad.event_generate("<<Paste>>")


def undo():
    global textPad
    textPad.event_generate("<<Undo>>")


def redo():
    global textPad
    textPad.event_generate("<<Redo>>")


def select_all():
    global textPad
    # textPad.event_generate("<<Cut>>")
    textPad.tag_add("sel", "1.0", "end")


def find():
    t = Toplevel(top)
    t.title("查找")
    t.geometry("260x60+200+250")
    t.transient(top)
    Label(t, text="查找:").grid(row=0, column=0, sticky="e")
    v = StringVar()
    e = Entry(t, width=20, textvariable=v)
    e.grid(row=0, column=1, padx=2, pady=2, sticky="we")
    e.focus_set()
    c = IntVar()
    Checkbutton(t, text="不区分大小写", variable=c).grid(row=1, column=1, sticky='e')
    Button(t, text="查找所有", command=lambda: search(v.get(), c.get(),
                                                  textPad, t, e)).grid(row=0, column=2, sticky="e" + "w", padx=2,
                                                                       pady=2)
    def close_search():
        textPad.tag_remove("match", "1.0", END)
        t.destroy()

    t.protocol("WM_DELETE_WINDOW", close_search)


def mypopup(event):
    # global editmenu
    editmenu.tk_popup(event.x_root, event.y_root)


def search(needle, cssnstv, textPad, t, e):
    textPad.tag_remove("match", "1.0", END)
    count = 0
    if needle:
        pos = "1.0"
        while True:
            pos = textPad.search(needle, pos, nocase=cssnstv, stopindex=END)
            if not pos:
                break
            lastpos = pos + str(len(needle))
            textPad.tag_add("match", pos, lastpos)
            count += 1
            pos = lastpos
        textPad.tag_config('match', fg='yellow', bg="green")
        e.focus_set()
        t.title(str(count) + "个被匹配")


top = Tk()
top.title("记事本")
top.geometry("600x400+100+50")

menubar = Menu(top)

# 文件功能
filemenu = Menu(top)
filemenu.add_command(label="新建", accelerator="Ctrl+N", command=mynew)
filemenu.add_command(label="打开", accelerator="Ctrl+O", command=myopen)
filemenu.add_command(label="保存", accelerator="Ctrl+S", command=mysave)
filemenu.add_command(label="另存为", accelerator="Ctrl+shift+s", command=mysaveas)
menubar.add_cascade(label="文件", menu=filemenu)
# 编辑功能
editmenu = Menu(top)
editmenu.add_command(label="撤销", accelerator="Ctrl+Z", command=undo)
editmenu.add_command(label="重做", accelerator="Ctrl+Y", command=redo)
editmenu.add_separator()
editmenu.add_command(label="剪切", accelerator="Ctrl+X", command=cut)
editmenu.add_command(label="复制", accelerator="Ctrl+C", command=copy)
editmenu.add_command(label="粘贴", accelerator="Ctrl+V", command=paste)
editmenu.add_separator()
editmenu.add_command(label="查找", accelerator="Ctrl+F", command=find)
editmenu.add_command(label="全选", accelerator="Ctrl+A", command=select_all)
menubar.add_cascade(label="编辑", menu=editmenu)

# 关于 功能
aboutmenu = Menu(top)
aboutmenu.add_command(label="作者", command=author)
aboutmenu.add_command(label="版权", command=power)
menubar.add_cascade(label="关于", menu=aboutmenu)

top['menu'] = menubar
# shortcutbar = Frame(top, height=25, bg='light sea green')
# shortcutbar.pack(expand=NO, fill=X)
# Inlabe = Label(top, width=2, bg='antique white')
# Inlabe.pack(side=LEFT, anchor='nw', fill=Y)

textPad = Text(top, undo=True)
textPad.pack(expand=YES, fill=BOTH)
scroll = Scrollbar(textPad)
textPad.config(yscrollcommand=scroll.set)
scroll.config(command=textPad.yview)
scroll.pack(side=RIGHT, fill=Y)
# 热键绑定
textPad.bind("<Control-N>", mynew)
textPad.bind("<Control-n>", mynew)
textPad.bind("<Control-O>", myopen)
textPad.bind("<Control-o>", myopen)
textPad.bind("<Control-S>", mysave)
textPad.bind("<Control-s>", mysave)
textPad.bind("<Control-A>", select_all)
textPad.bind("<Control-a>", select_all)
textPad.bind("<Control-F>", find)
textPad.bind("<Control-f>", find)

textPad.bind("<Button-3>", mypopup)
top.mainloop()



总结

大家喜欢的话,给个👍,点个关注!给大家分享更多有趣好玩的Python黑科技!


欢迎关注

『Python黑科技』 系列

,持续更新中



欢迎关注

『Python黑科技』 系列

,持续更新中



【Python黑科技】tkinter库实战7个小项目合集(保姆级图文+实现代码)



【Python黑科技】tkinter库实战制作一个计算器(保姆级图文+实现代码)



【Python黑科技】tkinter库实战制作一个记事本(保姆级图文+实现代码)



【Python黑科技】tkinter库实战用户的注册和登录(保姆级图文+实现代码)



【Python黑科技】tkinter库实战“2048”小游戏(保姆级图文+实现代码)



【Python黑科技】tkinter库实战“俄罗斯方块”小游戏(保姆级图文+实现代码)



【Python黑科技】tkinter库实战“贪吃蛇”小游戏(保姆级图文+实现代码)



【Python黑科技】tkinter库实战“连连看”小游戏(保姆级图文+实现代码)


【Python安装第三方库一行命令永久提高速度】



【使用PyInstaller打包exe】



【免登陆爬虫一键下载知乎文章图片(保姆级图文+实现代码)】



【孤独的程序员和AI机器人朋友聊天解闷(免费接口+保姆级图文+实现代码注释)】



【几行代码绘制gif动图(保姆级图文+实现代码)】



【几行代码实现网课定时循环截屏,保存重要知识点(保姆级图文+实现代码)】



【常用的user_agent 浏览器头爬虫模拟用户(保姆级图文+实现代码)】


【更多内容敬请期待】




版权声明:本文为u011027547原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。