Python 3.2 Tkinter GUI Beginners Example Code

February 2, 2013
Tags:

UPDATED (2/2/2013)

This example is now working. I had to add a sequence to the bind statements.

Have you ever wanted to add a simple GUI to an excellent python script without installing any additional modules? Here is some basic example code to help you. When I started I wanted a GUI solution that would work natively¬†with Python 3.2. This search brought me to Tkinter. Tkinter has been around for a long time so I had trouble finding example code specifically for Python 3.2. I have included examples of items that are explained in this “how to” on Pythonware.com. This was written on a Windows (XP/7) machine with ActivePython 3.2 in Notepad++. Enjoy!

I ran into problems when running this script from the command line. I was successful when I opened the code in IDLE and pressed F5 for Run Module.


#!/usr/bin/env python
#--------------------------
#Brad 
#
#Aug 4
#
#Gui testing
#--------------------------

from tkinter import *
import tkinter.messagebox as tkMessageBox
import tkinter.simpledialog as tkSimpleDialog
import tkinter.filedialog as tkFileDialog
import tkinter.simpledialog as tkSimpleDialog
import os

#########################
#
#Global vars
#
#########################

labeltext = "Status"

########################
#
#Simple Dialog Class (Not used)
#
########################
class MyDialog(tkSimpleDialog.Dialog):

    def body(self, master):

        Label(master, text="First:").grid(row=0)
        Label(master, text="Second:").grid(row=1)

        self.e1 = Entry(master)
        self.e2 = Entry(master)

        self.e1.grid(row=0, column=1)
        self.e2.grid(row=1, column=1)
        return self.e1 # initial focus

    def apply(self):
        first = string.atoi(self.e1.get())
        second = string.atoi(self.e2.get())
        print (first)

##########################
#
#Define Functions
#
###########################
def windowclose():
	if tkMessageBox.askokcancel("Quit", "Do you really wish to quit?"):
		root.destroy()		

def tempwindow2():
	tkMessageBox.showwarning("Hi","close me")

def middlemouseclick(event):
	if tkMessageBox.askokcancel("Quit", "Do you really wish to quit?"):
		root.destroy()

#####CAPTURE MOUSE MOTION AND UPDATE LABELS############
def mousemotion(event):
		sx.set(event.x)
		sy.set(event.y)

#####CAPTURE MOUSE CLICK AND UPDATE LABELS#############
def mouseclick(event):
		cx.set(event.x)
		cy.set(event.y)

########CHANGE THE BACKGROUND BASED ON SCALE VALUE######
def scaleupdate(sc):
		#print (scalevalue.get())
		if scalevalue.get() == 0:
			frame.config(bg="lightblue")
		elif scalevalue.get() == 1:
			frame.config(bg="blue")
		elif scalevalue.get() == 3:
			frame.config(bg="red")
		elif scalevalue.get() ==5:
			frame.config(bg="green")
		else:
			frame.config(bg="yellow")

########UPDATE THE LABELS IN THE STATUS BAR########
def labelupdate(newtext):
		l.set(newtext)
		if ckb.get():
			n.set(n.get()+5)
		else:
			n.set(n.get()+1)

###########INPUT DATA################
def inputdata():
	x = tkSimpleDialog.askinteger("Input","Multiplier")
	if x:
		tkMessageBox.showwarning("Yes",x)
		listbox.insert(END, x)
	else:
		tkMessageBox.showwarning("No","You did it wrong")

############SELECT FILE#################
def fileselect():
	filename = tkFileDialog.askopenfilename()
	if filename:
		tkMessageBox.showwarning("Yes", filename)
	else:
		tkMessageBox.showwarning("No", "You did it wrong")

#####################################
#
#MAIN
#
#####################################

root = Tk()

###########ROOT WINDOW PROPERTIES#####
root.resizable(False,False)
root.iconbitmap(default="")  ##input your own ico file here. this will replace the default red Tk icon
root.title("Test")
root.attributes("-alpha", 1.0)
root.protocol("WM_DELETE_WINDOW", windowclose)

###############main frame##############
frame = Frame(root,relief='raised', border=4, bg="lightblue", padx=12)
frame.bind("<a>", mousemotion)
frame.bind("<a>", middlemouseclick)
frame.grid(column =0,row=1, sticky='w')

#############CLICK HERE#################
label = Label(frame, text="Click Here", cursor="gumby")
label.bind("<Enter>", mouseclick)
label.grid(column=0,row=0)

############HELLO########################
message = Message(frame, text="Hello")
message.grid(column=1,row=0)

############SCALE##########################
scalevalue = DoubleVar()
scalevalue.set(0)
scale = Scale(frame, activebackground="blue", background="red", to="10", troughcolor="green", width="25", command= scaleupdate, variable=scalevalue )
scale.grid(column=0,row=1, columnspan=2)

###################STATUS BAR###############
l = StringVar()
l.set("Status")
status = Label(root, textvariable=l, bd=1, relief=SUNKEN, anchor='w')
status.grid(column=0, row=4, sticky='w')

n = IntVar()
n.set(0)
status2 = Label(root, textvariable=n, bd=1, relief=SUNKEN, anchor='w')
status2.grid(column=0, row=4, sticky='w', padx=40)

sx = IntVar()
sx.set(0)
statusx = Label(root, textvariable=sx, bd=1, relief=SUNKEN, anchor='w')
statusx.grid(column=0, row=4, sticky='w', padx=65)

sy = IntVar()
sy.set(0)
statusy = Label(root, textvariable=sy, bd=1, relief=SUNKEN, anchor='w')
statusy.grid(column=0, row=4, sticky='w', padx=95)

