当前位置:网站首页>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_())
边栏推荐
- [high concurrency] deeply analyze the execution process of worker threads in the thread pool through the source code
- 大话DevOps监控,团队如何选择监控工具?
- Stm32cubemx learning record -- installation, configuration and use
- Hacker introductory tutorial (very detailed) from zero basic introduction to proficiency, it is enough to read this one.
- Shell Chapter 5 homework
- The principle analysis of filter to solve the request parameter garbled code
- Shell fourth day homework
- [recursion] 938. Range and of binary search tree
- Introduction to shortcut keys in debug chapter
- 第4章线性方程组
猜你喜欢

如何解决“W5500芯片在TCP_Client模式下,断电重启之后无法立即连接到服务器”的问题

【mysql学习09】

Information management system for typical works of urban sculpture (picture sharing system SSM)

Introduction to shortcut keys in debug chapter
苹果美国宣布符合销售免税假期的各州产品清单细节

大话DevOps监控,团队如何选择监控工具?

Breadth first traversal (problems related to sequence traversal of graphs and binary trees)

How to judge the performance of static code quality analysis tools? These five factors must be considered

SQL injection less23 (filter comment)

教你如何通过MCU配置S2E为TCP Client的工作模式
随机推荐
SQL language (I)
Implementation of recommendation system collaborative filtering in spark
黑客入门教程(非常详细)从零基础入门到精通,看完这一篇就够了。
MIIdock简述
数据库完整性——六大约束学习
W5500在处于TCP_Server模式下,在交换机/路由器网络中无法ping通也无法通讯。
varest蓝图设置json
世界上最高效的笔记方法(改变你那老版的记笔记方法吧)
Stm32cubemx learning record -- installation, configuration and use
常见WEB攻击与防御
苹果美国宣布符合销售免税假期的各州产品清单细节
Learn PHP -- phpstudy tips mysqld Exe: Error While Setting Value ‘NO_ ENGINE_ Solution of substitution error
圆角大杀器,使用滤镜构建圆角及波浪效果!
LVS负载均衡之LVS-DR搭建Web群集与LVS结合Keepalived搭建高可用Web群集
[high concurrency] deeply analyze the execution process of worker threads in the thread pool through the source code
大话DevOps监控,团队如何选择监控工具?
SQL language (III)
各种控件==PYQT5
Nowcodertop1-6 - continuous updating
Redis之压缩列表ziplist