216.md 2.1 KB
Newer Older
W
wizardforcel 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
# QT4 消息框

> 原文: [https://pythonspot.com/qt4-messagebox/](https://pythonspot.com/qt4-messagebox/)

[PyQT4](https://pythonspot.com/pyqt4/) 使用多种功能提供消息框功能。 [PyQT4](https://pythonspot.com/pyqt4/) 中包含的 消息框是:问题,警告,错误,信息,批评和关于框。

**相关课程:** [使用 Python PyQt5 创建 GUI 应用程序](https://gum.co/pysqtsamples)

## PyQt4 消息框

The code below will display a message box with two buttons:

```
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
import sys
from PyQt4.QtGui import *

# Create an PyQT4 application object.
a = QApplication(sys.argv)

# The QWidget widget is the base class of all user interface objects in PyQt4.
w = QWidget()

# Show a message box
result = QMessageBox.question(w, 'Message', "Do you like Python?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)

if result == QMessageBox.Yes:
    print('Yes.')
else:
    print('No.')

# Show window
w.show()

sys.exit(a.exec_())

```

结果:

![qtMessagebox question](img/42005bdb1e24d6b5c1b14e5895521e3a.jpg) qtMessagebox question

[PyQT4](https://pythonspot.com/pyqt4/) 提供了不同类型的消息框。

## PyQT4 警告框

You can display a warning box using this line of code:

```
QMessageBox.warning(w, "Message", "Are you sure you want to continue?")

```

## PyQT4 信息框

We can display an information box using QMessageBox.information()

```
QMessageBox.information(w, "Message", "An information messagebox @ pythonspot.com ")

```

结果:

![QMessageBox Info](img/6efbaae64acdb4e69214c7589963b5bd.jpg) QMessageBox Info

## PyQT4 临界盒

If something goes wrong in your application you may want to display an error message.

```
QMessageBox.critical(w, "Message", "No disk space left on device.")

```

Result: ![QMessagebox ](img/af9cd039b8bc48207cfd140b3bd3dd1c.jpg) QMessagebox

## PyQT4 关于盒子

We have shown the question box above.

```
QMessageBox.about(w, "About", "An example messagebox @ pythonspot.com ")

```

Result: ![qt Messagebox](img/843b3c0a68065ac101f04944c2bac053.jpg) qt Messagebox

[下载 PyQT 代码(批量收集)](https://pythonspot.com/python-qt-examples/)