QSS由白到绿的渐变色
时间: 2024-06-15 08:03:01 浏览: 189
要实现QSS中由白到绿的渐变色,可以使用线性渐变(QLinearGradient)来实现。下面是一个示例代码:
```python
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtGui import QPainter, QLinearGradient, QColor
from PyQt5.QtCore import Qt
class MyWidget(QWidget):
def paintEvent(self, event):
painter = QPainter(self)
gradient = QLinearGradient(0, 0, 0, self.height())
gradient.setColorAt(0, Qt.white)
gradient.setColorAt(1, Qt.green)
painter.setBrush(gradient)
painter.drawRect(self.rect())
if __name__ == '__main__':
app = QApplication([])
widget = MyWidget()
widget.resize(300, 200)
widget.show()
app.exec_()
```
在上面的代码中,我们创建了一个自定义的QWidget子类MyWidget,并重写了它的paintEvent方法。在paintEvent方法中,我们使用QPainter绘制了一个矩形,并使用QLinearGradient设置了从上到下的渐变色。setColorAt方法用于设置渐变色的起始和结束颜色,这里我们将起始颜色设置为白色(Qt.white),结束颜色设置为绿色(Qt.green)。
运行上述代码,会显示一个大小为300x200的窗口,窗口背景色从上到下渐变为白色到绿色。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)