我正在使用这段代码来制作一个文本编辑器,我正在尝试添加菜单和快捷方式,我将在稍后植入它到现在我不知道该怎么做(如果你知道答案,那是另一天的另一个问题请分享它。但无论如何我收到此错误,我不知道为什么。错误:
Traceback (most recent call last):
File "C:\Python27\Completed Projects\editor.py", line 90, in <module>
Editor().mainloop()
File "C:\Python27\Completed Projects\editor.py", line 52, in __init__
filemenu.add_command(text='Save', command=self.onSave).pack(side=LEFT)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 2683, in add_command
self.add('command', cnf or kw)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 2674, in add
self._options(cnf, kw))
TclError: unknown option "-text"
和代码:
from Tkinter import *
from tkSimpleDialog import *
from tkFileDialog import *
from tkMessageBox import *
root = Tk()
menubar = Menu(root)
class QuitMe(Frame):
def __init__(self, parent=None):
Frame.__init__(self, parent)
self.pack()
widget = Button(self, text='Quit', command=self.quit)
widget.pack(expand=YES, fill=BOTH, side=LEFT)
def quit(self):
ans = askokcancel('Confirm exit', "Sure you want to Quit?")
if ans: Frame.quit(self)
class ScrolledText(Frame):
def __init__(self, parent=None, text='', file=None):
Frame.__init__(self, parent)
self.pack(expand=YES, fill=BOTH)
self.makewidgets()
self.settext(text, file)
def makewidgets(self):
sbar = Scrollbar(self)
text = Text(self, relief=SUNKEN)
sbar.config(command=text.yview)
text.config(yscrollcommand=sbar.set)
sbar.pack(side=RIGHT, fill=Y)
text.pack(side=LEFT, expand=YES, fill=BOTH)
self.text = text
def settext(self, text='', file=None):
if file:
text = open(file, 'r').read()
self.text.delete('1.0', END)
self.text.insert('1.0', text)
self.text.mark_set(INSERT, '1.0')
self.text.focus()
def gettext(self):
return self.text.get('1.0', END+'-1c')
class Editor(ScrolledText):
def __init__(self, parent=None, file=None):
frm = Frame(parent)
frm.pack(fill=X)
filemenu = Menu(menubar, tearoff=0)
editmenu = Menu(menubar, tearoff=0)
filemenu.add_command(text='Save', command=self.onSave).pack(side=LEFT)
editmenu.add_command(text='Cut', command=self.onCut).pack(side=LEFT)
editmenu.add_command(text='Paste', command=self.onPaste).pack(side=LEFT)
editmenu.add_command(text='Find', command=self.onFind).pack(side=LEFT)
QuitMe(frm).pack(side=LEFT)
ScrolledText.__init__(self, parent, file=file)
self.text.config(font=('courier', 9, 'normal'))
def onSave(self):
filename = asksaveasfilename()
if filename:
alltext = self.gettext()
open(filename, 'w').write(alltext)
def onCut(self):
text = self.text.get(SEL_FIRST, SEL_LAST)
self.text.delete(SEL_FIRST, SEL_LAST)
self.clipboard_clear()
self.clipboard_append(text)
def onPaste(self):
try:
text = self.selection_get(selection='CLIPBOARD')
self.text.insert(INSERT, text)
except TclError:
pass
def onFind(self):
target = askstring('SimpleEditor', 'Search String?')
if target:
where = self.text.search(target, INSERT, END)
if where:
print where
pastit = where + ('+%dc' % len(target))
self.text.tag_add(SEL, where, pastit)
self.text.mark_set(INSERT, pastit)
self.text.see(INSERT)
self.text.focus()
if len(sys.argv) > 1:
Editor(file=sys.argv[1]).mainloop()
else:
Editor().mainloop()