tcl中的tcl错误

时间:2013-07-23 15:47:44

标签: python tkinter

我正在使用这段代码制作一个文本编辑器,我正在尝试添加菜单和快捷方式,我将在以后植入,现在我不知道该怎么做(这是另一个问题,但如果你知道答案请分享。但无论如何我得到这个错误,我不知道为什么。     错误:

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

1 个答案:

答案 0 :(得分:2)

只需将text更改为label即可解决您的问题。例如:

filemenu.add_command(text='Save',  command=self.onSave)

应该是

filemenu.add_command(label='Save',  command=self.onSave)