Best way to structure a tkinter application?

I advocate an object oriented approach. This is the template that I start out with: # Use Tkinter for python 2, tkinter for python 3 import tkinter as tk class MainApplication(tk.Frame): def __init__(self, parent, *args, **kwargs): tk.Frame.__init__(self, parent, *args, **kwargs) self.parent = parent <create the rest of your GUI here> if __name__ == “__main__”: root … Read more

Why does Tkinter image not show up if created in a function?

The variable photo is a local variable which gets garbage collected after the class is instantiated. Save a reference to the photo, for example: self.photo = tkinter.PhotoImage(…) If you do a Google search on “tkinter image doesn’t display”, the first result is this: Why do my Tkinter images not appear? (The FAQ answer is currently … Read more

Tkinter: AttributeError: NoneType object has no attribute

The grid, pack and place functions of the Entry object and of all other widgets returns None. In python when you do a().b(), the result of the expression is whatever b() returns, therefore Entry(…).grid(…) will return None. You should split that on to two lines like this: entryBox = Entry(root, width=60) entryBox.grid(row=2, column=1, sticky=W) That … Read more

Why is the command bound to a Button or event executed when declared?

Consider this code: b = Button(admin, text=”as”, command=button(‘hey’)) It does exactly the same as this: result = button(‘hey’) b = button(admin, text=”as”, command=result) Likewise, if you create a binding like this: listbox.bind(“<<ListboxSelect>>”, some_function()) … it’s the same as this: result = some_function() listbox.bind(“<<ListboxSelect>>”, result) The command option takes a reference to a function, which is … Read more

How to remove curly bracket from the output in python?

You are inserting a list or tuple into the listbox instead of a string. You need to explicitly convert each row to a string before passing to the insert method. Otherwise, the underlying tcl interpreter will try to preserve the list-like structure of the data by adding curly braces or backslashes when converting the value … Read more