In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how to use python to achieve notepad function". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. Case introduction
Tkinter is a tk-oriented graphical interface library under Python, which can easily carry out graphical interface design and interactive programming. Tkinter has the advantages of easy to use and good combination with Python. Tkinter is integrated by default under Python 3.x and does not require additional installation operations; the deficiency is the lack of appropriate visual interface design tools and the need for code to complete window design and element layout. The Python version of this example is 3.8. if you want to use tkinter under python 2.x, please install it first. It should be noted that tkinter may be used slightly differently under different Python versions, and the Python3.x version is recommended. This example is intermediate in difficulty and is suitable for users with basic knowledge of Python and Tkinter component programming.
two。 Example effect
3. Example source code from tkinter import * from tkinter.filedialog import * from tkinter.messagebox import * import os filename = "" def author (): showinfo (title= "author", message= "Python") def power (): showinfo (title= "copyright Information", message= "Class activity") def mynew (): global top, filename, textPad top.title ("unnamed File") filename = None textPad.delete END) def myopen (): global filename filename = askopenfilename (defaultextension= ".txt") if filename = "": filename = None else: top.title ("notepad" + 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= "unnamed .txt" Defaultextension= ".txt") filename = f fh = open (f,'w') msg = textPad.get (1.0) END) fh.write (msg) fh.close () top.title ("notepad" + 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") Def find (): t = Toplevel (top) t.title ("find") t.geometry ("260x60+200+250") t.transient (top) Label (t, text= "find:") .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= "case insensitive", variable=c) .grid (row=1, column=1, sticky='e') Button (t, text= "find all", 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) + "matched") top = Tk () top.title ("notepad") top.geometry ("600x400+100+50") menubar = Menu (top) # File function filemenu = Menu (top) filemenu.add_command (label= "New", accelerator= "Ctrl+N", command=mynew) filemenu.add_command (label= "Open", accelerator= "Ctrl+O", command=myopen) filemenu.add_command (label= "Save" Accelerator= "Ctrl+S", command=mysave) filemenu.add_command (label= "Save as", accelerator= "Ctrl+shift+s", command=mysaveas) menubar.add_cascade (label= "File", menu=filemenu) # Editing function editmenu = Menu (top) editmenu.add_command (label= "undo", accelerator= "Ctrl+Z", command=undo) editmenu.add_command (label= "redo", accelerator= "Ctrl+Y", command=redo) editmenu.add_separator () editmenu.add_command (label= "cut", accelerator= "Ctrl+X", command=cut) editmenu.add_command (label= "copy" Accelerator= "Ctrl+C", command=copy) editmenu.add_command (label= "paste", accelerator= "Ctrl+V", command=paste) editmenu.add_separator () editmenu.add_command (label= "find", accelerator= "Ctrl+F", command=find) editmenu.add_command (label= "Select", accelerator= "Ctrl+A", command=select_all) menubar.add_cascade (label= "Edit", menu=editmenu) # about features aboutmenu = Menu (top) aboutmenu.add_command (label= "author", command=author) aboutmenu.add_command (label= "copyright" Command=power) menubar.add_cascade (label= "about", 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) Fill=Y) # Hot key binding 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 () "how to implement notepad with python" ends here. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.