如何用python实现记事本功能
发表于:2024-10-27 作者:千家信息网编辑
千家信息网最后更新 2024年10月27日,本篇内容介绍了"如何用python实现记事本功能"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!1.
千家信息网最后更新 2024年10月27日如何用python实现记事本功能
本篇内容介绍了"如何用python实现记事本功能"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
1. 案例介绍
tkinter 是 Python下面向 tk 的图形界面接口库,可以方便地进行图形界面设计和交互操作编程。tkinter 的优点是简单易用、与 Python 的结合度好。tkinter 在 Python 3.x 下默认集成,不需要额外的安装操作;不足之处为缺少合适的可视化界面设计工具,需要通过代码来完成窗口设计和元素布局。本例采用的 Python 版本为 3.8,如果想在 python 2.x下使用 tkinter,请先进行安装。需要注意的是,不同 Python 版本下的 tkinter 使用方式可能略有不同,建议采用 Python3.x 版本。本例难度为中级,适合具有 Python 基础和 Tkinter 组件编程知识的用户学习。
2. 示例效果
3. 示例源码
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("<>") def copy(): global textPad textPad.event_generate("< >") def paste(): global textPad textPad.event_generate("< >") def undo(): global textPad textPad.event_generate("< >") def redo(): global textPad textPad.event_generate("< >") def select_all(): global textPad # textPad.event_generate("< >") 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(" ", mynew)textPad.bind(" ", mynew)textPad.bind(" ", myopen)textPad.bind(" ", myopen)textPad.bind(" ", mysave)textPad.bind(" ", mysave)textPad.bind(" ", select_all)textPad.bind(" ", select_all)textPad.bind(" ", find)textPad.bind(" ", find) textPad.bind(" ", mypopup)top.mainloop()
"如何用python实现记事本功能"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!
功能
记事本
文件
版本
界面
知识
设计
不同
作者
内容
图形
更多
案例
版权
界面设计
示例
学习
编程
合适
实用
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
psn账号管理服务器
衡阳提升网络安全技术容易且快速
软件开发人员考核办法
数据库点击次数用什么类型字符
销售软件开发需求文档
魔兽世界90数据库在哪里
签章服务器管理工具在哪儿
现在做服务器还有前途吗
小米手机远程服务器制作
潮州数据链软件开发
前端数据怎么存放数据库
校园配餐公司网络安全
pve 数据库
数据库信息系统sql代码
大学互联网科技竞赛
网络安全产品效能难吗
眼镜行业软件开发有什么用
sybase数据库学习
网络安全项目建设目标
计算机网络技术的题app
db2数据库表删除不了
kdb数据库性能
戴尔服务器和西数硬盘兼容吗
mc怎么创建服务器
矩阵服务器和磁盘阵列
山东电信网络安全
佛山专业的软件开发公司
浙江尚易网络技术有限公司
我的世界九龙谷服务器怎么进
山东应用软件开发服务保障