python简单GUI(模拟记事本)

    技术2022-07-27  74

    import tkinter.filedialog import tkinter.messagebox import tkinter.scrolledtext import tkinter.simpledialog root = tkinter.Tk() root.title("Notepad") root['width'] = 200 root['height'] = 300 filename="E://无标题.txt" textChanged=tkinter.IntVar(root,value=0) textContent=tkinter.scrolledtext.ScrolledText(root,wrap=tkinter.WORD) textContent.pack(fill=tkinter.BOTH,expand=tkinter.YES) def open_file(): global filename global textChanged if textChanged.get(): choose=tkinter.messagebox.askyesnocancel(title='无标题', message="Do you want to save?") if choose==tkinter.YES: save_as() filename=tkinter.filedialog.askopenfilename(title="Open File",filetypes=[("Text","*.txt")]) if filename: textContent.delete(0.0, tkinter.END) with open(filename,'r') as fp: textContent.insert(tkinter.INSERT, ''.join(fp.read())) root.title(filename) textChanged.set(0) def save(): global filename if filename=="E://无标题.txt": save_as() else: with open(filename,'w') as fp: fp.write(textContent.get(0.0,tkinter.END)) textChanged.set(0) def save_as(): global filename newFilename=tkinter.filedialog.asksaveasfilename(title='Save as',initialdir='E://',initialfile='新建文本文档.txt') if newFilename: with open(newFilename,'w') as fp: fp.write(textContent.get(0.0,tkinter.END)) filename=newFilename root.title(filename) textChanged.set(0) def close(): if textChanged.get(): save_as() root.quit() def undo(): textContent['undo']=True try: textContent.edit_undo() except Exception as ex: pass def copy(): textContent.clipboard_clear() textContent.clipboard_append(textContent.selection_get()) def cut(): copy() textContent.delete(tkinter.SEL_FIRST,tkinter.SEL_LAST) def paste(): try: textContent.delete(tkinter.SEL_FIRST, tkinter.SEL_LAST) textContent.insert(tkinter.SEL_FIRST, textContent.clipboard_get()) return except Exception as e: pass textContent.insert(tkinter.INSERT, textContent.clipboard_get()) def search(): text=tkinter.simpledialog.askstring(title='Search', prompt="Text:") flag=textContent.search(text,0.0,tkinter.END) if flag: tkinter.messagebox.showinfo(title="Search Status",message='Success') else: tkinter.messagebox.showerror(title="Search Status", message='Fail') def readme(): tkinter.messagebox.showinfo(title='Readme',message='Author:Bitter elf') def keyPress(event): textChanged.set(1) menu=tkinter.Menu(root) menuFile=tkinter.Menu(menu,tearoff=0) menuEdit=tkinter.Menu(menu,tearoff=0) menuHelp=tkinter.Menu(menu,tearoff=0) #子菜单添加项目 menuFile.add_command(label='Open',command=open_file) menuFile.add_command(label='Save',command=save) menuFile.add_command(label='Save as',command=save_as) menuFile.add_separator() menuFile.add_command(label='Close',command=close) menuEdit.add_command(label='Undo',command=undo) menuEdit.add_separator() menuEdit.add_command(label='Copy',command=copy) menuEdit.add_command(label='Cut',command=cut) menuEdit.add_command(label='Paste',command=paste) menuEdit.add_separator() menuEdit.add_command(label='Search',command=search) menuHelp.add_command(label='Readme',command=readme) menu.add_cascade(label='File',menu=menuFile) menu.add_cascade(label='Edit',menu=menuEdit) menu.add_cascade(label='Help',menu=menuHelp) textContent.bind('<KeyPress>',keyPress) root.config(menu=menu) root.mainloop()

    Processed: 0.009, SQL: 9