Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use messagebox in PyQt5

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you "how to use messagebox in PyQt5", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use messagebox in PyQt5" this article.

Preface

How to use messagebox will be introduced. Messagebox can be used in message box, warning box, query box, error box, about dialog box, etc.

1. Basic knowledge 1. Introduction to messagebox

Messagebox is a message dialog box, which can prompt the user to focus on the message and obtain the user's choice, so that it is easy to control the execution of the program under special circumstances.

The standard button types are as follows

Example 1. QT Designer design UI

Open designer.exe, create it using the default Main Window, and click the Create button directly.

The design UI diagram is as follows, and saved as messagebox.ui

2 convert messagebox.ui to ui_messagebox.py

Enter the messagebox.py directory and enter pyuic5-o ui_messagebox.py messagebox.ui

3 write python program # encoding=utf-8import sysimport PyQt5.QtWidgets as qwimport ui_msgboxclass myForm (qw.QWidget Ui_msgbox.Ui_Form): def _ init__ (self): super (). _ init__ () self.setupUi (self) self.btn_info.clicked.connect (self.btn_info_cb) self.btn_warn.clicked.connect (self.btn_warn_cb) self.btn_critical.clicked.connect (self.btn_critical_cb) self.btn_ Question.clicked.connect (self.btn_question_cb) self.btn_about.clicked.connect (self.btn_about_cb) def btn_info_cb (self): print ("ready to show messagebox.") Res = qw.QMessageBox.information (self, "prompt", "I am MessageBox of type info!" , qw.QMessageBox.Yes | qw.QMessageBox.No) if (qw.QMessageBox.Yes = = res): print ("[info] you clicked yes button!") Elif (qw.QMessageBox.No = = res): print ("[info] you clicked no button!") Def btn_warn_cb (self): res = qw.QMessageBox.warning (self, "warning", "I am MessageBox of type warn!" , qw.QMessageBox.Yes | qw.QMessageBox.No) if (qw.QMessageBox.Yes = = res): print ("[warn] you clicked yes button!") Elif (qw.QMessageBox.No = = res): print ("[warn] you clicked no button!") Def btn_critical_cb (self): res = qw.QMessageBox.critical (self, "error", "I am MessageBox of type critical!" , qw.QMessageBox.Abort | qw.QMessageBox.Cancel) if (qw.QMessageBox.Abort = = res): print ("[critical] you clicked Abort button!") Elif (qw.QMessageBox.Cancel = = res): print ("[critical] you clicked Cancel button!") Def btn_question_cb (self): res = qw.QMessageBox.question (self, "ask", "I am MessageBox of critical type", qw.QMessageBox.Retry | qw.QMessageBox.Ok) if (qw.QMessageBox.Retry = = res): print ("[question] you clicked Retry button!") Elif (qw.QMessageBox.Ok = = res): print ("[question] you clicked Ok button!") Def btn_about_cb (self): qw.QMessageBox.about (self, "about", "I am MessageBox of type critical!") if _ _ name__ = ='_ main__': app = qw.QApplication (sys.argv) w = myForm () w.show () sys.exit (app.exec_ ())

First, set the button designed with QT Designer in the previous step, add the corresponding messagebox, and get the user's click on the situation, which is displayed through the console log.

III. Operation

Enter the file directory and type python3 run.py to pop up the above page designed with QT Designer.

Click info button:

Click warn button:

Click critical button:

Click question button:

Click about button:

The above is all the content of the article "how to use messagebox in PyQt5". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report