PyQt5 must learn the pop-up message box every day

不言
Release: 2018-04-19 11:23:20
Original
6525 people have browsed it

This article mainly introduces in detail the pop-up message box that you must learn every day in PyQt5. It has a certain reference value. Interested friends can refer to it

By default, if we click on the title X button on the bar, the QWidget closes. Sometimes, we need to change this default behavior. For example, if we have a file that we want to open in an editor, we can first display a message box to confirm the open or not operation.

#!/usr/bin/python3
# -*- coding: utf-8 -*-

"""
PyQt5 教程

当我们点击应用程序窗口的关闭按钮,该程序显示一个确认信息框。

作者:我的世界你曾经来过
博客:http://blog.csdn.net/weiaitaowang
最后编辑:2016年7月29日
"""

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QMessageBox

class Example(QWidget):

 def __init__(self):
  super().__init__()

  self.initUI()

 def initUI(self):

  self.setGeometry(300, 300, 300, 220)
  self.setWindowTitle('消息盒子')  
  self.show()

 def closeEvent(self, event):

  reply = QMessageBox.question(self, '信息', '确认退出吗?', 
   QMessageBox.Yes | QMessageBox.No, QMessageBox.No)

  if reply == QMessageBox.Yes:
   event.accept()
  else:
   event.ignore()

if __name__ == '__main__':

 app = QApplication(sys.argv)
 ex = Example()
 sys.exit(app.exec_())
Copy after login

If we close the QWidget control, the QCloseEvent event will be generated. To modify the control properties we need to reimplement the closeEvent() event handler.

 reply = QMessageBox.question(self, '信息', '确认退出吗?', 
   QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
Copy after login

We display a message box with two buttons: Yes and No. The first string appears in the title bar. The second string is the message text displayed in the dialog box. The third parameter specifies the button combination in the pop-up dialog box. The last parameter is the default button, which is the button with initial keyboard focus. The return value is stored in the reply variable.

 if reply == QMessageBox.Yes:
   event.accept()
  else:
   event.ignore()
Copy after login

Here we use if to test the return value. If we click the Yes button, we accept the closing of the button control and execute the application's terminate event. Otherwise, we ignore the closing event.

After the program is executed, click the ##PyQt5 realizes the download progress bar effect

PyQt5 must learn the pop-up message box every dayPyQt5 must learn the pop-up message box every dayPyQt5 must learn the progress bar effect every day


PyQt5 must learn every day QSplitter implements window separation

The above is the detailed content of PyQt5 must learn the pop-up message box every day. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!