1. 程式人生 > >python tkinter 自制文字編輯器

python tkinter 自制文字編輯器

成品如圖:

在這裡插入圖片描述在這裡插入圖片描述

程式碼

from tkinter import *
from tkinter import filedialog
from tkinter import messagebox
import time


def nodefined():
    pass
#開啟檔案函式
def openfile():
    filename=filedialog.askopenfilename()
    f=open(filename,'r')
    f2=f.read()
    f.close()
    text.insert(INSERT,f2)
#儲存檔案函式
def savefileas():
    filename=filedialog.asksaveasfilename(filetypes=[("TXT",".txt")])
    with open(filename,'a') as f:
        f.write(text.get())

def printf():
    messagebox.showerror("錯誤", "沒有連線印表機")
#退出文件函式
def quit():
    root.destroy()

def about():
    messagebox.showinfo("關於","開發者:Change")


root=Tk()
root.title("自制文字編輯器")

def callback():
    text.edit_undo()


#右鍵選單顯示函式
def popup(event):
    popupmenu.post(event.x_root,event.y_root)

#複製功能函式
def copy():

    global content

    content=text.get(SEL_FIRST,SEL_LAST)
    return content

#剪下函式
def cut():
    global content

    content=text.get(SEL_FIRST,SEL_LAST)
    text.delete(SEL_FIRST,SEL_LAST)

    return content

#貼上功能函式
def paste():

        text.insert(INSERT,content)

#全選文字函式
def select_all():
    pass

#刪除函式
def textdelete():
    text.delete(SEL_FIRST,SEL_LAST)
    
#獲取時間函式
def get_time():
    msg=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
    text.insert(INSERT,msg)

#文字編輯區text
text=Text(root,width=90,height=40,selectforeground="black",undo=True,font=50)
text.pack()

def insretimage():
    filename=filedialog.askopenfilename()

    photo=PhotoImage(file=filename)
    text.image_create(INSERT,image=photo)
    
    mainloop()
    
#頂級選單視窗
    
topmenu=Menu(root)

#建立檔案下拉選單,新增到頂層選單視窗
filemenu=Menu(topmenu,tearoff=False)

#新增下拉內容:

filemenu.add("command",label="開啟",command=openfile)
filemenu.add_command(label="儲存",command=savefileas)
filemenu.add_command(label="另存為",command=savefileas)
filemenu.add_separator()
filemenu.add_command(label="頁面設定",command=nodefined)
filemenu.add_command(label="列印",command=printf)
filemenu.add_separator()
filemenu.add_command(label="退出",command=quit)
topmenu.add_cascade(label="檔案", menu=filemenu)

#建立編輯選單
editmenu=Menu(topmenu,tearoff=False)

#建立編輯下拉內容
editmenu.add_command(label="插入圖片",command=insretimage)
editmenu.add_separator()
editmenu.add_command(label="撤銷",command=callback)
editmenu.add("command",label="剪下",command=cut)
editmenu.add_command(label="複製",command=copy)
editmenu.add_command(label="貼上",command=paste)

editmenu.add_separator()
editmenu.add_command(label="查詢",command=nodefined)
editmenu.add_command(label="替換",command=nodefined)
editmenu.add_command(label="轉到",command=nodefined)
editmenu.add_separator()
editmenu.add_command(label="全選",command=select_all)
editmenu.add_command(label="時間/日期",command=get_time)

topmenu.add_cascade(label="編輯",menu=editmenu)

#建立格式選單
formatmenu=Menu(topmenu,tearoff=False)
formatmenu.add_checkbutton(label="自動換行",command=nodefined)
formatmenu.add_command(label="字型",command=nodefined)
topmenu.add_cascade(label="格式",menu=formatmenu)

#檢視選單
viewmenu=Menu(topmenu,tearoff=False)

viewmenu.add_command(label="檢視狀態列",command=callback)
topmenu.add_cascade(label="檢視",menu=viewmenu)

#建立幫助選單
helpmenu=Menu(topmenu,tearoff=False)
helpmenu.add_command(label="檢視幫助",command=callback)
helpmenu.add_separator()
helpmenu.add_command(label="關於筆記本",command=about)

topmenu.add_cascade(label="幫助",menu=helpmenu)

#右鍵選單欄
popupmenu=Menu(root,tearoff=False)
popupmenu.add_command(label="儲存",command=savefileas)
popupmenu.add_command(label="另存為",command=savefileas)
popupmenu.add_separator()
popupmenu.add_command(label="全選",command=select_all)
popupmenu.add_command(label="插入",command=insretimage)
popupmenu.add_command(label="撤回",command=callback)
popupmenu.add_separator()
popupmenu.add("command",label="剪下",command=cut)
popupmenu.add_command(label="複製",command=copy)
popupmenu.add_command(label="貼上",command=paste)
popupmenu.add("command",label="刪除",command=textdelete)
text.bind("<Button-3>",popup)

#跟視窗顯示選單欄
root.config(menu=topmenu)


mainloop()

程式碼很爛,想到那寫到那,有部分函式未編寫。 主要功能都具有了,諸如插入文字,圖片,儲存,複製,刪除等等內容。 主要做點東西激勵一下自己。