python实现记事本功能
程序员文章站
2022-06-16 12:40:54
本文实例为大家分享了python实现记事本功能的具体代码,供大家参考,具体内容如下1. 案例介绍tkinter 是 python下面向 tk 的图形界面接口库,可以方便地进行图形界面设计和交互操作编程...
本文实例为大家分享了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("<<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()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 让爱延续 白色情人节适合送男友手机推荐