記事本的開發---Tkinter的使用

來源:互聯網
上載者:User
#coding:utf-8__author__ = 'xshengjing'from Tkinter import *from tkMessageBox import *from tkFileDialog import *import osfile_name = ""def search():    top_search = Toplevel(root)    #top_search.geometry("300*30+20+250")    label_01 = Label(top_search,text="Find")    label_01.grid(row=0,column=0,padx=5)    entry_01 = Entry(top_search,width=20)    entry_01.grid(row=0,column=1,padx=5)    button_01 = Button(top_search,text="尋找")    button_01.grid(row=0,column=2)def anthor():    showinfo("作者資訊","本軟體由小徐完成")def about():    showinfo("著作權資訊.Copyright","本軟體著作權歸小徐所有")def open_file():    global file_name    file_name = askopenfilename(defaultextension=".txt")    if file_name == "":        file_name = None    else:        root.title("FileName:" + os.path.basename(file_name))        text_pad.delete(1.0,END)        f = open(file_name,"r")        text_pad.insert(1.0,f.read())        f.close()def cut():    text_pad.event_generate("<<Cut>>")def copy():    text_pad.event_generate("<<Copy>>")def paste():    text_pad.event_generate("<<Paste>>")def redo():    text_pad.event_generate("<<Redo>>")def undo():    text_pad.event_generate("<<Undo>>")def select_all():    text_pad.tag_add('sel','1.0',END)def new():    global file_name    root.title("未命名檔案")    file_name = None    text_pad.delete(1.0,END)def save():    global file_name    try:        f = open(file_name,"w")        msg = text_pad.get(1.0,END)        f.write(msg)        f.close()    except:        save_as()def save_as():    f = asksaveasfilename(initialfil="未命名.txt",defaultextension=".txt")    global file_name    file_name = f    fh = open(f,"w")    msg = text_pad.get(1.0,END)    fh.write(msg)    fh.close()    root.title("FileName:" + os.path.basename(f))root = Tk()root.title("小徐記事本")#root.geometry("500*500+100+100")menubar = Menu(root)# create a pulldown menu, and add it to the menu barfilemenu = Menu(menubar, tearoff=0)filemenu.add_command(label="建立", accelerator="Ctrl + N",command=new)filemenu.add_command(label="開啟", accelerator="Ctrl + N",command=open_file)filemenu.add_command(label="儲存", accelerator="Ctrl + S",command=save)filemenu.add_command(label="另存新檔", accelerator="Ctrl + Shift + S",command=save_as)filemenu.add_command(label="退出", command=root.quit)menubar.add_cascade(label="檔案", menu=filemenu)# create more pulldown menuseditmenu = Menu(menubar)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=search)editmenu.add_command(label="全選", accelerator="Ctrl + A",command=select_all)menubar.add_cascade(label="編輯", menu=editmenu)aboutmenu = Menu(menubar, tearoff=0)aboutmenu.add_command(label="作者",command=anthor)aboutmenu.add_command(label="著作權",command=about)menubar.add_cascade(label="關於", menu=aboutmenu)root.config(menu=menubar)toolbar = Frame(root,height=25,bg="light sea green")short_button = Button(toolbar,text="開啟",command= open_file)short_button.pack(side=LEFT,padx=5,pady=5)short_button = Button(toolbar,text="儲存")short_button.pack(side=LEFT)toolbar.pack(expand=NO,fill=X)status = Label(root,text="Ln20",bd=1,relief=SUNKEN,anchor=W)status.pack(side=BOTTOM,fill=X)#顯示行號lnlabel = Label(root,width=2,bg='antique white')lnlabel.pack(side=LEFT,fill=Y)text_pad = Text(root,undo=True)text_pad.pack(expand=YES,fill=BOTH)scroll = Scrollbar(text_pad)text_pad.config(yscrollcommand=scroll.set)scroll.config(command=text_pad.yview)scroll.pack(side=RIGHT,fill=Y)root.mainloop()
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.