-1

Hello I am making a GUI for a program with Tkinter, but I keep getting an error:

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'"

I am a noobie what it comes to classes so I don't know what I'm making wrong.

If you could help me I would appreciate it.

Thanks in advance!!

    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()
  • `Widget(...).grid()` will always assign `None` to variable because `grid()/pack()/place()` returns `None` – furas Jan 14 '16 at 01:27

1 Answers1

0

The problem is this line:

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

Change to two lines:

# 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)
Reut Sharabani
  • 30,449
  • 6
  • 70
  • 88