Python GUI Tk : UI Class

 Sub class button: add callback method and pack myself

from Tkinter import *

class HelloButton(Button):
    def __init__(self, parent=None, side=TOP, **config): 
        Button.__init__(self, parent, config)            
        self.pack(side=side)                             
        self.config(command=self.callback)
    def callback(self):
        print 'callback...'                   
        self.quit()
 
if __name__ == '__main__':
    HelloButton(side=LEFT, text='Hello Button').mainloop()
Demonstrates using a class with Tkinter
from Tkinter import *

class Application(Frame):
    def __init__(self, master):
        Frame.__init__(self, master)
        self.grid()
        self.create_widgets()

    def create_widgets(self):
        self.bttn1 = Button(self, text = "OK")
        self.bttn1.grid()

        self.bttn2 = Button(self)
        self.bttn2.grid()  
        self.bttn2.configure(text = "Cancel")

        self.bttn3 = Button(self)
        self.bttn3.grid()
        self.bttn3["text"] = "Apply"

root = Tk()
root.title("Use button")
root.geometry("200x85")
app = Application(root)
root.mainloop()
Define GUI in a class
from Tkinter import *

class GUI:
    def __init__(self):
     self.root = Tk()
        self.root.title('Frame Styles')
        for bdw in range(5):
            setattr(self, 'of%d' % bdw, Frame(self.root, borderwidth=0))
            Label(getattr(self, 'of%d' % bdw),
                  text='borderwidth = %d  ' % bdw).pack(side=LEFT)
            ifx = 0
            for relief in [RAISED, SUNKEN, FLAT, RIDGE, GROOVE, SOLID]:
                setattr(self, 'f%d' % ifx, Frame(getattr(self, 'of%d' % bdw),
                                                 borderwidth=bdw, relief=relief))
                Label(getattr(self, 'f%d' % ifx), text=relief, width=10).pack(side=LEFT)
                getattr(self, 'f%d' % ifx).pack(side=LEFT, padx=7-bdw, pady=5+bdw)        
                ifx = ifx+1
            getattr(self, 'of%d' % bdw).pack()        

myGUI = GUI()
myGUI.root.mainloop()

Using A Class Structure to define GUI

from Tkinter import *

class MyApp:                         
  def __init__(self, myParent):
    self.myContainer1 = Frame(myParent)
    self.myContainer1.pack()
    
    self.button1 = Button(self.myContainer1) 
    self.button1["text"]= "Hello"     
    self.button1["background"] = "green"      
    self.button1.pack()                         
    
root = Tk()
myapp = MyApp(root)  
root.mainloop()    

Creating a simple dialog

from Tkinter import *

class MyDialog:
    def __init__(self, parent):

        top = self.top = Toplevel(parent)

        Label(top, text="Value").pack()

        self.e = Entry(top)
        self.e.pack(padx=5)

        b = Button(top, text="OK", command=self.ok)
        b.pack(pady=5)

    def ok(self):

        print "value is", self.e.get()

        self.top.destroy()


root = Tk()

d = MyDialog(root)

root.wait_window(d.top)
Button action inside a class
from Tkinter import *

class App:
    def __init__(self, master):

        frame = Frame(master)
        frame.pack()

        self.button = Button(frame, text="QUIT", fg="red", command=frame.quit)
        self.button.pack(side=LEFT)

        self.hi_there = Button(frame, text="Hello", command=self.say_hi)
        self.hi_there.pack(side=LEFT)

    def say_hi(self):
        print "hi!"

root = Tk()

app = App(root)

root.mainloop()

Define class to handle GUI components

from Tkinter import *

class App:
    def __init__(self, master):

        frame = Frame(master)
        frame.pack()

        self.button = Button(frame, text="QUIT", fg="red", command=frame.quit)
        self.button.pack(side=LEFT)

        self.hi_there = Button(frame, text="Hello", command=self.say_hi)
        self.hi_there.pack(side=LEFT)

    def say_hi(self):
        print "hi there, everyone!"

root = Tk()

app = App(root)

root.mainloop()       

Subclasses button

from Tkinter import *

class HelloButton(Button):
    def __init__(self, parent=None, **config):     
        Button.__init__(self, parent, config)      
        self.pack()
        self.config(command=self.callback)
    def callback(self):                            
        print 'Hi'                   
        self.quit()
 
if __name__ == '__main__':
    HelloButton(text='Hello Button').mainloop()
Subclass HelloButton: redefine press-handler method
from Tkinter import *

class HelloButton(Button):
    def __init__(self, parent=None, **config):       
        Button.__init__(self, parent, config)        
        self.pack()
        self.config(command=self.callback)
    def callback(self):                              
        print 'Hiiii...'                     
        self.quit()

class MyButton(HelloButton):        
    def callback(self):             
        print "Ignoring press!..."

if __name__ == '__main__':
    MyButton(None, text='My Button').mainloop()