关于tkinter的一个很简单的编辑器代码错误

关于tkinter的一个很简单的编辑器代码错误

#!/usr/local/bin/python

from Tkinter import *
from ScrolledText import*
import tkMessageBox
from tkFileDialog import *
import fileinput

tl = []
root = None

def die():
    sys.exit(0)

def about():
    tkMessageBox.showinfo("Tkeditor","test\n" "2007.09.16\n" "author:rubee")

class editor:
    def __init__(self,rt):
        if rt == None:
            self.t = Tk()
        else:
            self.t = Toplevel(rt)
        self.t.title("Tkeditor %d" % len(tl))
        self.bar = Menu(rt)

        self.filem = Menu(self.bar)
        self.filem.add_command(label="Open...",command=self.openfile)
        self.filem.add_command(label="New...",command=neweditor)
        self.filem.add_command(label="Save as...",command=self.savefile)
        self.filem.add_command(label="Close",command=self.close)
        self.filem.add_separator()
        self.filem.add_command(label="Exit",command=die)

        self.helpm = Menu(self.bar)
        self.helpm.addcommand(label="About",command=About)

        self.bar.add_cascade(label="File",menu=self.filem)
        self.bar.add_cascade(label="Help",menu=self.helpm)
        self.t.config(menu=self.bar)

        self.f = Frame (self.t,width=512)
        self.f.pack(expand=1,fill=BOTH)
        self.st = ScrolledText(self.f,background="white")
        self.st.pack(side=LEFT,fill=BOTH,expand=1)

def close(self):
    self.t.destroy()

def openfile(self):
    pl = END
    oname = askopenfilename(filetypes=(("python files","*.py")))
    if oname:
        for line in fileinput.input(oname):
            self.st.insert(pl,line)
        self.t.title(oname)

def savefile(self):
    sname = asksaveasfilename()
    if sname:
        ofp = open(sname,"w")
        ofp.write(self.st.get(1.0,END))
        ofp.flush()
        ofp.close()
        self.t.title(sname)

def neweditor():
    global root
    tl.append(editor(root))

if __name__== "__main__":
    root = None
    tl.append (editor(root))
    root = tl[0].t
root.mainloop()
        

   
        

        




这段代码错在什么地方了?
缩进问题:(例如)
    self.filem.add_command(label="Open...",command=self.openfile)

但是openfile不在类中.

Menu控件问题:
    self.helpm.addcommand(label="About",command=About)
AttributeError: Menu instance has no attribute 'addcommand'

#!/usr/local/bin/python

from Tkinter import *
from ScrolledText import*
import tkMessageBox
from tkFileDialog import *
import fileinput

tl = []
root = None

def die():
    sys.exit(0)

def about():
    tkMessageBox.showinfo("Tkeditor","test\n" "2007.09.16\n" "author:rubee")

class editor:
    def __init__(self,rt):
        if rt == None:
            self.t = Tk()
        else:
            self.t = Toplevel(rt)
            self.t.title("Tkeditor %d" % len(tl))
            self.bar = Menu(rt)

            self.filem = Menu(self.bar)
            self.filem.add_command(label="Open...",command=self.openfile)
            self.filem.add_command(label="New...",command=neweditor)
            self.filem.add_command(label="Save as...",command=self.savefile)
            self.filem.add_command(label="Close",command=self.close)
            self.filem.add_separator()
            self.filem.add_command(label="Exit",command=die)

            self.helpm = Menu(self.bar)
            self.helpm.addcommand(label="About",command=About)

            self.bar.add_cascade(label="File",menu=self.filem)
            self.bar.add_cascade(label="Help",menu=self.helpm)
            self.t.config(menu=self.bar)

            self.f = Frame (self.t,width=512)
            self.f.pack(expand=1,fill=BOTH)
            self.st = ScrolledText(self.f,background="white")
            self.st.pack(side=LEFT,fill=BOTH,expand=1)

        def close(self):
            self.t.destroy()

        def openfile(self):
            pl = END
            oname = askopenfilename(filetypes=(("python files","*.py")))
            if oname:
                for line in fileinput.input(oname):
                    self.st.insert(pl,line)
                    self.t.title(oname)

        def savefile(self):
            sname = asksaveasfilename()
            if sname:
                ofp = open(sname,"w")
                ofp.write(self.st.get(1.0,END))
                ofp.flush()
                ofp.close()
                self.t.title(sname)

        def neweditor():
            global root
            tl.append(editor(root))

if __name__== "__main__":
    root = None
    tl.append (editor(root))
    root = tl[0].t
root.mainloop()
                                

        

        


我改过后,可以运行,但是出现不了我想要的结果

真的可以运行吗?
self.filem.add_command(label="New...",command=neweditor)
但你的neweditor定义是类里面。

类似的缩进问题还有不少。
#!/usr/local/bin/python

from Tkinter import *
from ScrolledText import*
import tkMessageBox
from tkFileDialog import *
import fileinput

tl = []
root = None


class editor:
      
      
      def __init__(self,rt):
            
            if rt == None:
                  self.t = Tk()
            else:
                  self.t = Toplevel(rt)
            self.t.title("Tkeditor %d" % len(tl))
            self.bar = Menu(rt)

            self.filem = Menu(self.bar)
            self.filem.add_command(label="Open...",command=self.openfile)
            self.filem.add_command(label="New...",command=neweditor)
            self.filem.add_command(label="Save as...",command=self.savefile)
            self.filem.add_command(label="Close",command=self.close)
            self.filem.add_separator()
            self.filem.add_command(label="Exit",command=self.die)

            self.helpm = Menu(self.bar)
            self.filem.add_command(label="About",command=self.about)

            self.bar.add_cascade(label="File",menu=self.filem)
            self.bar.add_cascade(label="Help",menu=self.helpm)
            self.t.config(menu=self.bar)

            self.f = Frame(self.t,width=512)
            self.f.pack(expand=1,fill=BOTH)
            self.st = ScrolledText(self.f,background="white")
            self.st.pack(side=LEFT,fill=BOTH,expand=1)

      def close(self):
            self.t.destroy()

      def openfile(self):   
            pl = END
            oname = askopenfilename(filetypes=(("python files","*.py")))
            if oname:            
                  for line in fileinput.input(oname):
                        self.st.insert(pl,line)
                        self.t.title(oname)

      def savefile(self):   
            sname = asksaveasfilename()
            if sname:
                  ofp = open(sname,"w")
                  ofp.write(self.st.get(1.0,END))
                  ofp.flush()
                  ofp.close()
                  self.t.title(sname)
                  
      def about(self):
            tkMessageBox.showinfo("Tkeditor","test\n" "2007.09.16\n" "author:rubee")
            
      def die(self):
            sys.exit(0)

def neweditor(self):
      global root
      tl.append(editor(root))

if __name__== "__main__":
    root = None
    tl.append (editor(root))
    root = tl[0].t
    root.mainloop()
给你调好了,运行一下,看能得到你要的结果不?
可以了,谢谢 。我好好看看我的错误!