记事本的开发---Tkinter的使用

本文介绍了一个使用Python和Tkinter库开发的简易记事本应用程序,该应用包含了文件操作如新建、打开、保存等功能,并实现了基本的编辑功能如撤销、重做等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#coding:utf-8
__author__ = 'xshengjing'

from Tkinter import *
from tkMessageBox import *
from tkFileDialog import *
import os
file_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 bar
filemenu = 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 menus
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, 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()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值