Python 2.7:Tkinter:类 - 属性错误

时间:2016-01-13 23:58:49

标签: python-2.7 class tkinter attributeerror nonetype

您好我正在为使用Tkinter的程序制作GUI,但我一直收到错误:

Traceback (most recent call last):
 File "C:\Python27\lib\lib-tk\Tkinter.py", line 1470, in __call__
 return self.func(*args)
 File "C:\Users\jorge\Desktop\new  1.py", line 53, in browse_button
 self.browse_entry.delete(0,END)
AttributeError: 'NoneType' object has no attribute 'delete'"

我对课程有什么看法,所以我不知道自己做错了什么。

如果你能帮助我,我将不胜感激。

提前致谢!!

    from Tkinter import *
    from tkFileDialog import *
    import ttk,os 

    class App(Tk):

        def __init__(self, *args, **kwargs):
            Tk.__init__(self, *args, **kwargs)

            self.title("Subtitles Master")
            self.iconbitmap(default = r"C:\Users\jorge\Desktop\Programas Python\notepad.ico")
            mainframe = Frame(self)
            mainframe.pack(side = "top")
            mainframe.grid_rowconfigure(0, weight = 1)
            mainframe.grid_columnconfigure(0, weight = 1)

            self.frames = {}

            for F in (StartPage, Timing):

                frame = F(mainframe, self)

                self.frames[F] = frame

                frame.grid (row = 0, column = 0, sticky = "nsew")

            self.show_frame(StartPage)

        def show_frame(self, cont):

            frame = self.frames[cont]
            frame.tkraise()


    class StartPage(Frame):

        def __init__(self, parent, controller):
            Frame.__init__(self, parent)
            filler1 = Frame(self, width = 30, height = 20).grid(column = 4, row = 0, rowspan = 2)
            syncsubs_button = ttk.Button(self, text = "Sync Subtitles", state = DISABLED).grid(column = 5, row = 0)
            timing_button = ttk.Button(self, text = "Edit Timing", command = lambda: controller.show_frame(Timing)).grid(column = 5, row = 1)
            filler2 = Frame(self, width = 20, height = 20).grid(column = 6, row = 0, rowspan = 2)
            syncsubs_label= Label(self, font = ("Alien Encounters", 20), text = "Sync Subtitles", fg = "dark green").grid(row = 2, columnspan = 6)
            browse_label = Label(self, font = ("Calibri",12), text = "Subtitles to sync:").grid(row = 3, column = 0, sticky = "w")
            filename1 = StringVar()
            self.browse_entry = Entry(self, textvariable = filename1, width = 50).grid(row = 4, column = 1, columnspan = 5)
            browse_button = ttk.Button(self, text = "Browse", command = self.browse_button).grid(row = 4, column = 0)

        def browse_button(self):
            filename = askopenfilename(title = "Choose a file",filetypes = (("Subtitles", "*.srt"), ("Text", "*.txt")))
            self.browse_entry.delete(0,END)
            self.browse_entry.insert(0,filename)
            return filename

    class Timing(Frame):
        def __init__(self, parent, controller):
            Frame.__init__(self, parent)
            filler1 = Frame(self, width = 30, height = 20).grid(column = 4, row = 0, rowspan = 2)
            syncsubs_button = ttk.Button(self, text = "Sync Subtitles", command = lambda: controller.show_frame(StartPage)).grid(column = 5, row = 0)
            timing_button = ttk.Button(self, text = "Edit Timing", state = DISABLED).grid(column = 5, row = 1)
            filler2 = Frame(self, width = 20, height = 20).grid(column = 6, row = 0, rowspan = 2)
            syncsubs_label= Label(self, font = ("Alien Encounters", 20), text = "Edit Timing", fg = "dark red").grid(row = 2, columnspan = 6)

    window = App()

    window.mainloop()

1 个答案:

答案 0 :(得分:0)

问题在于这一行:

# returns None (Entry.grid(...))
self.browse_entry = Entry(self, textvariable = filename1, width = 50).grid(row = 4, column = 1, columnspan = 5)

更改为两行:

# returns entry
self.browse_entry = Entry(self, textvariable = filename1, width = 50)
# do stuff with entry
self.browse_entry.grid(row = 4, column = 1, columnspan = 5)