[QT] Use of QMessageBox message box (16)

In actual projects, pop-up message boxes are a very common operation, including error message prompts, warning message prompts, about message prompts, and judgment information selection and other operations. So today, through this section, we will learn more about the use of message boxes. method.

1. Environment configuration

1.python 3.7.8  

You can directly enter the official website to download and install: Download Python | Python.org

2. QT  Designer 

Official download path: Qt Designer Download for Windows and Mac

 

2. Example demonstration

Here we can understand it directly through the code, look down.

First import the library:

from PyQt5.QtWidgets import QMessageBox

1. Information prompt box

msg_box = QMessageBox(QMessageBox.Information, '标题', '今天真的很开心啊!')
msg_box.exec_()

The output looks like this:

A simple two lines of code can complete the pop-up display of our information prompt text box.

Let’s take a look at the analysis of the internal parameters of the function:

(1) QMessageBox.Information: The function of this parameter is to indicate that this is an information prompt box, which mainly displays some basic information to the user. It is easy to understand through the Chinese meaning of Information.

(2) Parameter 2: Parameter 2 here is the title information of our text box. Just write the desired title information.

(3) Parameter 3: This is the text content of our information text box

Here we use the QMessageBox.exec_() method, and its return value is the button type clicked by the user. More detailed usage of this function will be discussed in the following examples.

2. Error prompt box

msg_box = QMessageBox(QMessageBox.Critical, '错误', '错误提示!')
msg_box.exec_()

The output looks like this:

Let’s take a look at the analysis of the internal parameters of the function:

(1) QMessageBox.Critical: The function of this parameter is to indicate that this is an error prompt box, which mainly prompts error messages to the user. The other two parameters have similar functions to the information prompt box parameters above.

3.Alarm prompt box

msg_box = QMessageBox(QMessageBox.Warning, '警告', '10S后程序即将结束运行!')
msg_box.exec_()

The output looks like this:

Let’s take a look at the internal parameter analysis of this function:

(1) QMessageBox.Critical: The function of this parameter is to indicate that this is an alarm prompt box, which mainly prompts the user with alarm information. The other two parameters have similar functions to the information prompt box parameters above.

4. About the prompt box

def trigger_actHelp(self):  # 触发
    QMessageBox.about(self, "About",
                      """使用说明:1.xxxxxx  2.xxxxxxx""")
    return

The output looks like this:

Let’s take a look at the internal parameter analysis of this function:

(1) QMessageBox.about: The function of this parameter is to indicate that this is a text description prompt box, which mainly prompts users with instructions, introduction and other information. The other two parameters have similar functions to the information prompt box parameters above.

5. Judgment prompt box

The main function of the above prompt boxes is to give us the information prompted, so how to perform different choices based on the prompted information? Here comes the judgment prompt box .

# 创建消息弹出框
msg_box = QMessageBox(QMessageBox.Information, '标题', '那么,做出你的选择吧!', QMessageBox.Yes | QMessageBox.No)
# 获取用户点击的按钮
reply = msg_box.exec_()
if reply == QMessageBox.Yes:
    print('您选择了YES!')
else:
    print('您选择了NO!')

The output is shown below:

When we select Yes, the following content is output:

Otherwise the output is:

3. Summary

So the above is the basic use of our message prompt box. In the project, pop-up box judgment should be actively used to enhance the interactive experience between the program and the user.

@Neng

Guess you like

Origin blog.csdn.net/pengneng123/article/details/132688625