...

Python GUI Toolkits

Python GUI toolkits quick reference.


TKinter cheat sheet code:

References
  • http://effbot.org/tkinterbook/radiobutton.htm
  • http://www.pythonware.com/library/tkinter/introduction/index.htm
#!/usr/bin/python

from Tkinter import *
import tkMessageBox
import tkSimpleDialog

class GridRefImpl:
    def __init__(self, master=None):
       self.master = master
       self.layoutWidgets()
       #self.master.protocol("WM_DELETE_WINDOW", self.window_close_handler)
        
    def layoutWidgets(self):
        self.frame = Frame(self.master, width=400, height=420)
        
        
        menu = Menu(self.master)
        self.master.config(menu=menu)
        filemenu = Menu(menu)
        menu.add_cascade(label="File", menu=filemenu)
        filemenu.add_command(label="New", command=self.menu_handler)
        filemenu.add_command(label="Open...", command=self.menu_handler)
        filemenu.add_separator()
        filemenu.add_command(label="Exit", command=self.menu_handler)
        helpmenu = Menu(menu)
        menu.add_cascade(label="Help", menu=helpmenu)
        helpmenu.add_command(label="About...", command=self.menu_handler) 
        
        
        self.body = Frame(self.frame, bd=1, relief=GROOVE, padx=50, pady=50)
        self.body.pack(fill=BOTH, expand=1)
        
        Label(self.body, text='Entry1:').grid(row=0, sticky=W)
        Label(self.body, text='dialog demo:').grid(row=1, sticky=W)
        Label(self.body, text='listbox:').grid(row=2, sticky=W)
        Label(self.body, text='checkbox:').grid(row=3, sticky=W)
        Label(self.body, text='Quit:').grid(row=4)  
        Label(self.body, text='radiobutton1:').grid(row=5,rowspan=3,sticky=W)      
        Label(self.body, text='radiobutton2:').grid(row=8,rowspan=3,sticky=W)
        Label(self.body, text='Scale:').grid(row=11)
        Label(self.body, text='SpinBox1:').grid(row=12, sticky=W)
        
        self.label1 = Entry(self.body, width=16) # enteredText = self.label1.get()
        self.label1.bind('<Return>',self.enter_key_handler)
        #
        self.button1 = Button(self.body, text="Enter Name", command=self.simple_dialog_demo)
        #
        self.listbox1 = Listbox(self.body, height=4)
        self.listbox1.insert(END, "item1")
        self.listbox1.insert(END, "item2")
        self.listbox1.bind('<Button-3>', self.list_right_click_handler)
        #
        self.cbVar = StringVar()
        self.cb1 = Checkbutton(self.body, text="Enable Tab", variable=self.cbVar, onvalue="RGB", offvalue="L", command=self.cb_clicked)
        #
        self.quitbutton = Button(self.body, text="QUIT", fg="red", command=self.frame.quit)
        #
        self.rb1Var = StringVar()
        self.rb1Var.set("A") # initialize
        self.rb1a = Radiobutton(self.body, text="RB1 A", variable=self.rb1Var, value="A", command=self.rb1_selected)
        self.rb1b = Radiobutton(self.body, text="RB1 B", variable=self.rb1Var, value="B", command=self.rb1_selected)
        self.rb1c = Radiobutton(self.body, text="RB1 C", variable=self.rb1Var, value="C", command=self.rb1_selected)
        #
        self.rb2Var = StringVar()
        self.rb2Var.set("B") # initialize
        self.rb2a = Radiobutton(self.body, text="RB2 A", variable=self.rb2Var, indicatoron=0, value="A", command=self.rb2_selected)
        self.rb2b = Radiobutton(self.body, text="RB2 B", variable=self.rb2Var, indicatoron=0, value="B", command=self.rb2_selected)
        self.rb2c = Radiobutton(self.body, text="RB2 C", variable=self.rb2Var, indicatoron=0, value="C", command=self.rb2_selected)
        #
        self.scale1 = Scale(self.body, from_=0, to=25, resolution=0.5, orient=HORIZONTAL, command=self.scale1_handler)
        #
        self.spinbox1 = Spinbox(self.body, value=(8,6,4,2,0), width=4, command=self.spinbox_handler)
        
        self.label1.grid(row=0, column=1, sticky=W)
        self.button1.grid(row=1, column=1, sticky=W)
        self.listbox1.grid(row=2, column=1, sticky=W)
        self.cb1.grid(row=3, column=1, sticky=W)
        self.quitbutton.grid(row=4, column=1, sticky=W)        
        self.rb1a.grid(row=5,column=1)
        self.rb1b.grid(row=6,column=1)
        self.rb1c.grid(row=7,column=1)
        self.rb2a.grid(row=8,column=1)
        self.rb2b.grid(row=9,column=1)
        self.rb2c.grid(row=10,column=1)        
        self.scale1.grid(row=11,column=1)
        self.spinbox1.grid(row=12,column=1)
        
        self.statusText = StringVar()
        Label(self.master, text="status bar", bd=1, relief=SUNKEN, anchor=W, textvariable=self.statusText).pack(side=BOTTOM, fill=X)        
        self.statusText.set("Tkinter Demo...")
        #self.status = Label(self.master, text="status bar", bd=1, relief=SUNKEN, anchor=W)
        #self.status.pack(side=BOTTOM, fill=X)

        self.frame.pack(fill=BOTH, expand=1)


    def menu_handler(self):
        print "called the menu!"

    def window_close_handler(self):
        # showinfo, showwarning, showerror, askquestion, askokcancel, askyesno, or askretrycancel
        if tkMessageBox.askokcancel("Quit", "Are you sure?\nDo you really wish to quit?"):
            self.master.destroy() 

    def simple_dialog_demo(self):
        # tkSimpleDialog.askinteger(title, prompt [initialvalue="5",minvalue="0",maxvalue="9"])
        # tkSimpleDialog.askfloat(title, prompt [initialvalue="0.12",minvalue="0",maxvalue="9.0"])
        result = tkSimpleDialog.askstring("Como se llama?", "Nombre", initialvalue="?")
        if result:
            self.statusText.set("result:" + result)
            
    def enter_key_handler(self, event):
        self.statusText.set("label1value" + self.label1.get())
        
    def list_right_click_handler(self, event):
        if self.listbox1.curselection():
            self.statusText.set("listbox1 value:" + self.listbox1.get(self.listbox1.curselection()[0]))
        
    def cb_clicked(self):
        self.statusText.set("cbVar value:" + self.cbVar.get())
        
    def rb1_selected(self):
        self.statusText.set("rb1Value: " + self.rb1Var.get())
        
    def rb2_selected(self):
        self.statusText.set("rb2Value: " + self.rb2Var.get())
        
    def scale1_handler(self, event):
        self.statusText.set("rb2Value: " + str(self.scale1.get()))

    def spinbox_handler(self):
        self.statusText.set("spinbox1value" + self.spinbox1.get())
        
root = Tk()
root.title('Grid Ref Impl')
gridRefImpl = GridRefImpl(root)
root.mainloop()