python寫一個簡單的圖形化記事本

Tkinter 桌面編程

此程序使用 IDE 工具 pycharm 完成,首先建立一個項目 project_practice,而後建立一個 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('版權信息.Copyright', '本軟件版權爲君惜全部')

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")

# 建立菜單
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)       # 關聯

# 編輯菜單
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='複製', 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)
aboutmenu.add_command(label='做者', command=author)
aboutmenu.add_command(label='版權', command=about)
menubar.add_cascade(label='關於', 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)         # 顯示

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

# 行號和文本編輯
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)

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


查看效果

相關文章
相關標籤/搜索