PyQt各类对话框的使用案例

写作思路

1、InputDialog的使用
2、ColorDialog的使用
3、FontDialog的使用
4、FileDialog的使用
5、ProgressDialog的使用

效果展示

在这里插入图片描述

1、InputDialog的使用

# -*- coding: utf-8 -*-
import sys

from PyQt5.QtCore import QTimer
from PyQt5.QtGui import QColor
from PyQt5.QtWidgets import (QWidget, QPushButton, QLineEdit,
                             QInputDialog, QApplication, QVBoxLayout, QHBoxLayout, QFrame, QColorDialog, QLabel,
                             QFontDialog, QFileDialog, QProgressDialog)


class DialogTotal(QWidget):

    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):
        self.vLayout = QVBoxLayout()
        self.inputDialogShow()
        self.colorDialogShow()
        self.fontDialogShow()
        self.fileDialogShow()
        self.progressDialogShow()
        self.setLayout(self.vLayout)
        self.setWindowTitle('对话框集合')
        self.show()

    def inputDialogShow(self):
        hLayout = QHBoxLayout()
        self.vLayout.addLayout(hLayout)
        inputBtn = QPushButton('输入对话框', self)
        hLayout.addWidget(inputBtn)
        inputBtn.clicked.connect(self.showInputDialog)
        self.le = QLineEdit(self)
        hLayout.addWidget(self.le)

    def showInputDialog(self):
        text, ok = QInputDialog.getText(self, 'Input Dialog', 'Enter your name:')
        if ok:
            self.le.setText(str(text))

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

QInputDialog.getText()这个方法返回两个值,一个是设置的字符串、一个是否确认的布尔值,在QInputDialog中还有很多方法 比如getInt() getDouble() 等等 具体自行查找API

2、ColorDialog的使用

    def colorDialogShow(self):
        hLayout = QHBoxLayout()
        self.vLayout.addLayout(hLayout)
        inputBtn = QPushButton('颜色对话框', self)
        hLayout.addWidget(inputBtn)
        inputBtn.clicked.connect(self.showColorDialog)
        col = QColor(0, 0, 0)
        self.frm = QFrame(self)
        self.frm.setStyleSheet("QWidget { background-color: %s }"% col.name())
        hLayout.addWidget(self.frm)

    def showColorDialog(self):
        col = QColorDialog.getColor()
        if col.isValid():
            self.frm.setStyleSheet("QWidget { background-color: %s }"% col.name())

没啥好说的,返回一个颜色值

3、FontDialog的使用


    def fontDialogShow(self):
        hLayout = QHBoxLayout()
        self.vLayout.addLayout(hLayout)
        inputBtn = QPushButton('字体对话框', self)
        hLayout.addWidget(inputBtn)
        inputBtn.clicked.connect(self.showFontDialog)
        self.lable = QLabel('Font Test', self)
        hLayout.addWidget(self.lable)

    def showFontDialog(self):
        font, ok = QFontDialog.getFont()
        if ok:
            self.lable.setFont(font)

用于更换字体,和ColorDialog一样的

4、FileDialog的使用


    def fileDialogShow(self):
        hLayout = QHBoxLayout()
        self.vLayout.addLayout(hLayout)
        inputBtn = QPushButton('文件夹对话框', self)
        hLayout.addWidget(inputBtn)
        inputBtn.clicked.connect(self.showFileDialog)

    def showFileDialog(self):
        fname = QFileDialog.getOpenFileName(self, 'Open file', '/home')
        if fname[0]:
            f = open(fname[0], 'r')
            with f:
                data = f.read()
                self.textEdit.setText(data)

用于打开文件夹

5、ProgressDialog的使用


    def progressDialogShow(self):
        hLayout = QHBoxLayout()
        self.vLayout.addLayout(hLayout)
        inputBtn = QPushButton('进度条对话框', self)
        hLayout.addWidget(inputBtn)
        inputBtn.clicked.connect(self.showprogessDialog)

    def showprogessDialog(self):
        countMin = 0
        countMax = 100
        progress = QProgressDialog("Copying files...", "取消", countMin, countMax, self)
        # progress.setCancelButtonText('cancel')
        progress.setMinimumDuration(0)
        progress.setWindowTitle("进度条")
        progress.setValue(0)
        timer = QTimer(progress)

        def valueChanged():
            if progress.value() + 1 >= progress.maximum() or progress.wasCanceled():
                timer.stop()
            progress.setValue(progress.value() + 1)

        timer.timeout.connect(valueChanged)
        timer.start(100)

获取一个进度条(这样就知道加载到什么程度啦~)
API文档

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值