在 PyQt4 中,QLinearGradient
获取水平方向。虽然 PySide 似乎处理它就像它是一个垂直渐变。该代码通过QLineEdit
设置背景颜色创建一个palette.setBrush(QPalette.Base, QBrush(QLinearGradient))
。按钮推动将进度条值提高 10%。
from PyQt4 import QtCore, QtGui
import time
class Dialog(QtGui.QDialog):
value = 0.001
def __init__(self, parent=None):
QtGui.QDialog .__init__(self, parent)
mainLayout = QtGui.QVBoxLayout()
self.lineedit = QtGui.QLineEdit()
self.setValues()
mainLayout.addWidget(self.lineedit)
button = QtGui.QPushButton('Calculate')
button.clicked.connect(self.buttonClicked)
mainLayout.addWidget(button)
self.setLayout(mainLayout)
def setValues(self):
self.lineedit.setText('progress %s'%self.value)
palette = self.lineedit.palette()
QRectF = QtCore.QRectF(self.lineedit.rect())
gradient = QtGui.QLinearGradient(QRectF.topLeft(), QRectF.topRight())
gradient.setColorAt(self.value-0.001, QtGui.QColor('#f99e41'))
gradient.setColorAt(self.value, QtGui.QColor('#ffffff'))
gradient.setColorAt(self.value+0.001, QtGui.QColor('#ffffff'))
palette.setBrush(QtGui.QPalette.Base, QtGui.QBrush(gradient))
self.lineedit.setPalette(palette)
def buttonClicked(self):
if self.value >0.9:
self.value = 0.001
else:
self.value=self.value+0.1
self.setValues()
time.sleep(1)
if __name__ == '__main__':
app = QtGui.QApplication([])
window = Dialog()
window.resize(300, 50)
window.show()
app.exec_()
对于渐变可能是水平的 PySide:
import PySide.QtCore as QtCore
import PySide.QtGui as QtGui
class Dialog(QtGui.QDialog):
value = 1.00
def __init__(self, parent=None):
QtGui.QDialog .__init__(self, parent)
mainLayout = QtGui.QVBoxLayout()
self.lineedit = QtGui.QLineEdit()
self.setValues()
mainLayout.addWidget(self.lineedit)
button = QtGui.QPushButton('Calculate')
button.clicked.connect(self.buttonClicked)
mainLayout.addWidget(button)
self.setLayout(mainLayout)
def setValues(self):
self.lineedit.setText('progress %s'%self.value)
palette = self.lineedit.palette()
QRectF = QtCore.QRectF(self.lineedit.rect())
gradient = QtGui.QLinearGradient(QRectF.topLeft(), QRectF.topRight())
gradient.setColorAt(self.value-0.001, QtGui.QColor('#ffffff'))
gradient.setColorAt(self.value, QtGui.QColor('#f99e41'))
gradient.setColorAt(self.value+0.001, QtGui.QColor('#f99e41'))
palette.setBrush(QtGui.QPalette.Base, QtGui.QBrush(gradient))
self.lineedit.setPalette(palette)
def buttonClicked(self):
if self.value <0.1:
self.value = 1.00
else:
self.value=self.value-0.1
self.setValues()
if __name__ == '__main__':
app = QtGui.QApplication([])
window = Dialog()
window.resize(300, 50)
window.show()
app.exec_()