欢迎访问我的网站,希望内容对您有用,感兴趣的可以加入我们的社群。

PyQt5系列教程(十)对话框类控件

PyQt5开发 迷途小书童 5年前 (2020-04-11) 5021次浏览 0个评论

软硬件环境

  • Windows 10 64bit
  • Anaconda3 with python 3.7
  • PyCharm 2019.3

视频看这里

此处是youtube的播放链接,需要科学上网。喜欢我的视频,请记得订阅我的频道,打开旁边的小铃铛,点赞并分享,感谢您的支持。

简介

对话框是人机交互中最常用的一种方式。PyQt5中使用QDialog来表示对话框,它有几个常用的子类,QMessageBoxQFileDialogQInputDialogQFontDialog

pyqt5 qdialog

QDialog使用

基本对话框

import sys

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class DialogDemo(QMainWindow):

    def __init__(self, parent=None):
        super(DialogDemo, self).__init__(parent)
        # 设置窗口标题
        self.setWindowTitle("Dialog demo")
        self.resize(600, 400)

        self.button = QPushButton(self)
        self.button.setText("点击弹出对话框")
        self.button.move(50, 50)
        # 绑定slot函数
        self.button.clicked.connect(self.showDialog)

    def showDialog(self):
        dialog = QDialog()
        btn = QPushButton("ok", dialog)
        btn.move(50, 50)
        dialog.setWindowTitle("Dialog")
        # 设置窗口模态,Qt.ApplicationModal子窗口会拦截事件,父窗口不响应;Qt.WindowModal可以响应事件
        dialog.setWindowModality(Qt.ApplicationModal)
        dialog.exec_()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    demo = DialogDemo()
    demo.show()
    sys.exit(app.exec_())

pyqt5 qdialog

QMessageBox

弹出消息对话框,也是最常用的对话框

import sys

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class DialogDemo(QMainWindow):

    def __init__(self, parent=None):
        super(DialogDemo, self).__init__(parent)
        self.setWindowTitle("QMessageBox demo")
        self.resize(600, 400)

        self.button = QPushButton(self)
        self.button.setText("点击弹出QMessageBox")
        self.button.move(50, 50)
        self.button.clicked.connect(self.showDialog)

    def showDialog(self):
        # 除了information,还有warning、critical、question、about
        reply = QMessageBox.information(self, "标题", "消息正文", QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
        print(reply)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    demo = DialogDemo()
    demo.show()
    sys.exit(app.exec_())

pyqt5 qdialog

QFileDialog

文件对话框,主要用来进行文件选择或者文件保存

import sys

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class DialogDemo(QMainWindow):

    def __init__(self, parent=None):
        super(DialogDemo, self).__init__(parent)
        self.setWindowTitle("Dialog demo")
        self.resize(600, 400)

        self.button = QPushButton(self)
        self.button.setText("点击弹出文件对话框")
        self.button.move(50, 50)
        self.button.clicked.connect(self.showDialog)

    def showDialog(self):
        # 文件打开窗口,路径"D:\\"是窗口打开默认显示的路径,最后一个参数文件的过滤,不满足条件的不会显示
        filename, _ = QFileDialog.getOpenFileName(self, "打开文件", "D:\\", "Image Files (*.jpg *.png)")
        if filename:
            print(f"file: {filename}")

if __name__ == '__main__':
    app = QApplication(sys.argv)
    demo = DialogDemo()
    demo.show()
    sys.exit(app.exec_())

pyqt5 qdialog

QInputDialog使用

输入对话框,包含一个输入框和二个按钮

import sys

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class DialogDemo(QMainWindow):

    def __init__(self, parent=None):
        super(DialogDemo, self).__init__(parent)
        self.setWindowTitle("QInputDialog demo")
        self.resize(600, 400)

        self.button = QPushButton(self)
        self.button.setText("点击弹出QInputDialog")
        self.button.move(50, 50)
        self.button.clicked.connect(self.showDialog)

    def showDialog(self):
        # name是获取到的文本值
        name, okPressed = QInputDialog.getText(self, "迷途小书童", "请输入你的大名:", QLineEdit.Normal, " ")
        if okPressed and name:
            print(f'Welcome {name}')
        else:
            QMessageBox.critical(self, "Error", "请输入大名并点击OK!")

if __name__ == '__main__':
    app = QApplication(sys.argv)
    demo = DialogDemo()
    demo.show()
    sys.exit(app.exec_())

pyqt5 qdialog

QFontDialog使用

字体选择对话框

import sys

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class DialogDemo(QMainWindow):

    def __init__(self, parent=None):
        super(DialogDemo, self).__init__(parent)
        self.setWindowTitle("Dialog demo")
        self.resize(600, 400)

        self.button = QPushButton(self)
        self.button.setText("点击弹出字体对话框")
        self.button.move(50, 50)
        self.button.clicked.connect(self.showDialog)

    def showDialog(self):
        font, ok = QFontDialog.getFont()
        if ok:
            print(f'font: {font.pointSize()}')

if __name__ == '__main__':
    app = QApplication(sys.argv)
    demo = DialogDemo()
    demo.show()
    sys.exit(app.exec_())

pyqt5 qdialog

源码下载

https://github.com/xugaoxiang/learningPyQt5

喜欢 (0)

您必须 登录 才能发表评论!