以下是程序,欢迎点赞收藏
from tkinter import*
from tkinter.filedialog import asksaveasfilename
from tkinter.filedialog import askopenfilename

def newfile():
    text.delete("1.0",END)
    root.title("Untitled")

def openfile():
    global filename
    filename = askopenfilename()
    if filename == "":
        return
    with open (filename,"r") as fileObj:
        content = fileobj.read()
    text.delete("1.0",END)
    text.insert(END,content)
    root.titile(filename)
    
def saveAsFile():
    global filename
    textContent = text.get("1.0",END)
    filename = asksaveasfilename()
    if filename == "":
        return
    with open(filename,"w")as output:
        output.write(textContent)
        root.title(filename)

filename = "Untitled"
root = Tk()
root.title("text editor")
root.geometry("300x180")

menubar = Menu(root)
filemenu = Menu(menubar,tearoff=False)
menubar.add_cascade(label="File",menu=filemenu)
filemenu.add_command(label="Save as",command=saveAsFile)
filemenu.add_command(label="Open file",command=openfile)
filemenu.add_command(label="New file",command=newfile)
filemenu.add_separator()
filemenu.add_command(label="Exit Editor",command=root.destroy)
root.config(menu=menubar)

text =Text(root,undo=True)
text.pack(fill=BOTH,expand=True)
root.mainloop()

本站作者已申明原创,禁止转载!

文章内容属作者个人观点,不代表本站立场,如有侵权立删。

   口袋儿题库-青少儿编程自测题库