ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

PyQt各类对话框的使用案例

2020-11-28 18:30:14  阅读:277  来源: 互联网

标签:inputBtn 对话框 self PyQt 案例 addWidget progress hLayout def


写作思路

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文档

标签:inputBtn,对话框,self,PyQt,案例,addWidget,progress,hLayout,def
来源: https://blog.csdn.net/weixin_40301728/article/details/110286789

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有