QPainter

功能:QPainter实现在QWidget上画图功能

说明:绘图必须在paintEvent中完成,且要在bengin和end之间作图

接口:

方法 描述
begin 开始画图
end 结束画图
drawLine(int x1, int y1, int x2, int y2) 绘制直线从(x1,y1)到(x2,y2)
drawRect(int x, int y, int w, int h) 绘制矩形起始坐标(x,y),宽度w,高度h
drawText 绘制字符串
FillRect 使用颜色填充矩形
setBrush 设置画刷
setPen 设置画笔

QPen

功能:画笔,主要是设置线条的格式和颜色

QBrush

功能:画刷,主要是设置填充的格式和颜色

例子

import random
import string
import sys from PyQt5.QtCore import Qt, QRect
from PyQt5.QtGui import QPainter, QFont, QColor, QPen, QBrush
from PyQt5.QtWidgets import QApplication, QWidget class MyWidget(QWidget):
def __init__(self):
super(MyWidget, self).__init__() # def paintEvent(self, QPaintEvent):
# painter = QPainter(self)
# painter.begin(self)
# painter.setFont(QFont('Courier New', 20))
# painter.setPen(QColor(168, 34, 3))
# painter.drawText(QPaintEvent.rect(), Qt.AlignCenter, 'hello world')
# painter.end() # def paintEvent(self, QPaintEvent):
# painter = QPainter(self)
# painter.begin(self)
# painter.setFont(QFont('Courier New', 20))
# painter.setPen(QColor(168, 34, 3))
#
# for i in range(10000):
# painter.drawPoint(random.randint(0, 300), random.randint(0, 300))
#
# painter.end() # def paintEvent(self, QPaintEvent):
# painter = QPainter(self)
# painter.begin(self)
#
# pen = QPen(Qt.red, 2, Qt.SolidLine)
# painter.setPen(pen)
# painter.drawLine(20, 20, 400, 20)
#
# pen.setStyle(Qt.DashDotLine)
# painter.setPen(pen)
# painter.drawLine(20, 40, 400, 40)
#
# painter.end() # def paintEvent(self, QPaintEvent):
# painter = QPainter(self)
# painter.begin(self)
#
# brush = QBrush(Qt.SolidPattern)
# brush.setColor(Qt.green)
# painter.setBrush(brush)
# painter.drawRect(20, 20, 200, 100)
#
# brush = QBrush(Qt.HorPattern)
# brush.setColor(Qt.red)
# painter.setBrush(brush)
# painter.drawRect(20, 140, 200, 100)
#
# painter.end() # 实现随机验证码
def paintEvent(self, QPaintEvent):
painter = QPainter(self)
painter.begin(self)
painter.setFont(QFont('Courier New', 50))
randomString = "".join([random.choice(string.digits + string.ascii_letters) for i in range(10)])
for i in range(len(randomString)):
painter.setPen(QColor(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)))
painter.drawText(50 + i * 40, 150, randomString[i]) for i in range(25000):
painter.setPen(QColor(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)))
painter.drawPoint(random.randint(0, 500), random.randint(0, 300)) painter.end() if __name__ == '__main__':
app = QApplication(sys.argv)
w = MyWidget()
w.resize(500, 300)
w.move(300, 300)
w.setWindowTitle('Simple')
w.show()
sys.exit(app.exec_())

