1

私はこのコードを使用してテキストエディタを作成しています.メニューとショートカットを追加しようとしています.後で移植します.今のところ、その方法はわかりません.共有してください.しかし、とにかくこのエラーが発生し、理由がわかりません.エラー:

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

1 に答える 1

2

textに変更するだけで問題を解決できますlabel。例えば:

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

する必要があります

filemenu.add_command(label='Save',  command=self.onSave)
于 2013-07-23T16:02:46.457 に答える