GUI Apps
Python provides a variety of ways to develop graphical user interfaces (GUIs). Among these are:
- Tkinter - the standard and most commonly Python GUI package.
It is a free, thin object-oriented layer on top of Tcl/Tk. Both Tk and Tkinter are available
on most Unix platforms and Windows platforms. It was was written by Fredrik Lundh.
- wxPython - a free,
open-source, cross-platform GUI toolkit created in 1998 for the Python programming language.
- Jython - an implementation of the Python programming language designed to run on the Java platform and thus with access to the Java GUI tools such as Swing, AWT and SWT.
Example: Create a Window in Tkinter
import tkinter
win = tkinter.Tk()
win.mainloop()
Output:
Tkinter provides a variety of controls. These include:
- Button
- Canvas
- Checkbutton
- Entry
- Frame
- Label
- Listbox
- Menubutton
- Menu
- Message
- PanelWindow
- ProgressBar
- Radiobutton
- Scale
- Scrollbar
- Text
- Toplevel
- TreeView
- Spinbox
- PanelWindow
- LabelFrame
- tkMessageBox
Tkinter also the following top-level windows:
- tk_chooseColor - pops up a dialog box for user to select a color
- tk_chooseDirectory - pops up a dialog box for user to select a directory
- tk_dialog - creates a modal dialog and waits for a response
- tk_getOpenFile - pops up a dialog box for user to select a file to open
- tk_getSaveFile - pops up a dialog box for user to select a file to save
- tk_messageBox - pops up a message window and waits for user response
- tk_popup - posts a popup menu
- toplevel - creates and manipulates toplevel widgets
Example: Displaying Text and an Image in a Window
import tkinter as tk
root = tk.Tk()
logo = tk.PhotoImage(file="Alan_Turing_Memorial.png")
win1 = tk.Label(root, image=logo).pack(side="right")
explanation = """The Alan Turing memorial statue in
Sackville Park, Manchester."""
win2 = tk.Label(root,
justify=tk.LEFT,
padx = 8,
text=explanation).pack(side="left")
root.mainloop()
Output:
Example: Quit button using Tkinter Pack Geometry Manager
from tkinter import *
win = Frame()
win.pack()
Button(win, text='Quit', command=win.quit).pack(side=RIGHT)
win.mainloop()
Output:
Example: Text Labels
import tkinter as tk
root = tk.Tk()
tk.Label(root,
text="Row, row, row your boat,",
fg = "blue",
font = "Arrial").pack()
tk.Label(root,
text="Gently down the stream.",
fg = "yellow",
bg = "blue",
font = "Verdana 10 bold").pack()
tk.Label(root,
text="Merrily, merrily, merrily, merrily,",
fg = "black",
bg = "yellow",
font = "Helvetica 16 bold italic").pack()
tk.Label(root,
text="Life is but a dream.",
fg = "cyan",
bg = "dark green",
font = "Times 18 bold italic").pack()
root.mainloop()
Output:
Example: Passing Data to a Function
import tkinter as tk
def display(out):
tk.Label(root,
text=out,
fg="blue",
font="Arrial").pack()
root = tk.Tk()
root.title("IP Function Example")
display("Greetings")
button = tk.Button(root, text='Quit', width=25, command=root.destroy)
button.pack()
root.mainloop()
Output:
Example: Seconds Counter
The following Python program will display a counter that increment by one each second along with a Stop button that will stop end the program and close the window.
import tkinter as tk
counter = 0
def counter_label(label):
def count():
global counter
counter += 1
label.config(text=str(counter))
label.after(1000, count)
count()
root = tk.Tk()
root.title("Second Counter")
label = tk.Label(root, fg="blue")
label.pack()
counter_label(label)
button = tk.Button(root, text='Stop', width=25, command=root.destroy)
button.pack()
root.mainloop()
Output:
Example: Input Box, Button, and If Statement
In the example below, the user is asked to enter a text into a textbox. If the user enters "awesome" then the program responds "Correct". If the user does not enter "awesome", then the program responds "Incorrect".
import tkinter
root = tkinter.Tk()
root.geometry("250x250")
root.title("IF Statement Example")
start = tkinter.Entry(root)
start.pack()
print(start.get())
def on_button():
if start.get() == "awesome":
slabel = tkinter.Label(root, text="Correct")
slabel.pack()
else:
slabel = tkinter.Label(root, text="Sorry. Incorrect")
slabel.pack()
button = tkinter.Button(root, text="Check Answer", command=on_button)
button.pack()
root.mainloop()
Output:
Example: Input Box, Button, and If Statement
The example below allows user input to be evaluated as a float data type to determine an exam letter grade.
import tkinter
root = tkinter.Tk()
root.geometry("250x250")
root.title("IF Statement Example")
start = tkinter.Entry(root)
start.pack()
print(start.get())
def on_button():
if float(start.get()) >= 90:
slabel = tkinter.Label(root, text="You achieved an A")
slabel.pack()
elif float(start.get()) >= 80:
slabel = tkinter.Label(root, text="You achieved an B")
slabel.pack()
button = tkinter.Button(root, text="Enter Your Exam Score to Find out Grade", command=on_button)
button.pack()
root.mainloop()
Output: