Appearance
Graphical Interface
Python supports various third-party libraries for graphical interfaces, including:
- Tk
- wxWidgets
- Qt
- GTK
- etc.
However, Python comes with the built-in library Tkinter, which supports Tk. With Tkinter, no additional packages need to be installed, and it can be used directly. This chapter briefly introduces how to use Tkinter for GUI programming.
Tkinter
Let's clarify some concepts:
The Python code we write will call the built-in Tkinter, which wraps the interface to access Tk;
Tk is a graphical library that supports multiple operating systems and is developed using the Tcl language;
Tk will call the native GUI interface provided by the operating system to complete the final GUI.
Thus, our code only needs to call the interfaces provided by Tkinter.
The First GUI Program
Using Tkinter is very simple; let's create a GUI version of "Hello, world!".
The first step is to import everything from the Tkinter package:
python
from tkinter import *
The second step is to derive an Application class from Frame, which is the parent container for all widgets:
python
class Application(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.pack()
self.createWidgets()
def createWidgets(self):
self.helloLabel = Label(self, text='Hello, world!')
self.helloLabel.pack()
self.quitButton = Button(self, text='Quit', command=self.quit)
self.quitButton.pack()
In the GUI, every Button, Label, input box, etc., is a widget. Frame is a widget that can contain other widgets, and all widgets together form a tree structure.
The pack()
method adds the widget to the parent container and manages the layout. pack()
is the simplest layout, while grid()
can implement more complex layouts.
In the createWidgets()
method, we create a Label and a Button. When the Button is clicked, it triggers self.quit()
to exit the program.
The third step is to instantiate Application and start the message loop:
python
app = Application()
# Set window title:
app.master.title('Hello World')
# Main message loop:
app.mainloop()
The main thread of the GUI program is responsible for listening to messages from the operating system and processing each message in turn. Therefore, if message processing is very time-consuming, it needs to be handled in a new thread.
Running this GUI program will display the following window:
Clicking the "Quit" button or the "x" on the window will end the program.
Input Text
Let's improve this GUI program by adding a text box for user input, and then after clicking the button, a message dialog will pop up.
python
from tkinter import *
import tkinter.messagebox as messagebox
class Application(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.pack()
self.createWidgets()
def createWidgets(self):
self.nameInput = Entry(self)
self.nameInput.pack()
self.alertButton = Button(self, text='Hello', command=self.hello)
self.alertButton.pack()
def hello(self):
name = self.nameInput.get() or 'world'
messagebox.showinfo('Message', 'Hello, %s' % name)
app = Application()
# Set window title:
app.master.title('Hello World')
# Main message loop:
app.mainloop()
When the user clicks the button, it triggers hello()
, which retrieves the user input text via self.nameInput.get()
, and tkMessageBox.showinfo()
can pop up the message dialog.
The program's output looks like this:
Summary
The built-in Tkinter in Python can meet the basic requirements of GUI programs. For very complex GUI programs, it is recommended to use the natively supported languages and libraries of the operating system.