Python supports third-party libraries for a variety of graphical interfaces, including:
Wait a minute.
But Python's own library is a tkinter that supports TK, using Tkinter, which can be used directly without installing any packages. This chapter briefly describes how to use Tkinter for GUI programming.
Tkinter
Let's comb the concept:
- The Python code we write will call the built-in Tkinter,tkinter to encapsulate the interface to the TK;
- TK is a graphics library that supports multiple operating systems and is developed using TCL language;
- TK will invoke the local GUI interface provided by the operating system to complete the final GUI.
So, our code just needs to invoke the interface provided by Tkinter.
The first GUI program
Using Tkinter is very simple, let's write a GUI version of "Hello, world!".
The first step is to import all the contents of the Tkinter package:
From Tkinter Import *
The second step is to derive a application class from frame, which is the parent container for 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, input box, and so on, is a widget. Frame is a widget that can accommodate other widgets, and all widgets are grouped together as a tree.
The pack () method adds the widget to the parent container and implements the layout. Pack () is the simplest layout, and grid () allows for more complex layouts.
In the Createwidgets () method, we create a label and a button that triggers self.quit () to exit the program when the button is clicked.
The third step is to instantiate the application and start the message loop:
App = Application () # set window title: App.master.title (' Hello World ') # main message loop: App.mainloop ()
The main thread of the GUI program listens for messages from the operating system and processes each message in turn. Therefore, if message processing is time-consuming, it needs to be handled in a new thread.
To run this GUI program, you can see the following window:
Click the "Quit" button or the "X" of the window to end the program.
Enter text
Let's improve on this GUI program by adding a text box that allows the user to enter text and then click the button to pop 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 the user taps the button, a hello () is triggered, and when the user enters the text via Self.nameInput.get (), the message dialog box pops up using Tkmessagebox.showinfo ().
The results of the program run as follows:
Summary
Python's built-in Tkinter can meet the requirements of basic GUI programs, and if it is a very complex GUI program, it is recommended to write it in natively supported languages and libraries of the operating system.
Source Code Reference: Https://github.com/michaelliao/learn-python/tree/master/gui