cx = IntVar()
cx.set(0)
statuscx = Label(root, textvariable=cx, bd=1, relief=SUNKEN, anchor='w')
statuscx.grid(column=0, row=4, sticky='w', padx=125)

cy = IntVar()
cy.set(0)
statuscy = Label(root, textvariable=cy, bd=1, relief=SUNKEN, anchor='w')
statuscy.grid(column=0,row=4,sticky='w', padx=140)

####################RADIO BUTTONS###########
v = IntVar()
radbutton = Radiobutton(frame, text = "Option 1", variable=v, value=1)
radbutton.grid(column = 2, row = 0, sticky = NW)

radbutton2 = Radiobutton(frame, text = "Option 2", variable=v, value=2)
radbutton2.grid(column = 2, row = 1, sticky = NW)

#################CHECK BUTTONS##############
ckb = IntVar()
ckbbutton = Checkbutton(frame, text = "Add 5", variable=ckb)
ckbbutton.grid(column = 2, row= 2, sticky=NW)

#################LIST BOX#####################

listbox = Listbox(frame, selectmode=MULTIPLE)
listbox.grid(column=2, row=3, sticky=NW)

listbox.insert(END, "zero")
listbox.insert(END, "one")
listbox.insert(END, "two")

###################IMAGES IN BASE64 ############
img00 = PhotoImage(format='gif', data="R0lGODlhKwAQAJEAACWgA/3bYYNOGgAAACwAAAAAKwAQAAACfIyPqcsrD2M0oAJqa8h29yAkITiG3HWmKWiUrdtpseZdtcfmJSyjvf2Z5Q671u0wA9I+RtLjZcwgfxglTTchjqS34JUrCUMQySOzih07Tb62eeneqSfU8vsmf65xZa8S/zI3dlLD5deRl1dlxdT4MYIA2TBJuSZ2iZkZVgAAOw==")
img01 = PhotoImage(format='gif', data="R0lGODlhDwAPAKECAAAAzMzM/////wAAACwAAAAADwAPAAACIISPeQHsrZ5ModrLlN48CXF8m2i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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAMAAAEALAAAAAAQABAAAAiZAAMIHEiwoMGDBzNkwHDBAkKBGXpI5MGjAsKIMjJm7CEhAoQHDhoIxNBDo0mJEhncCHChB4yXMGPKWFBjgs2bOG1+aIGAxoQYJk3G6DCBhQGfQGPClPFiAogCNAL8dEG1KtUZGjwQiPpTxoivYEfM4LBhQFSpMUKoXatWBAUBNQROUECXboIDBgoQGGDCxkAbNAILHuz34cGAADs=")

imglabel = Label(frame, image=cat)
imglabel.grid(column=3, row=0, rowspan=4)

############TOOLBAR#####################
toolbar = Frame(root)
b = Button(toolbar, image=open, width=20, command=tempwindow2)
b.grid(column=0, row=0, sticky='w')
b2 = Button(toolbar, image=img00, width=50, command=lambda: labelupdate("status"))
b2.grid(column=1, row=0)
toolbar.grid(column=0, row=0, sticky='w')

#######################MENU#################
menu = Menu(root)
root.config(menu=menu)

filemenu = Menu(menu)
menu.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="Open", command=fileselect)
filemenu.add_command(label="Quit", command=windowclose)

editmenu = Menu(menu)
menu.add_cascade(label="Edit", menu=editmenu)
editmenu.add_command(label="Input", command=inputdata)

helpmenu = Menu(menu)
menu.add_cascade(label="Help", menu=helpmenu)
helpmenu.add_command(label = "About", command=tempwindow2)
#######ROOT MAIN###########################
root.mainloop()

 

6 Responses to “Python 3.2 Tkinter GUI Beginners Example Code”

  1. Sadly – it don’t work. Don’t even run.

    I’m too new to Python and tkinter to understand why. I’m going through as many scripts as I can to understand this tkinter beast.

    When run, all I get is:
    C:\Python33\python.exe “D:/002 Programs/Python Programming/Projects/Hello World/Hello World.py”
    Traceback (most recent call last):
    File “D:/002 Programs/Python Programming/Projects/Hello World/Hello World.py”, line 137, in
    frame.bind(“”, mousemotion)
    File “C:\Python33\lib\tkinter\__init__.py”, line 1006, in bind
    return self._bind((‘bind’, self._w), sequence, func, add)
    File “C:\Python33\lib\tkinter\__init__.py”, line 961, in _bind
    self.tk.call(what + (sequence, cmd))
    _tkinter.TclError: no events specified in binding

    Process finished with exit code 1

  2. Froggy,

    Are you using Python 3.2 or Python 3.3?

  3. Hi all,

    I got the same error on Python 3.2.

  4. I just tried it on python 3.2 with windows 7 and had problems. Are you guys using windows 7 64bit?

  5. steps done
    select script
    open vim
    control v or paste in VIM
    save file as tkint2.py ( what file name you prefer of course)
    run in DOS prompt (run as administrator)
    C:\Python32\python tkint2.py and the GUI displays it works!

    Thanks Brad for the headstart makes the tkinter tutorial easier to follow
    btw i had same issue finding example code specifically for Python 3.2

    windoze version and Python version details
    Windows OS 7 Entrerprise 64 Bit Operating System
    Python v3.2.3 (http://www.python.org/download/releases/3.2.3/)
    http://docs.python.org/3.2/library/tkinter.html?highlight=tkinter#a-simple-hello-world-program

  6. Thanks for the feedback.

Leave a Reply