Tktiner
Basics

#!/usr/bint/python

import tkinter as tk

class myGUI(object):

    def __init__(self):
        self.main_window = tk.Tk() # create root window
        self.main_window.pack_propagate(0)
        self.main_window.geometry("500x500")
        self.main_window.configure(bg = "#AAA5A4")

        self.frame1 = tk.Frame(self.main_window, bg = "blue", bd = 5, relief = "groove") # create frame 1
        self.frame2 = tk.Frame(self.main_window, bg = "yellow", bd = 5, relief = "groove") # create frame 2

        # add components into frame1
        self.label1 = tk.Label(self.frame1, text = "US Liquid Gallon:", bd = 5, relief = "ridge")
        v = tk.StringVar(self.frame1, value='10')
        self.entry = tk.Entry(self.frame1, textvariable = v, bd = 5, relief = "sunken", justify = "center")

        self.label1.place(relx = 0, rely = 0.1, relheight = 0.8, relwidth = 0.5, anchor = "nw")
        self.entry.place(relx = 0.5, rely = 0.1, relheight = 0.8, relwidth = 0.5, anchor = "nw")
        self.frame1.place(relx = 0, rely = 0.02, anchor="nw", relheight = 0.3, relwidth = 1)

        # add components into frame2
        self.label2 = tk.Label(self.frame2, text = "Liters:", bd = 5, relief = "ridge")
        self.label3 = tk.Label(self.frame2, text = "37.8541", bd = 5, relief = "sunken")

        self.label2.place(relx = 0, rely = 0.1, relheight = 0.8, relwidth = 0.5, anchor = "nw")
        self.label3.place(relx = 0.5, rely = 0.1, relheight = 0.8, relwidth = 0.5, anchor = "nw")

        self.frame2.place(relx = 0, rely = 0.34, anchor = "nw", relheight = 0.3, relwidth = 1)

        # add components into frame3
        self.frame3 = tk.Frame(self.main_window, bg = "red", bd = 5, relief = "groove")
        self.button = tk.Button(self.frame3, text = "Convert", bd = 20, relief = "raised", bg = "green", command =self.convert)

        self.button.bind("<Button-1>", self.mouseclick)
        self.button.bind("<ButtonRelease-1>", self.mouserelease)
        self.button.pack(side = "left", expand = True, fill = "both")

        self.frame3.place(relx = 0, rely = 0.66, anchor = "nw", relheight = 0.3, relwidth = 1)

        tk.mainloop()

    def convert(self):
        value = self.entry.get()
        self.label3.config(text = str(float(value)*3.78541))

    def mouseclick(self, event):
        self.frame3.config(bg = "#395497", relief = "sunken")
        self.button.config(activebackground="blue") # button color is supported in Mac

    def mouserelease(self, event):
        self.frame3.config(bg = "red", relief = "groove")

def main():
    gui = myGUI()

if __name__ == '__main__':
    main()
		
Layout
  • pack(), organizes widgets in blocks in parent widget
  • grid(), organizes widgets in a table-like structure in parent widget
  • place(), organizes widgets by placing them in a specific position in the parent widget
  • Events
  • <Button-1>, events that mouse button has been pressed while the mouse cursor is positioned over the widge, Button 1 is the left mouse button, Button 3 is the right, and Button 2 the middle button
  • <ButtonRelease-1>, indicates that the left button has been released
  • <Enter>, <Leave>, the mouse curson has entered or left the widget
  • <Key>, key on the keyboard was pressed
  • <Configure>, the widget has changed size.
  • Scrolling
    import Tkinter as tk
    
    class myGUI(object):
    
        def __init__(self):
            self.window = tk.Tk()
            self.window.geometry("200x100")
            self.window.resizable(0, 0)
    
            self.frame = tk.LabelFrame(self.window, text = "Scrolling", bd = 2, relief = "sunken")
            self.frame.place(x = 10, y = 10, height = 80, width = 180, anchor = "nw")
    
            self.scrollbar = tk.Scrollbar(self.frame)
            self.scrollbar.place(x = 160, y = 0, height = 80, width = 10, anchor = "nw")
    
            self.listbox = tk.Listbox(self.frame, bg = "blue", bd= 0, yscrollcommand=self.scrollbar.set)
            self.listbox.place(x = 0, y = 0, height = 80, width = 160, anchor = "nw")
            self.scrollbar.config(command=self.listbox.yview)
    
            for i in range(100):
                self.listbox.insert(i, i)
    
            tk.mainloop()
    
    def main():
        gui = myGUI()
    
    if __name__ == '__main__':
        main()
    		
    Reference
  • Tutorialspoint
  • Tkinter Summary
  • An Introduction To Tkinter
  • Documentation
  • Reference
  • Color Charts for TKinter
  • Python STL Library
  • Wiki