当前位置:网站首页>Plot ==pyqt5
Plot ==pyqt5
2022-07-25 11:39:00 【Brother Dong's cultivation diary】
- PyQt5 The drawing system can present vector graphics , Images , And outline font-based Text .
- We can also call the system in the program api Custom drawing controls .
- The drawing should be in paintEvent() Method implementation
- stay QPainter Object's begin() And end() Methods to write drawing code . It will perform low-level graphics rendering on controls or other graphics devices .
Draw text
- Let's start with a window Unicode Text drawing as an example .
- Example , Let's draw some Cylliric Text . Align text vertically and horizontally .
import sys
from PyQt5.QtWidgets import QWidget, QApplication
from PyQt5.QtGui import QPainter, QColor, QFont
from PyQt5.QtCore import Qt
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.text = u'\u041b\u0435\u0432 \u041d\u0438\u043a\u043e\u043b\u0430\
\u0435\u0432\u0438\u0447 \u0422\u043e\u043b\u0441\u0442\u043e\u0439: \n\
\u0410\u043d\u043d\u0430 \u041a\u0430\u0440\u0435\u043d\u0438\u043d\u0430'
self.setGeometry(300, 300, 280, 170)
self.setWindowTitle('Draw text')
self.show()
def paintEvent(self, event):# Drawing works in paintEvent Method is completed internally .
#QPainter Class is responsible for all elementary drawing . Between all the painting methods to start() and end() Method . The actual painting was entrusted to drawText() Method .
qp = QPainter()
qp.begin(self)
self.drawText(event, qp)
qp.end()
def drawText(self, event, qp):
qp.setPen(QColor(168, 34, 3))# Define a brush and a font for drawing text .
qp.setFont(QFont('Decorative', 10))
#drawText() Method to draw text on the form , Displayed in the center
qp.drawText(event.rect(), Qt.AlignCenter, self.text)
Draw a dot
- Point is the simplest graphic object that can be drawn .
- We drew randomly on the window 1000 A red dot
import sys, random
from PyQt5.QtWidgets import QWidget, QApplication
from PyQt5.QtGui import QPainter, QColor, QPen
from PyQt5.QtCore import Qt
class Example1(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(300, 300, 280, 170)# main window
self.setWindowTitle('Points')
self.show()
def paintEvent(self, e):
qp = QPainter()
qp.begin(self)
self.drawPoints(qp)
qp.end()
def drawPoints(self, qp):
qp.setPen(Qt.red)# Set the brush to red , We use the predefined Qt.red Constant
size = self.size()# Next time we change the size of the window , Generate a paint event event , Set the brush to red , We use the predefined Qt.red Constant
for i in range(1000):
x = random.randint(1, size.width() - 1)
y = random.randint(1, size.height() - 1)
qp.drawPoint(x, y)# adopt drawpoint Draw the dot
Color
- Color is an object that represents red 、 green 、 Blue intensity value RGB24
- In the example, we draw 3 A rectangle of different colors
import sys
from PyQt5.QtWidgets import QWidget, QApplication
from PyQt5.QtGui import QPainter, QColor, QBrush
class Example2(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(300, 300, 350, 100)
self.setWindowTitle('Colours')
self.show()
def paintEvent(self, e):
qp = QPainter()
qp.begin(self)
self.drawRectangles(qp)
qp.end()
def drawRectangles(self, qp):
col = QColor(0, 0, 0)
col.setNamedColor('#d4d4d4')# We define a color that uses hexadecimal symbols .
qp.setPen(col)
# We are QPainter Set a brush (Bursh) Object and draw a rectangle with it
#drawRect() Method accepts four parameters , The first two are the starting points x, y coordinate , The last two are the width and height of the rectangle
qp.setBrush(QColor(200, 0, 0))
qp.drawRect(10, 15, 90, 60)
qp.setBrush(QColor(255, 80, 0, 160))
qp.drawRect(130, 15, 90, 60)
qp.setBrush(QColor(25, 0, 90, 200))
qp.drawRect(250, 15, 90, 60)
OPen( paint brush )
- QPen Is a basic graphic object . Used to draw lines 、 Rectangles of curves and contours 、 The ellipse 、 Polygons or other shapes .
- They draw six lines . The lines outline six different pen styles . There are five predefined pen styles . We can also create custom pen styles . The last line uses a custom pen drawing style .
import sys
from PyQt5.QtWidgets import QWidget, QApplication
from PyQt5.QtGui import QPainter, QColor, QPen
from PyQt5.QtCore import Qt
class Example3(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(300, 300, 280, 270)
self.setWindowTitle('Pen styles')
self.show()
def paintEvent(self, e):
qp = QPainter()
qp.begin(self)
self.drawLines(qp)
qp.end()
def drawLines(self, qp):
pen = QPen(Qt.black, 2, Qt.SolidLine)# Create a QPen object . The color is black . The width is set to 2 Pixels , In parentheses is the style of the pen
qp.setPen(pen)
qp.drawLine(20, 40, 250, 40)
pen.setStyle(Qt.DashLine)
qp.setPen(pen)
qp.drawLine(20, 80, 250, 80)
pen.setStyle(Qt.DashDotLine)
qp.setPen(pen)
qp.drawLine(20, 120, 250, 120)
pen.setStyle(Qt.DotLine)
qp.setPen(pen)
qp.drawLine(20, 160, 250, 160)
pen.setStyle(Qt.DashDotDotLine)
qp.setPen(pen)
qp.drawLine(20, 200, 250, 200)
# Here we define a brush style .
pen.setStyle(Qt.CustomDashLine)
# Here we define 1 Solid lines of pixels ,4 White space in pixels ,5 Pixel solid line ,4 Pixel blank
pen.setDashPattern([1, 4, 5, 4])# Set up Qt.CustomDashLine And called setDashPattern() Method , Its parameters ( A list of numbers ) Defines a style , There must be an even number of digits ; Where an odd number means to draw a solid line , An even number means to leave blank . The greater the numerical
qp.setPen(pen)
qp.drawLine(20, 240, 250, 240)
OBrush( Brush )
- QBrush Is a basic graphic object
- It is used to paint the background graphic shape , Like a rectangle 、 Oval or polygon
- Three different types of brushes can : A predefined brush , A gradient , Or texture mode .
- In the example, nine different rectangles are drawn
import sys
from PyQt5.QtWidgets import QWidget, QApplication
from PyQt5.QtGui import QPainter, QBrush
from PyQt5.QtCore import Qt
class Example4(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(300, 300, 355, 280)
self.setWindowTitle('Brushes')
self.show()
def paintEvent(self, e):
qp = QPainter()
qp.begin(self)
self.drawBrushes(qp)
qp.end()
def drawBrushes(self, qp):
brush = QBrush(Qt.SolidPattern)
qp.setBrush(brush)
qp.drawRect(10, 15, 90, 60)
brush.setStyle(Qt.Dense1Pattern)
qp.setBrush(brush)
qp.drawRect(130, 15, 90, 60)
brush.setStyle(Qt.Dense2Pattern)
qp.setBrush(brush)
qp.drawRect(250, 15, 90, 60)
brush.setStyle(Qt.DiagCrossPattern)
qp.setBrush(brush)
qp.drawRect(10, 105, 90, 60)
brush.setStyle(Qt.Dense5Pattern)
qp.setBrush(brush)
qp.drawRect(130, 105, 90, 60)
brush.setStyle(Qt.Dense6Pattern)
qp.setBrush(brush)
qp.drawRect(250, 105, 90, 60)
brush.setStyle(Qt.HorPattern)
qp.setBrush(brush)
qp.drawRect(10, 195, 90, 60)
brush.setStyle(Qt.VerPattern)
qp.setBrush(brush)
qp.drawRect(130, 195, 90, 60)
# We define a brush object , Then set it to QPainter object , And call painter Of drawRect() Method to draw a rectangle
brush.setStyle(Qt.BDiagPattern)
qp.setBrush(brush)
qp.drawRect(250, 195, 90, 60)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example4()
sys.exit(app.exec_())
边栏推荐
- 数据库完整性——六大约束学习
- 如何解决“W5500芯片在TCP_Client模式下,断电重启之后无法立即连接到服务器”的问题
- Implementation of recommendation system collaborative filtering in spark
- Some errors of tensorflow calling multiple GPUs
- Redis之压缩列表ziplist
- My colleague looked at my code and exclaimed: how can I use a singleton in unity
- SQL language (6)
- Shell - Chapter 8 exercise
- 一篇看懂:IDEA 使用scala 编写wordcount程序 并生成jar包 实测
- 第一个C语言程序(从Hello World开始)
猜你喜欢

Only know that the preform is used to generate objects? See how I use unity to generate UI prefabs

Game backpack system, "inventory Pro plug-in", research and learning ----- mom doesn't have to worry that I won't make a backpack anymore (unity3d)

Emmet syntax quick query syntax basic syntax part

SQL injection less17 (error injection + subquery)

SQL注入 Less18(头部注入+报错注入)

There is a newline problem when passing shell script parameters \r

SQL language (II)
Details of the list of state products that Apple announced to be eligible for the sales tax holiday in the United States

工作面试总遇秒杀?看了京东T8大咖私藏的秒杀系统笔记,已献出膝盖
信息熵的定义
随机推荐
Breadth first traversal (problems related to sequence traversal of graphs and binary trees)
Txt to CSV file, blank lines appear every other line
SQL注入 Less17(报错注入+子查询)
小微企业智能名片管理小程序
Leetcode sword finger offer 27. image of binary tree
Detailed explanation of lvs-nat and lvs-dr modes of LVS load balancing
LVS load balancing lvs-dr builds Web Clusters and LVS combines with kept to build highly available Web Clusters
Nowcodertop12-16 - continuous updating
常见WEB攻击与防御
shell-第五章作业
【mysql学习09】
Multiply Floyd "suggestions collection"
动态规划问题05_导弹拦截
第一个C语言程序(从Hello World开始)
Emmet syntax quick query syntax basic syntax part
LVS负载均衡之LVS-NAT搭建Web群集
Nowcodertop1-6 - continuous updating
Shell Chapter 7 exercise
Esp8266 uses drv8833 drive board to drive N20 motor
相似矩阵,可对角化条件