B站: 迷途小书童的Note;微信公众号: Dev_Club;个人微信:xituxiaoshutong100

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

PyQt5开发 迷途小书童 0评论

软硬件环境

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

视频看这里

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

简介

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

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_())

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_())

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_())

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_())

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_())

qdialog

源码下载

https://github.com/xugaoxiang/learningPyQt5

喜欢 (0)
发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址