tkinter制作记事本

import tkinter.filedialog #这个模块必须放在from tkinter import *前边,不然会出现NameError
import tkinter.messagebox
from tkinter import *
import os
root = Tk()
#root.config(bg = 'white')
root.title('记事本')
root.geometry('600x400+150+150')
root.iconbitmap(r'C:\Users\Administrator\Desktop\tu.ico')
menubar = Menu(root)
#文件
def my_create():
    root.title('未命名文件')
    root.geometry('600x400+100+100')
    text.delete(1.0,END)
    #增加询问
def my_open():
    global filename
    filename =filedialog.askopenfilename(defaultextension = '.txt',filetypes = [('Text','.txt'),('DOC','.doc')])
    if filename == '':
        filename = None
    else:
        root.title('FileName:'+os.path.basename(filename))#后边这句要查查用法
        text.delete(1.0,END)
        f = open(filename,'r')
        text.insert(1.0,f.read())
        f.close()
def my_save():
    global filename
    f = open(filename,'w')
    w = text.get(1.0,END)
    f.write(w)
    f.close()
def my_save_1():
    try:
        filename = filedialog.asksaveasfilename(initialfile= '自己定义一个名字吧', \
                                                defaultextension = '.txt',\
                                                filetypes = [('Text','.txt'),('DOC','.doc')])
        fh = open(filename,'w')
        msg = text.get(1.0,END)
        fh.write(msg)
        fh.close()
        root.title('FileName:'+os.path.basename(filename))
    except FileNotFoundError:
        pass
def my_set():
    pass
def my_print():
    pass
#编辑
try:
    def my_undo():
        text.edit_undo()  
except:
    pass  #怎么忽略所有错误,忘了
def my_cut():
    text.event_generate('<>')
def my_copy():
    text.event_generate('<>')
def my_paste():
    text.event_generate('<>')
def my_del():
    text.delete(1.0,END) #应该是选中再删除,成全部删除了
def found():
        global entry
        e = entry.get()
        r = text.get(1.0,END)
        result = r.find(e)
        tkinter.messagebox.showinfo('提示信息','这个功能还没开发完善')
def my_find():
    global entry
    top = Toplevel(root)
    top.geometry('300x100+200+200')
    top.resizable(0,0)
    top.title('查找')
    label = Label(top,text = '查找内容: ')
    label.grid(row = 1,column = 1)
    entry = Entry(top,relief = 'sunken',width = 25)
    entry.grid(row = 1,column = 2,pady = 5)
    button = Button(top,text = '查找',command = found)#行不通
    button.grid(row = 1,column = 5,padx = 10)            
def my_next():
    pass
def my_tihuan():
    pass
def my_all():
    text.tag_add('sel',1.0,END)
def my_data():
    pass
#格式
def my_enter():
    pass
def my_formate():
    pass
#状态
def my_zt():
    pass
#帮助
def my_help():
    tkinter.messagebox.showinfo("帮助","没有哦!~")
def my_about():
    tkinter.messagebox.showinfo("关于","由nkd50000辛苦制作未完成")
#右键事件
def selectAll():
     text.tag_add('sel',1.0,END)
def copy():
     text.event_generate('<>')
def paste():
     text.event_generate('<>')
def cut():
     text.event_generate('<>')
#文件
filemenu = Menu(menubar,tearoff = False)
filemenu.add_command(label = '新建(N)',accelerator="Ctrl + N",command = my_create)#快捷方式不能用,是不是需要绑定键盘事件啊?
filemenu.add_command(label = '打开(O)',accelerator = 'Ctrl+O',command = my_open)
filemenu.add_command(label = '保存(S)',accelerator = 'Ctrl+S',command = my_save)
filemenu.add_command(label = '另存为(A)',command = my_save_1)
filemenu.add_separator()
filemenu.add_command(label = '页面设置(U)',command = my_set)
filemenu.add_command(label = '打印(P)',accelerator = 'Ctrl+P',command = my_print)
filemenu.add_command(label = '退出(Q)',accelerator = 'Ctrl+Q',command = root.quit)
menubar.add_cascade(label = '文件(F)',menu = filemenu)
#编辑
editmenu = Menu(menubar,tearoff = False)
editmenu.add_command(label = '撤销(U)',accelerator = 'Ctrl+Z',command = my_undo)
editmenu.add_separator()
editmenu.add_command(label = '剪切(T)',accelerator = 'Ctrl+X',command = my_cut)
editmenu.add_command(label = '复制(C)',accelerator = 'Ctrl+C',command = my_copy)
editmenu.add_command(label = '粘贴(P)',accelerator = 'Ctrl+V',command = my_paste)
editmenu.add_command(label = '删除(D)',accelerator = 'Del',command = my_del)
editmenu.add_separator()
editmenu.add_command(label = '查找(F)',accelerator = 'Ctrl+F',command = my_find)
editmenu.add_command(label = '查找下一个(N)',accelerator = 'F3',command = my_next)
editmenu.add_command(label = '替换(R)',accelerator = 'Ctrl+H',command = my_tihuan)
editmenu.add_separator()
editmenu.add_command(label = '全选(A)',accelerator = 'Ctrl+A',command = my_all)
editmenu.add_command(label = '时间/日期(D)',accelerator = 'F5',command = my_data)
menubar.add_cascade(label = '编辑(E)',menu = editmenu)
#格式
fmenu = Menu(menubar,tearoff = False)
fmenu.add_command(label = '自动换行(W)',command = my_enter)
fmenu.add_command(label = '字体(F)',command = my_formate)
menubar.add_cascade(label = '格式(O)',menu = fmenu)
#查看
v = IntVar()
smenu = Menu(menubar,tearoff = False)
smenu.add_checkbutton(label = '状态栏(S)',command = my_zt,variable = v)#用单选的不好,汗
menubar.add_cascade(label = '查看(V)',menu = smenu)
#帮助
hmenu = Menu(menubar,tearoff = False) 
hmenu.add_command(label = '查看帮助(H)',command = my_help)
hmenu.add_command(label = '关于记事本(A)',command = my_about)
menubar.add_cascade(label = '帮助(H)',menu = hmenu)
#滚动条
sc_y = Scrollbar(root)
sc_y.pack(side = RIGHT,fill = Y)
text = Text(root,bg = 'white',width = 600,height = 400,\
            yscrollcommand = sc_y.set,\
            undo = True)
text.pack()
sc_y.config(command = text.yview)
#右键事件绑定
menu = Menu(text,tearoff = False)
menu.add_command(label = '全选',command = selectAll)
menu.add_command(label = '复制',command = copy)
menu.add_command(label = '剪切',command = cut)
menu.add_command(label = '粘贴',command = paste)
def popup(event):
     menu.post(event.x_root,event.y_root)
text.bind('',popup)
root.config(menu = menubar)
root.mainloop()

下一步封装一下!~

你可能感兴趣的:(Tkinter学习)