写作思路
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文档