This article describes how to add a graphical interface for a Python program. graphical interface programming is essential for developing a Python local application. For more information, see third-party libraries that support multiple graphical interfaces in Python, including:
And so on.
However, the Python library supports Tkinter of Tk. you can use Tkinter directly without installing any packages. This chapter briefly introduces how to use Tkinter for GUI programming.
Tkinter
Let's sort out the concept:
- The Python code we compile calls the built-in Tkinter, which encapsulates the interface for accessing Tk;
- Tk is a graphics library that supports multiple operating systems and is developed using the Tcl language;
- Tk calls the local GUI interface provided by the operating system to complete the final GUI.
Therefore, our code only needs to call the interface provided by Tkinter.
First GUI program
It is very easy to use Tkinter. let's compile a GUI version "Hello, world !".
The first step is to import all the content of the Tkinter package:
from Tkinter import *
The second step is to derive an Application class from Frame, which is the parent container of all widgets:
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, each Button, Label, and input box is a Widget. Frame is a Widget that can accommodate other widgets. all widgets are combined as a tree.
The pack () method adds widgets to the parent container and implements layout. Pack () is the simplest layout, and grid () can achieve more complex layout.
In the createWidgets () method, we create a Label and a Button. when a Button is clicked, self. quit () is triggered to exit the program.
Step 3: instantiate the Application and start the Message loop:
App = Application () # set the 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 sequence. Therefore, if message processing is time-consuming, it must be processed in a new thread.
Run the GUI program and you can see the following window:
Click "Quit" or "x" in the window to end the program.
Input text
Let's improve the GUI program by adding a text box so that users can enter the text and click the button to bring up the message dialog box.
from Tkinter import *import tkMessageBoxclass 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' tkMessageBox.showinfo('Message', 'Hello, %s' % name)
When a user clicks the button, the hello () is triggered, and the user input text is obtained through self. nameInput. get (). The message dialog box is displayed using tkMessageBox. showinfo.
The program running result is as follows:
Summary
Python's built-in Tkinter can meet the requirements of basic GUI programs. if it is a very complex GUI program, we recommend that you write it in languages and libraries supported by the operating system.
Source code reference: https://github.com/michaelliao/learn-python/tree/master/gui