【Python黑科技】tkinter库实战制作一个记事本(保姆级图文+实现代码)
目錄
- 實現效果
- 實現思路
- 實現代碼
- 總結
歡迎關注 『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: 記事本.pyfrom tkinter import * from tkinter.filedialog import * from tkinter.messagebox import * import osfilename = ""def author():showinfo(title="作者", message="Python學習萌新")def power():showinfo(title="版權信息", message="網易云的課堂練習")def mynew():global top, filename, textPadtop.title("未命名文件")filename = NonetextPad.delete(1.0, END)def myopen():global filenamefilename = askopenfilename(defaultextension=".txt")if filename == "":filename = Noneelse: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 filenametry:f = open(filename, 'w')msg = textPad.get(1.0, 'end')f.write(msg)f.close()except:mysaveas()def mysaveas():global filenamef = asksaveasfilename(initialfile="未命名.txt", defaultextension=".txt")filename = ffh = open(f, 'w')msg = textPad.get(1.0, END)fh.write(msg)fh.close()top.title("記事本 " + os.path.basename(f))def cut():global textPadtextPad.event_generate("<<Cut>>")def copy():global textPadtextPad.event_generate("<<Copy>>")def paste():global textPadtextPad.event_generate("<<Paste>>")def undo():global textPadtextPad.event_generate("<<Undo>>")def redo():global textPadtextPad.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 editmenueditmenu.tk_popup(event.x_root, event.y_root)def search(needle, cssnstv, textPad, t, e):textPad.tag_remove("match", "1.0", END)count = 0if needle:pos = "1.0"while True:pos = textPad.search(needle, pos, nocase=cssnstv, stopindex=END)if not pos:breaklastpos = pos + str(len(needle))textPad.tag_add("match", pos, lastpos)count += 1pos = lastpostextPad.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 瀏覽器頭爬蟲模擬用戶(保姆級圖文+實現代碼)】
【更多內容敬請期待】
總結
以上是生活随笔為你收集整理的【Python黑科技】tkinter库实战制作一个记事本(保姆级图文+实现代码)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java datasource使用_Da
- 下一篇: 【LeetCode】871. Minim