Python Programming by Example (2015)
17. Python GUI Programming
This chapter explains how to work with GUI in Python.
17.1 Getting Started
There are many modules to implement GUI in Python. In this chapter, we learn tkinter to build Python GUI. This library can be read on this site, https://docs.python.org/3/library/tk.html . This library has installed on Python 3.x.
Let's start to build Python app with tkinter library.
17.2 Hello Python GUI
The first demo is to build Python GUI Hello World. We use Tk object to build a form.
Write these scripts.
import tkinter as tk
dialog = tk.Tk()
dialog.title('Simple Form')
dialog.mainloop()
Save these scripts into a file, called ch17_01.py. Then, run the program.
$ python3 ch17_01.py
You should see a form dialog with title "Simple Form".
17.3 Working with Input Form
Now we can extend our Tk object into an input form. In this scenario, we put two Textbox and a button. If we click a button, we read Textbox values.
Let's write these scripts for demo.
import tkinter as tk
class InputForm(object):
def __init__(self):
self.root = tk.Tk()
self.root.title('Input Form')
self.num_a = ''
self.num_b = ''
self.frame = tk.Frame(self.root)
self.frame2 = tk.Frame(self.root)
self.frame.pack()
self.frame2.pack()
self.initialization()
def initialization(self):
r = self.frame
k_a = tk.Label(r,text='Number A')
k_a.grid(row=0, column=0)
self.e_a = tk.Entry(r, text='NumA')
self.e_a.grid(row=0, column=1)
self.e_a.focus_set()
k_b = tk.Label(r,text='Number B')
k_b.grid(row=1, column=0)
self.e_b = tk.Entry(r, text='NumB')
self.e_b.grid(row=1, column=1)
self.e_b.focus_set()
r2 = self.frame2
b = tk.Button(r2,text='Save',command=self.get_inputs)
b.pack(side='left')
def get_inputs(self):
self.num_a = self.e_a.get()
self.num_b = self.e_b.get()
self.root.destroy()
def get_values(self):
return self.num_a, self.num_b
def wait_for_input(self):
self.root.mainloop()
dialog = InputForm()
dialog.wait_for_input()
num_a, num_b = dialog.get_values()
print('num a:', num_a)
print('num b:', num_b)
Save the program into a file, called ch17_02.py.
Run the program.
$ python3 ch17_02.py
You should see the input form dialog.
Fill values on Textbox. Then, click Save button.
After clicked, the program will read input values and shows them on Terminal.
17.4 Working with Common Dialogs
tkinter library also provides common dialogs such Messagebox, filedialog and colorchooser.
For illustration, write these scripts.
import tkinter as tk
from tkinter import messagebox, filedialog
from tkinter.colorchooser import *
# messagebox
print('demo messagebox')
messagebox.showinfo('Information', 'This is message')
messagebox.showerror('Error', 'This is error message')
messagebox.showwarning('Warning', 'This is warning message')
# filedialog
dir = filedialog.askdirectory()
print('selected directory:', dir)
file = filedialog.askopenfile(mode="r")
print('selected file:', file.name)
new_file_name = filedialog.asksaveasfilename()
print('save as file:', new_file_name)
# colorchooser
def get_color():
color = askcolor()
print('selected color:', color)
dialog = tk.Tk()
tk.Button(dialog, text='Select Color', command=get_color).pack()
dialog.title('Simple Form')
dialog.mainloop()
Save the program into a file, called ch17_03.py. Then, run the program.
$ python3 ch17_03.py
The following is output forms for information, error and warning.
Output form for selecting a directory.
Output form for selecting a file.
Output form for saving a file.
Output form for selecting a color. Click Select Color button to show Colors dialog.
The following is program output in Terminal.