In this PyQt5 article iam going to have Introduction To PyQt5 QLinearGradient Color
also we are going to make an example for the QLinearGradient Color in PyQt5, there are three different kinds of Gradient Color in PyQt5, we have QLinearGradient, QRadialGradient and QConicalGradient. so first of all what is QLinearGradient ? according to Qt Documentation The QLinearGradient class is used in combination with QBrush to specify a linear gradient brush. Linear gradients interpolate colors between start and end points. Outside these points the gradient is either padded, reflected or repeated depending on the currently set spread method.
So first of all you can check my some articles on QPainter
1: PyQt5 Drawing Rectangle With QPainter Class
2: PyQt5 Drawing Ellipse With QPainter Class
3: PyQt5 Drawing Polygon With QPainter Class
Also you can check more Python GUI articles in the below links
1: Kivy GUI Development Tutorials
2: TKinter GUI Development Tutorials
4: wxPython GUI Development Tutorials
5: PyQt5 GUI Development Tutorials
So now this is the complete source code for Introduction To PyQt5 QLinearGradient Color
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
from PyQt5 import QtGui from PyQt5.QtWidgets import QApplication, QMainWindow import sys from PyQt5.QtGui import QPainter, QBrush, QPen, QLinearGradient from PyQt5.QtCore import Qt class Window(QMainWindow): def __init__(self): super().__init__() self.title = "PyQt5 Window" self.top = 200 self.left = 500 self.width = 400 self.height = 300 self.InitWindow() def InitWindow(self): self.setWindowIcon(QtGui.QIcon("icon.png")) self.setWindowTitle(self.title) self.setGeometry(self.left, self.top, self.width, self.height) self.show() def paintEvent(self, e): painter = QPainter(self) painter.setPen(QPen(Qt.black, 4, Qt.SolidLine)) grad1 = QLinearGradient(25, 100, 150, 175) grad1.setColorAt(0.0, Qt.darkGray) grad1.setColorAt(0.5, Qt.green) grad1.setColorAt(1.0, Qt.yellow) painter.setBrush(QBrush(grad1)) painter.drawRect(10, 10, 200, 200) App = QApplication(sys.argv) window = Window() sys.exit(App.exec()) |
So this is the important line of code in QPainter class, when you want to draw you need create a built in method of def paintEvent(), the purpose of this method is for drawing shapes, so first we have created the object of QPainter class and after that we have created the QLinearGradient object and we set the color position for the Linear Gradient at the end we have drawn our rectangle.
1 2 3 4 5 6 7 8 9 10 11 12 |
def paintEvent(self, e): painter = QPainter(self) painter.setPen(QPen(Qt.black, 4, Qt.SolidLine)) grad1 = QLinearGradient(25, 100, 150, 175) grad1.setColorAt(0.0, Qt.darkGray) grad1.setColorAt(0.5, Qt.green) grad1.setColorAt(1.0, Qt.yellow) painter.setBrush(QBrush(grad1)) painter.drawRect(10, 10, 200, 200) |
Run the complete code and this will be the result.
Also you can watch the complete video for this article.
Subscribe and Get Free Video Courses & Articles in your Email
Comments are closed.