[Introduction to Python] 38. First GUI program

Keywords: Python Programming Qt network

ABSTRACT: Teach you how to use Python's built-in Tkinter to create the first GUI program.

Write on the front: In order to learn python better, bloggers record their learning journey. This learning note is based on Liao Xuefeng's Python Course If there is any infringement, please inform us to delete it. Welcome to study Python with bloggers.

Catalog

Graphical interface

Python supports a variety of third-party libraries for graphical interfaces, including Tk, wxWidgets, Qt, GTK, etc.

This paper introduces how to use Tkinter to program GUI.

Python has built-in Tkinter, which encapsulates the interface for accessing the graphics library Tk, through which simple GUI programming can be completed.

Tkinter

The steps to create a GUI program are as follows:
1. Import Tkinter module;
2. Create a class that inherits the Frame (Frame is a container for control, which you can understand as a rectangular frame);
3. Create controls
4. Specify the master of the control;
5. Start the message loop

The first GUI program:

from tkinter import *                    # All the content of introducing the Tkinter package

class A(Frame):                          # Define a Frame class
    def __init__(self, master=None):     
        Frame.__init__(self, master)     
        self.pack()                      # Place controls in the main interface to achieve layout
        self.createWidgets()             # Call the method to create the control

    def createWidgets(self):
        self.helloLabel = Label(self, text='Hello,world!')       # Create a label control to display text or bitmaps
        self.helloLabel.pack()                         # Put the label control in the main interface
        self.quitButton = Button(self, text='Quit', command=self.quit)    # Create button controls for exit
        self.quitButton.pack()

app = A()                                          # Instantiation of A
app.master.title('The first GUI')                  # Setting the title of the window
app.master.geometry('200x100')                     # Set window size
app.mainloop()                                     # Start a message loop

The results are as follows:

Of course, many other controls can be added, such as adding text boxes, letting users enter text, and then displaying information.

At this point, we need to introduce a messagebox module, the specific code () is as follows:

from tkinter import *
import tkinter.messagebox as messagebox

class A(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.pack()
        self.createWidgets()

    def createWidgets(self):
        self.nameInput = Entry(self)                              # Entry lets users enter text
        self.nameInput.pack()
        self.alertButton = Button(self, text='Hello', command=self.hello)   # Add button control, button with hello method
        self.alertButton.pack()

    def hello(self):
        name = self.nameInput.get() or 'world'                    # Get the text entered by the user
        messagebox.showinfo('Message', 'Hello, %s' % name)        # Pop-up message dialog box

app = A()
app.master.title('Hello World')
app.mainloop()

The results are as follows:

Introduction to Core Control:

Control name function
Button Button control, create a button
Canvas Drawing control for displaying graphics or text
Checkbutton Multi-check box control, displaying a multi-select box
Entry Enter controls to allow users to enter text
Text Text control to display multi-line text
Frame Frame controls, containers for storing controls
Label Label control for displaying text or bitmaps
Listbox Listbox control to display a list of strings
Menu Menu control, displaying menu bar
Menubutton Menu button control to display menu items
Message Message control that displays multiple lines of text, similar to label
Radiobutton A radio box control that displays a one-way selection box
Scale Scope control, which creates a slider to set a range value
Scrollbar Scrollbar Framework, used when the interface exceeds the visual area
Toplevel Container framework for creating subwindows

Python's built-in Tkinter can meet the requirements of basic GUI programs. If it's a very complex GUI program, it's recommended to write in native operating system supported languages and libraries.

That's all for this section. Thank you for reading.

Next section: Network programming

If you have any questions or thoughts, please welcome comments and Tucao.

Learn Python with bloggers ()~*

Posted by bufke on Thu, 09 May 2019 14:30:38 -0700