当前位置:网站首页>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_())
边栏推荐
- The most efficient note taking method in the world (change your old version of note taking method)
- cookie and session
- RedisUtil
- Dynamic planning problem 03_ Maximum sub segment sum
- ESP8266 使用 DRV8833驱动板驱动N20电机
- [tree] 100. Same tree
- How does the whole network display IP ownership?
- 常见WEB攻击与防御
- LVS负载均衡之LVS-DR搭建Web群集与LVS结合Keepalived搭建高可用Web群集
- Dataframe print ellipsis problem
猜你喜欢

Some usages of beautifulsoup

The B2B2C multi merchant system has rich functions and is very easy to open!!!

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

ArcMap cannot start the solution

只知道预制体是用来生成物体的?看我如何使用Unity生成UI预制体

Leetcode sword finger offer 27. image of binary tree

SQL injection less17 (error injection + subquery)

leetcode 剑指 Offer 28. 对称的二叉树

2022 年中回顾|一文看懂预训练模型最新进展

模型部署简述
随机推荐
[recursion] 938. Range and of binary search tree
Learn Luzhi PHP -- tp5.0 uses Chinese as an alias and reports "unsupported data expression"
一篇看懂:IDEA 使用scala 编写wordcount程序 并生成jar包 实测
flinksql client 连接kafka select * from table没有数据报错,如何解决?
Mlx90640 infrared thermal imager temperature measurement module development notes (V)
Information management system for typical works of urban sculpture (picture sharing system SSM)
Why should the hashcode () method be rewritten when rewriting the equals () method
Linked list related (design linked list and ring linked list)
MIIdock简述
各种控件==PYQT5
菜单栏+状态栏+工具栏==PYQT5
My colleague looked at my code and exclaimed: how can I use a singleton in unity
新能源销冠宏光MINIEV,有着怎样的产品力?
Small and micro enterprise smart business card management applet
苹果美国宣布符合销售免税假期的各州产品清单细节
工作面试总遇秒杀?看了京东T8大咖私藏的秒杀系统笔记,已献出膝盖
Shell Chapter 7 exercise
Fillet big killer, use filter to build fillet and wave effect!
Redis 入门
[tree] 100. Same tree