PyQt5绘图的更多相关文章

  1. PyQt5绘图API

    PyQt5绘图API大全1.绘图API:绘制文本#1.文本 2.各种图形 3.图像#QPainter painter=QPainter() painter.begin() painter.end() ...

  2. 使用 PyQt5 实现图片查看器

    一.前言 在学习 PyQt5 的过程中我会不断地做一些小的 Demo,用于让自己能够更好地理解和学习,这次要做的就是一个图片查看器,主要功能包括打开图片.拖动图片.放大和缩小图片. 最终实现的图片查看 ...

  3. 用 eric6 与 PyQt5 实现python的极速GUI编程(系列03)---- Drawing(绘图)(2)-- 画点

    [概览] 本文实现如下的程序:(在窗体中绘画出[-100, 100]两个周期的正弦函数图像) 主要步骤如下: 1.在eric6中新建项目,新建窗体 2.(自动打开)进入PyQt5 Desinger,编 ...

  4. 用 eric6 与 PyQt5 实现python的极速GUI编程(系列03)---- Drawing(绘图)(1)-- 绘写文字

    [概览] 本文实现如下的程序:(在窗体中绘画出文字) 主要步骤如下: 1.在eric6中新建项目,新建窗体 2.(自动打开)进入PyQt5 Desinger,编辑图形界面,保存 3.回到eric 6, ...

  5. 用 eric6 与 PyQt5 实现python的极速GUI编程(系列03)---- Drawing(绘图)(3)-- 画线

    [概览] 本文实现如下的程序:(在窗体中绘画出各种不同风格的线条) 主要步骤如下: 1.在eric6中新建项目,新建窗体 2.(自动打开)进入PyQt5 Desinger,编辑图形界面,保存 3.回到 ...

  6. Python3使用PyQt5制作简单的画板/手写板

    0.目录 1.前言 2.简单的画板1.0 在定点和移动中的鼠标所在处画一条线 3.简单的画板2.0 在定点和移动中的鼠标所在处画一条线 并将画过的线都保留在窗体上 4.简单的画板3.0 将按住鼠标后移 ...

  7. PyQt5多点触控写字板实现及困惑

    Qt支持程序多点触控,就想使用PyQt5做一个触控画板,经过几番周折,查阅了TouchEvent官方文档,又参考了一篇QT for Android的例子,采用eventfilter过滤器来识别触屏事件 ...

  8. pyqt5之简单窗口的创建

    在学完tkinter后,发现tkinter在布局方面特别的不方便(Tkinter资料:http://effbot.org/tkinterbook/tkinter-index.htm),因此学习pyqt ...

  9. 一、PyQt5基础概念与安装配置

    一.初识PyQt5 对于桌面程序开发,用户图形界面(GUI)的设计非常重要.一款美观.易用的用户界面可以很大程度上提高对使用这的友好度.由于Python最初是作为脚本语言开发,并没有GUI功能.但Py ...

随机推荐

  1. android自定义控件onMeasure方法

    1.自定义控件首先定义一个类继承View 有时,Android系统控件无法满足我们的需求,因此有必要自定义View.具体方法参见官方开发文档:http://developer.android.com/ ...

  2. Github仓库如何选择开源许可证

    Github仓库如何选择开源许可证 目录 Github仓库如何选择开源许可证 为什么需要开源许可证? 不使用开源许可证对于开发者有何影响? 不使用开源许可证对于项目的使用者有何影响? Github的开 ...

  3. 入门大数据---Spark简介

    一.简介 Spark 于 2009 年诞生于加州大学伯克利分校 AMPLab,2013 年被捐赠给 Apache 软件基金会,2014 年 2 月成为 Apache 的顶级项目.相对于 MapRedu ...

  4. python文件处理-根据csv文件内容,将对应图像拷贝到指定文件夹

    内容涉及:文件遍历,读取csv指定列,拷贝文件,清理和创建文件 # -*- coding: utf-8 -*- import csv import os import sys import numpy ...

  5. vue与react对比总结(一)

    一.react和vue设计上的共同理念 1.使用 Virtual DOM 2.提供了响应式 (Reactive) 和组件化 (Composable) 的视图组件. 3.将注意力集中保持在核心库,而将其 ...

  6. 为什么Spring Security看不见登录失败或者注销的提示

    有很多人在利用Spring Security进行角色权限设计开发时,一般发现正常登录时没问题,但是注销.或者用户名时,直接就回到登录页面了,在登录页面上看不见任何提示信息,如“用户名/密码有误”或“注 ...

  7. 什么是JDK的SPI机制

    什么是SPI和API Application Programming Interface (API)? The API is the description of classes/interfaces ...

  8. 《The Design of a Practical System for Fault-Tolerant Virtual Machines》论文总结

    VM-FT 论文总结 说明:本文为论文 <The Design of a Practical System for Fault-Tolerant Virtual Machines> 的个人 ...

  9. Redis基础01-redis的数据结构

    参考书:<redis设计与实现> Redis虽然底层是用C语言写的,但是底层的数据结构并不是直接使用C语言的数据结构,而是自己单独封装的数据结构: Redis的底层数据结构由,简单动态字符 ...

  10. Codeforces 1215D Ticket Game 题解

    Codeforces 1215D Ticket Game 原题 题目 Monocarp and Bicarp live in Berland, where every bus ticket consi ...