手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

使用PyQt5和Qt Designer创建Python GUI应用程序(五)

消息框/弹出窗口

[日期:2020-05-12] 来源:Linux公社  作者:醉落红尘 [字体: ]

这篇PyQt5教程将向您展示如何使用pyqt创建消息框/弹出窗口。我们将使用QMessageBox类来完成此操作。

如何创建PyQt5消息框:

为了显示一个消息框,我们需要导入QMessageBox。

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

我们使用方法QMessageBox.question()来显示消息框。

PyQt5消息框代码

复制下面的代码以显示一个消息框。

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QMessageBox
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot

class Linuxidc(QWidget):

    def __init__(self):
        super().__init__()
        self.title = 'PyQt5消息框 - Linux公社 www.linuxidc.com'
        self.left = 50
        self.top = 50
        self.width = 600
        self.height = 400
        self.initUI()

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        buttonReply = QMessageBox.question(self, 'PyQt5消息框', "你喜欢PyQt5吗?Linux公社 www.linuxidc.com", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
        if buttonReply == QMessageBox.Yes:
            print('单击Yes')
        else:
            print('单击No')

        self.show()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Linuxidc()
    sys.exit(app.exec_())

运行以下代码:

使用PyQt5和Qt Designer创建Python GUI应用程序(五)

使用PyQt5和Qt Designer创建Python GUI应用程序(五)

消息框的更多按钮

考虑到我们使用QMessageBox.Yes和QMessageBox.No。 我们可以轻松添加其他选项:

buttonReply = QMessageBox.question(self, 'PyQt5消息框', "你喜欢PyQt5吗?Linux公社 www.linuxidc.com", QMessageBox.Yes  | QMessageBox.No | QMessageBox.Cancel,  QMessageBox.Cancel)
print(int(buttonReply))
if buttonReply ==  QMessageBox.Yes:
    print('点击了YES')
if buttonReply ==  QMessageBox.No:
    print('点击了No')
if buttonReply ==  QMessageBox.Cancel:
    print('取消')

可用的按钮有:

QMessageBox.Cancel QMessageBox.Ok QMessageBox.Help
QMessageBox.Open QMessageBox.Save QMessageBox.SaveAll
QMessageBox.Discard QMessageBox.Close QMessageBox.Apply
QMessageBox.Reset QMessageBox.Yes QMessageBox.YesToAll
QMessageBox.No QMessageBox.NoToAll QMessageBox.NoButton
QMessageBox.RestoreDefaults QMessageBox.Abort QMessageBox.Retry
QMessageBox.Ignore

创建弹出窗口:

PyQt QMessageBox,可以用来创建对话框。这是一个你经常在桌面上看到的小弹出窗口。

它可能是一行消息,“您确定要保存吗?”消息或更高级的东西。

这个消息框支持各种变化和按钮。接下来你将学习如何创建一个信息对话框窗口。

对话框

初始窗口

创建一个带有按钮的窗口。如果你点击按钮,对话框就会弹出。

(PyQt也是在这里初始化的。)

def window():
   app = QApplication(sys.argv)
   win = QWidget()
   button1 = QPushButton(win)
   button1.setText("点击这里显示对话框 www.linuxidc.com")
   button1.move(200,50)
   button1.clicked.connect(showDialog)
   win.setWindowTitle("显示对话框www.linuxidc.com")
   win.show()
   sys.exit(app.exec_())

因此,让我们看一下showDialog()。

创建一个对话框

使用QMessageBox()创建一个对话框。 不要忘记从PyQt5导入它。

from PyQt5.QtWidgets import QPushButton

然后使用setIcon(), setText(), setWindowTitle()方法设置窗口装饰。

您可以使用setStandardButtons()配置对话框按钮。

def showDialog():
   msgBox = QMessageBox()
   msgBox.setIcon(QMessageBox.Information)
   msgBox.setText("消息框弹出窗口www.linuxidc.com")
   msgBox.setWindowTitle("消息框示例www.linuxidc.com")
   msgBox.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
   msgBox.buttonClicked.connect(msgButtonClick)

   returnValue = msgBox.exec()
   if returnValue == QMessageBox.Ok:
      print('点击了 OK')

完整代码:

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QMessageBox
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot

def window():
   app = QApplication(sys.argv)
   win = QWidget()
   button1 = QPushButton(win)
   button1.setText("点击这里显示对话框 www.linuxidc.com")
   button1.move(200,50)
   button1.clicked.connect(showDialog)
   win.setWindowTitle("显示对话框www.linuxidc.com")
   win.show()
   sys.exit(app.exec_())

def showDialog():
   msgBox = QMessageBox()
   msgBox.setIcon(QMessageBox.Information)
   msgBox.setText("消息框弹出窗口www.linuxidc.com")
   msgBox.setWindowTitle("消息框示例www.linuxidc.com")
   msgBox.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
   msgBox.buttonClicked.connect(msgButtonClick)

   returnValue = msgBox.exec()
   if returnValue == QMessageBox.Ok:
      print('点击了 OK')

def msgButtonClick(i):
   print("点击的按钮是:",i.text())

if __name__ == '__main__': 
   window()

运行代码:

 使用PyQt5和Qt Designer创建Python GUI应用程序(五)

 使用PyQt5和Qt Designer创建Python GUI应用程序(五)

 本文暂时先这样,有空再补充,请继续关注本文。

在下一个教程中,我们将讨论如何使用ComboBoxes组合框。

使用PyQt5和Qt Designer创建Python GUI应用程序https://www.linuxidc.com/search.aspx?where=nkey&keyword=65658

更多Python相关信息见Python 专题页面 https://www.linuxidc.com/topicnews.aspx?tid=17

Linux公社的RSS地址https://www.linuxidc.com/rssFeed.aspx

本文永久更新链接地址https://www.linuxidc.com/Linux/2020-05/163168.htm

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款