python 寫一個(gè)簡(jiǎn)單的圖形化記事本

Tkinter 桌面編程

此程序使用 IDE 工具 pycharm 完成,首先創(chuàng)建一個(gè)項(xiàng)目 project_practice,然后創(chuàng)建一個(gè) python 文件 note.py

#!/usr/bin/env python
# _*_ coding:utf-8 _*_
__author__ = 'junxi'


from tkinter import *
from tkinter.messagebox import *
from tkinter.filedialog import *
from tkinter.simpledialog import *
import os


def author():
    showinfo('作者信息', '本軟件由君惜完成')

def about():
    showinfo('版權(quán)信息.Copyright', '本軟件版權(quán)為君惜所有')

def openfile():
    global filename
    filename = askopenfilename(defaultextension='.txt')
    if filename == '':
        filename = None
    else:
        root.title('FileName: ' + os.path.basename(filename))
        textPad.delete(1.0, END)
        f = open(filename,'r')
        textPad.insert(1.0, f.read())
        f.close()

def new():
    global filename
    root.title('未命名文件')
    textPad.delete(1.0, END)

def save():
    global filename
    try:
        f = open(filename, 'w')
        msg = textPad.get(1.0, END)
        f.write(msg)
        f.close()
    except:
        save_as()

def save_as():
    f = asksaveasfilename(initialfile='未命名.txt', defaultextension='.txt')
    global filename
    filename = f
    fh = open(f, 'w')
    msg = textPad.get(1.0, END)
    fh.write(msg)
    fh.close()
    root.title('FileName: ' + os.path.basename(f))

def cut():
    textPad.event_generate('<<Cut>>')

def copy():
    textPad.event_generate('<<Copy>>')

def paste():
    textPad.event_generate('<<Paste>>')

def redo():     # 重做
    textPad.event_generate('<<Redo>>')

def undo():     # 撤銷
    textPad.event_generate('<<Undo>>')

def select_all():
    textPad.tag_add('sel', '1.0', END)


def search():
    topsearch = Toplevel(root)
    topsearch.geometry('300x30+200+250')
    label1 = Label(topsearch, text='Find')
    label1.grid(row=0, column=0, padx=5)
    entry1 = Entry(topsearch, width=20)
    entry1.grid(row=0, column=1, padx=5)
    button1 = Button(topsearch, text='查找')
    button1.grid(row=0, column=2, padx=10)



root = Tk()
root.title('junxi note')
root.geometry("800x500+100+100")

# 創(chuàng)建菜單
menubar = Menu(root)
root.config(menu = menubar)

# 文件菜單
filemenu = Menu(menubar)
filemenu.add_command(label='新建', accelerator='Ctrl + N', command=new)
filemenu.add_command(label='打開', accelerator='Ctrl + O', command=openfile)
filemenu.add_command(label='保存', accelerator='Ctrl + S', command=save)
filemenu.add_command(label='另存為', accelerator='Ctrl + Shift + S', command=save_as)
menubar.add_cascade(label= '文件', menu = filemenu)       # 關(guān)聯(lián)

# 編輯菜單
editmenu = 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='復(fù)制', 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)

# 關(guān)于
aboutmenu = Menu(menubar)
aboutmenu.add_command(label='作者', command=author)
aboutmenu.add_command(label='版權(quán)', command=about)
menubar.add_cascade(label='關(guān)于', menu = aboutmenu)

# 工具欄
toolbar = Frame(root, height=25, bg='Light sea green')

# 按鈕
shortButton = Button(toolbar, text='打開', command=openfile)
shortButton.pack(side=LEFT, padx=5, pady=5)
shortButton = Button(toolbar, text='保存', command=save)
shortButton.pack(side=LEFT)
toolbar.pack(expand=NO, fill=X)         # 顯示

# 狀態(tài)欄
status = Label(root, text='Ln20', bd=1, relief=SUNKEN, anchor=W)
status.pack(side=BOTTOM, fill=X)

# 行號(hào)和文本編輯
linelabel = Label(root, width=2, bg='antique white')
linelabel.pack(side=LEFT, fill=Y)
textPad = Text(root, undo=True)
textPad.pack(expand=YES, fill=BOTH)

# 右邊滾動(dòng)下拉條
scroll = Scrollbar(textPad)
textPad.config(yscrollcommand=scroll.set)
scroll.config(command=textPad.yview)
scroll.pack(side=RIGHT, fill=Y)


root.mainloop()


查看效果

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容