博客
关于我
QT教程5:消息框
阅读量:796 次
发布时间:2023-03-03

本文共 2390 字,大约阅读时间需要 7 分钟。

PyQt5 Message Box与窗口居中示例

在PyQt5中,消息框和窗口居中的实现是日常开发中的常见需求。以下将详细介绍如何在PyQt5中创建消息框以及如何将窗口居中。

消息框示例

首先,让我们看看如何在PyQt5中创建一个带有确认按钮的消息框。以下是一个简单的PyQt5脚本:

#!/usr/bin/python
"""ZetCode PyQt5 tutorial
This program shows a confirmation message box when we click on the close button of the application window.
Author: Jan Bodnar
Website: zetcode.com"""
import sys
from PyQt5.QtWidgets import QWidget, QMessageBox, QApplication
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Message box')
self.show()
def closeEvent(self, event):
reply = QMessageBox.question(
self, 'Message',
"Are you sure to quit?",
QMessageBox.Yes | QMessageBox.No,
QMessageBox.No
)
if reply == QMessageBox.Yes:
event.accept()
else:
event.ignore()
def main():
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()

消息框的工作原理

  • 定义消息框:使用QMessageBox.question()方法创建一个带有确认按钮的消息框。该方法返回一个QMessageBox.Result类型的值,表示用户选择的按钮。

  • 设置参数:第一个参数是父窗口,第二个是标题,第三个是显示在消息框中的文本。第四个参数是按钮组合,第五个参数是默认按钮。

  • 处理返回值:根据返回值判断用户是否点击了"是"或"否"按钮,并相应地处理事件。

  • 屏幕居中示例

    为了确保窗口在屏幕上居中,我们可以使用QDesktopWidget和几何功能。以下是一个实现窗口居中的PyQt5脚本:

    #!/usr/bin/python
    """ZetCode PyQt5 tutorial
    This program centers a window on the screen.
    Author: Jan Bodnar
    Website: zetcode.com"""
    import sys
    from PyQt5.QtWidgets import QWidget, QDesktopWidget, QApplication
    class Example(QWidget):
    def __init__(self):
    super().__init__()
    self.initUI()
    def initUI(self):
    self.resize(250, 150)
    self.center()
    self.setWindowTitle('Center')
    self.show()
    def center(self):
    qr = self.frameGeometry()
    cp = QDesktopWidget().availableGeometry().center()
    qr.moveCenter(cp)
    self.move(qr.topLeft())
    def main():
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())
    if __name__ == '__main__':
    main()

    屏幕居中代码解释

  • 获取窗口几何qr = self.frameGeometry()获取当前窗口的几何形状。

  • 计算屏幕中心cp = QDesktopWidget().availableGeometry().center()计算屏幕的可用区域中心点。

  • 移动窗口中心qr.moveCenter(cp)将窗口的几何形状移动到屏幕中心。

  • 调整窗口位置self.move(qr.topLeft())将窗口的左上角移动到几何形状的左上角,从而实现居中。

  • 通过以上代码,我们可以轻松地在PyQt5中创建消息框并将窗口居中。这些建议在实际开发中非常实用,可以提升用户体验。

    转载地址:http://mcxfk.baihongyu.com/

    你可能感兴趣的文章
    POJ 1061 青蛙的约会 (扩展欧几里得)
    查看>>
    Quartz2.2.1简单使用
    查看>>
    POJ 1080 Human Gene Functions(DP:LCS)
    查看>>
    Quant 开源项目教程
    查看>>
    POJ 1088 滑雪
    查看>>
    POJ 1095 Trees Made to Order
    查看>>
    POJ 1113 Wall(计算几何--凸包的周长)
    查看>>
    poj 1125Stockbroker Grapevine(最短路)
    查看>>
    Qualitor processVariavel.php 未授权命令注入漏洞复现(CVE-2023-47253)
    查看>>
    poj 1151 (未完成) 扫描线 线段树 离散化
    查看>>
    POJ 1151 / HDU 1542 Atlantis 线段树求矩形面积并
    查看>>
    poj 1163 数塔
    查看>>
    POJ 1177 Picture(线段树:扫描线求轮廓周长)
    查看>>
    Qualitor checkAcesso.php 任意文件上传漏洞复现(CVE-2024-44849)
    查看>>
    POJ 1182 食物链(并查集拆点)
    查看>>
    POJ 1185 炮兵阵地 (状态压缩DP)
    查看>>
    POJ 1195 Mobile phones
    查看>>
    POJ 1228 Grandpa's Estate (稳定凸包)
    查看>>
    poj 1236(强连通分量分解模板题)
    查看>>
    poj 1258 Agri-Net
    查看>>