使用tkinter做记事本,使用textpad相关功能时报错
from tkinter import *
from tkinter.messagebox import *
from tkinter.filedialog import *
import os
filename = ""
textPad = ""
root = Tk()
root.title('Jerry.textbook')
root.geometry('500x300+100+100')
def author():
showinfo('作者信息:','xxx')
def about():
showinfo('版本:','记事本1.0版')
def openfile():
global filename
filename = askopenfilename(defaultextension = '.txt')
if filename == '':
filename = None
else:
root.title('OpenFile'+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('未命名文件')
filename = None
textPad.delete(1.0,END)
def saveas():
global filename
f = asksaveasfilename(initialfile='未命名文件',defaultextension='.txt')
filename = f
fh = open(f,'w')
message = textPad.get(1.0,END)
fh.write(message)
fh.close()
root.title('FileName:'+os.path.basename(f))
def save():
global filename
try:
f = open(filename,'w')
message = textPad.get(1.0,END)
f.write(message)
f.close()
except:
saveas()
def cut():
global textPad
textPad.event_generate('<<Cut>>')
def copy():
global textPad
textPad.event_generate('<<Copy>>')
def paste():
global textPad
textPad.event_generate('<<Paste>>')
def undo():
global textPad
textPad.event_generate('<<Undo>>')
def redo():
global textPad
textPad.event_generate('<<Redo>>')
def all():
global textPad
textPad.tag_add('sel','1.0',END)
menubar = Menu(root)
root.config(menu=menubar)
filemenu = Menu(menubar)
filemenu.add_command(label='新建',command=new)
filemenu.add_command(label='打开',command=openfile)
filemenu.add_separator()
filemenu.add_command(label='保存',command=save)
filemenu.add_command(label='另存为',command=saveas)
menubar.add_cascade(label='文件',menu=filemenu)
changemenu = Menu(menubar)
changemenu.add_command(label='复制',command=copy)
changemenu.add_command(label='粘贴',command=paste)
changemenu.add_command(label='剪切',command=cut)
changemenu.add_separator()
changemenu.add_command(label='撤销',command=undo)
changemenu.add_command(label='重做',command=redo)
changemenu.add_separator()
changemenu.add_command(label='全选',command=all)
menubar.add_cascade(label='编辑',menu=changemenu)
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=12,bg='light sea green')
smallbutton = Button(toolbar,padx=5,pady=5,text='打开',command=openfile)
smallbutton.pack(side=LEFT)
smallbutton = Button(toolbar,padx=5,pady=5,text='保存',command=save)
smallbutton.pack(side=LEFT)
toolbar.pack(fill=X)
status = Label(root,text='Lv.1.1',bd=1,relief=SUNKEN,anchor=W)
status.pack(side=BOTTOM,fill=X)
lnlabel = Label(root,width=2,bg='light sea green')
lnlabel.pack(side=LEFT,fill=Y)
textinput = Text(root)
textinput.pack(expand=YES,fill=BOTH)
scrolly = Scrollbar(textinput)
textinput.config(yscrollcommand=scrolly.set)
scrolly.pack(side=RIGHT,fill=Y)
scrollx = Scrollbar(textinput)
textinput.config(yscrollcommand=scrollx.set)
scrollx.pack(side=BOTTOM,fill=X)
root.mainloop()
运行结果及详细报错内容:
AttributeError: 'str' object has no attribute 'event_generate'
若不将textPad全局化,则报错NameError: name 'textPad' is not defined