I’ve seen two basic ways of setting up a tkinter program. Is there any reason to prefer one to the other?

from Tkinter import *

class Application():
    def __init__(self, root, title):
        self.root = root

        self.label = Label(self.root, text="Hello")
        self.label.grid(row=0, column=0)  

root = Tk()
app = Application(root, 'Sample App')


from Tkinter import *

class Application(Frame):
    def __init__(self, title, master=None):
        Frame.__init__(self, master)

        self.label = Label(self, text="Hello")
        self.label.grid(row=0, column=0) 

app = Application('Sample App')

The option I prefer* is to inherit from the class Tk. I think it is the more reasonable choice since the window is, in effect, your application. Inheriting from Frame doesn’t make any more sense to me then inheriting from Button or Canvas or Label. Since you can only have a single root, it makes sense that that is what you inherit from.

I also think it makes the code more readable if you do the import as import Tkinter as tk rather than from Tkinter import *. All of your calls then explicitly mention the tk module. I don’t recommend this for all modules, but to me it makes sense with Tkinter.

For example:

import Tkinter as tk

class SampleApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.label = tk.Label(text="Hello, world")
        self.label.pack(padx=10, pady=10)

app = SampleApp()

* Note: since originally writing this answer I have changed my position. I now prefer to inherit from Frame rather than Tk. There’s no real advantage one way or the other, it’s more of a philosophical choice than anything else. Regardless, I believe that whether you inherit from Frame or Tk, I think either choice is better than the first example in the code that inherits from nothing.

The one slight advantage inheriting from Frame has over Tk is in the case where you want your application to support multiple identical windows. In that case, inheriting from Frame lets you create the first window as a child of root, and additional windows as children of instances of Toplevel. However, I’ve seen very few programs that ever have a need to do this.

For more information about how I think Tkinter programs should be structured, see my answer to the question Python Tkinter program structure.

A Frame is usually used as a geometry master for other widgets.
Since an application usually has numerous widgets, you’ll often want to contain them all in a Frame, or at least use the Frame to add some borderwidth, padding, or other nicety.

Many example snippets you might find on the web do not use a Frame because
they just want to demonstrate some feature in the shortest amount of code.

So, use a Frame if you need it, otherwise, do not.

Edit: I think the best way to organize a GUI is given in this Tkinter tutorial:


import Tkinter as tk

class SimpleApp(object):
    def __init__(self, master, **kwargs):
        frame=tk.Frame(master, **kwargs)
        self.label = tk.Label(frame, text=title)

if __name__=='__main__':
    root = tk.Tk()
    app = SimpleApp(root,title="Hello, world")

This is mainly like your first example in that SimpleApp inherits from object, not Frame. I think this is better than subclassing Frame since we are not overriding any Frame methods. I prefer to think of SimpleApp as having a Frame rather than being a Frame.

Having SimpleApp subclass object does have a significant advantage over subclassing tk.Tk, however: it makes it easy to embed SimpleApp in a larger app:

import simpleApp
import Tkinter as tk

class BigApp(object):
    def __init__(self, master, **kwargs):
        frame=tk.Frame(master, **kwargs)
        self.simple = simpleApp.SimpleApp(frame,title=title)
        frame.pack(padx=10, pady=10)
        self.simple2 = simpleApp.SimpleApp(frame,title=title)    

if __name__=='__main__':
    root = tk.Tk()
    app = BigApp(root,title="Hello, world")

Thus, simpleApp.py can be a stand-alone script as well as an importable module.
If you try this with SimpleApp inheriting from tk.Tk, you end up with extra undesired windows.

There can be an advantage to setting your top level object to inherit from Tk instead of Frame. The advantage arises when you have dynamic element to your GUI, e.g. a Label whose contents you want to set with a textvariable=foo instead of text="Label text".

In this case, it is very helpful to use the Tkinter.DoubleVar, Tkinter.IntVar, and Tkinter.StringVar objects to hold the data, since the GUI will automatically update whenever these objects are set. However, to use these objects, you must specify their master as the root Tkinter.Tk() instance running. This is easier if you explicitly make your main object be a subclass of Tkinter.Tk, then have that generate frames and widgets, so you can pass along the Tk instance and set up your variables properly.

Here is a short example program to illustrate the idea.

import Tkinter as tk       

class Tkclass(tk.Tk):
    def __init__(self):
        app.master.title("Animal to Meat")

class Application(tk.Frame):    

    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        self.meatvar = tk.StringVar(master=parent)

    def createWidgets(self):
        top.rowconfigure(0, weight=1)            
        top.columnconfigure(0, weight=1)         
        self.rowconfigure(0, weight=1)           
        self.columnconfigure(0, weight=1) 
        self.columnconfigure(1, weight=1)  
        self.columnconfigure(2, weight=1)  
        self.columnconfigure(3, weight=1)  

        self.cowButton = tk.Button(self, text="Cow", command=self.setBeef)
        self.pigButton = tk.Button(self, text="Pig",command=self.setPork)
        self.meatLabel = tk.Label(self)
        self.quit = tk.Button(self, text="Quit",command=self.QuitApp)
        self.quit.grid(row=0, column=3)           

    def setBeef(self):

    def setPork(self):

    def QuitApp(self):

main = Tkclass()