Small knowledge, big challenge! This article is participating in the “Essentials for Programmers” creative activity. This article has participated in the “Digitalstar Project” to win a creative gift package and challenge the creative incentive money.

The fifth official Python column, stop! Don’t miss this zero-based article!

In Python, you can use Python to create a simple UI application. You can use Python to create a simple UI application.

We’re going to make a window, and we’re going to put two buttons that tell us different messages.

Just do this simple function.

Let’s start with a UI library

The Tkinter Package (” Tk Interface “) is a Python interface based on The Tcl/Tk GUI tool standard. The Tk and Tkinter libraries are available on most operating systems, including MacOS, Windows, and some Unix-like operating systems!

Small white simple understanding, this is a standard graphical user interface development library! Or a built-in Python library that doesn’t need to be installed again.

Ok, let’s start the student committee to lead us to make App window.

Step one, make the window, run

Here, students are using the Tkinter library, python’s built-in UI library.

# -*- coding: utf-8 -*-
# @csDN /Juejin/Wechat: Lei Xuewei

from tkinter import *

Create the main window
top = Tk()
top.title("TkinterDemo by the Thunder Academy") # title Settings
top.geometry('300x100') # Set the window size to 300x100
Call the main event loop to keep the window program running.
top.mainloop()
Copy the code

Just a few lines of code, save it as first_ui.py and run it directly.

Ok, let’s get straight to the effect:

That’s easy.

The second step is to upgrade the difficulty and add the button

Here’s what it looks like. Try adding two buttons.

Just add the button component based on the previous code.

# -*- coding: utf-8 -*-
# @Author : LeiXueWei
# @csDN /Juejin/Wechat: Lei Xuewei
# @XueWeiTag: CodingDemo
# -*- coding: UTF-8 -*-

from tkinter import *

top = Tk()
top.title("TkinterDemo by the Thunder Academy")
top.geometry('300x100')


def open_msg_box():
    pass


def open_err_box():
    pass

# Add button 1
first = Button(top, text="Click -> View Message".fg="tomato".command=open_msg_box)
first.pack()
# Add button 2
second = Button(top, text="Click -> View Errors".fg="tomato".command=open_err_box)
second.pack()
top.mainloop()
Copy the code

It doesn’t seem too hard, but running the code above just shows

See, there is a command parameter assignment that is a function (but the committee hasn’t implemented the details yet).

The third step, to achieve the event pop-up message window, error window

Click different buttons to display different types of Windows and messages.

# -*- coding: utf-8 -*-
# @Time: 2021/8/29 11:58 am
# @Author : LeiXueWei
# @csDN /Juejin/Wechat: Lei Xuewei
# @XueWeiTag: CodingDemo
# @File : first_ui.py.py
# @Project : hello

from tkinter import *
import tkinter.messagebox as mb

top = Tk()
top.title("TkinterDemo by the Thunder Academy")
top.geometry('600x400')


def open_msg_box():
    mb.showinfo("[Tips from the Committee]"."Simple message pop-up window!")


def open_err_box():
    mb.showerror("[Tips from the Committee]"."Demo error message window!")


first = Button(top, text="Click -> View Message".fg="tomato".command=open_msg_box)
first.pack()
second = Button(top, text="Click -> View Errors".fg="tomato".command=open_err_box)
second.pack()
top.mainloop()
Copy the code

That’s it. That’s it. You got it? You’re welcome to punch in the comments section.

Be sure to learn. In the next installment, we’ll learn how to develop this tool:

If you like Python, please pay attention to learn the Basic Python column or Get started in Python to master the big column

Continuous learning and continuous development, I am Lei Xuewei! Programming is fun. The key is to get the technology right. Welcome to pay attention, and feel free to like the collection!