PyQt5实现画布小程序
作者:xiaopengyaonixi 时间:2022-03-02 07:37:46
本文实例为大家分享了PyQt5实现画布小程序的具体代码,供大家参考,具体内容如下
实现的效果图如下:
该实例中,涉及到的知识点有:
1.PyQt5 的常用的布局方式,这里使用到了QVBoxLayout,QHBoxLayout,如何灵活运用这些布局;
2.常用组件的使用方法,这里使用到了QPushButton, QLabel, QPixmap;
3.使用QPainter进行绘制;
4.事件与槽;
主要代码如下:
import random
import sys
from PyQt5.QtCore import QSize
from PyQt5.QtGui import QPixmap, QPainter, QColor
from PyQt5.QtWidgets import QMainWindow, QLabel, QApplication, QPushButton, QWidget, QVBoxLayout, QHBoxLayout
SPRAY_PARTICLES = 100
SPRAY_DIAMMETER = 10
COLORS = [
'#000000', '#141923', '#414168', '#3a7fa7', '#35e3e3', '#8fd970', '#5ebb49',
'#458352', '#dcd37b', '#fffee5', '#ffd035', '#cc9245', '#a15c3e', '#a42f3b',
'#f45b7a', '#c24998', '#81588d', '#bcb0c2', '#ffffff',
]
class QPlatteButton(QPushButton):
def __init__(self, color):
super().__init__()
self.setFixedSize(QSize(24, 24))
self.color = color
self.setStyleSheet("background-color: %s" % self.color)
class Canvas(QLabel):
def __init__(self):
super().__init__()
canvas = QPixmap(1200, 800)
canvas.fill(QColor('white'))
self.setPixmap(canvas)
self.last_x, self.last_y = None, None
self.pen_color = QColor('#000')
def set_pen_color(self, c):
self.pen_color = QColor(c)
def mouseReleaseEvent(self, *args, **kwargs):
"""
松开鼠标事件
"""
self.last_x, self.last_y = None, None
def mouseMoveEvent(self, e):
"""
移动鼠标事件
"""
if self.last_x is None:
self.last_x = e.x()
self.last_y = e.y()
return
painter = QPainter(self.pixmap())
pen = painter.pen()
pen.setWidth(4)
pen.setColor(self.pen_color)
painter.setPen(pen)
painter.drawLine(self.last_x, self.last_y, e.x(), e.y())
painter.end()
self.update()
# update the origin for next time
self.last_x = e.x()
self.last_y = e.y()
# def mouseMoveEvent(self, e):
# painter = QPainter(self.pixmap())
# p = painter.pen()
# p.setWidth(1)
# p.setColor(self.pen_color)
# painter.setPen(p)
#
# for n in range(SPRAY_PARTICLES):
# xo = random.gauss(0, SPRAY_DIAMMETER)
# yo = random.gauss(0, SPRAY_DIAMMETER)
# painter.drawPoint(e.x() + xo, e.y() + yo)
#
# self.update()
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("画板小程序")
self.canvas = Canvas()
widget = QWidget()
vlayout = QVBoxLayout()
widget.setLayout(vlayout)
vlayout.addWidget(self.canvas)
palette = QHBoxLayout()
vlayout.addLayout(palette)
self.add_palette_buttons(palette)
self.setCentralWidget(widget)
def add_palette_buttons(self, layout):
"""
在水平布局中放入一行调色板
"""
for c in COLORS:
b = QPlatteButton(c)
b.pressed.connect(lambda c=c: self.canvas.set_pen_color(c))
layout.addWidget(b)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.move(120, 120)
window.show()
app.exec_()
来源:https://blog.csdn.net/W3Chhhhhh/article/details/105468389
标签:PyQt5,画布
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
tensorflow 模型权重导出实例
2022-10-14 06:25:25
python发送json参数的实例代码
2022-07-26 15:43:47
![](https://img.aspxhome.com/file/2023/6/82676_0s.jpg)
表单name与id的区别和相似之处
2007-12-13 12:55:00
Asp操作Xml的精炼类,含示例代码
2011-02-28 11:11:00
laravel的用户修改密码与绑定邮箱的详细操作
2023-06-17 09:55:06
![](https://img.aspxhome.com/file/2023/5/55535_0s.jpg)
Python通过TensorFlow卷积神经网络实现猫狗识别
2023-09-29 10:59:56
ASP实现最简洁的多重查询的解决方案
2007-09-28 12:00:00
IE下Flash内容刷新后消失问题
2008-01-02 12:38:00
python字典序问题实例
2023-07-31 05:46:58
python线程定时器Timer实现原理解析
2022-01-08 20:25:36
Python基于pygame实现单机版五子棋对战
2021-02-26 05:53:54
![](https://img.aspxhome.com/file/2023/4/65974_0s.jpg)
Python实现消消乐小游戏
2021-02-19 19:37:42
![](https://img.aspxhome.com/file/2023/5/90585_0s.jpg)
Python pass语句作用和Python assert断言函数的用法
2021-03-19 12:14:08
用js限制网页只在微信浏览器中打开(或者只能手机端访问)
2023-09-24 00:11:25
PHP中include和require的使用详解
2023-10-22 03:57:03
使用CSS选择器创建个性化链接样式
2009-06-02 13:07:00
![](https://img.aspxhome.com/file/UploadPic/up/2009060213205633.gif)
python pprint模块中print()和pprint()两者的区别
2023-10-18 07:34:18
![](https://img.aspxhome.com/file/2023/0/63270_0s.png)
安装Python和pygame及相应的环境变量配置(图文教程)
2023-09-05 15:54:42
![](https://img.aspxhome.com/file/2023/9/97049_0s.png)
Python 生成VOC格式的标签实例
2021-09-09 04:39:07
Python Pandas中根据列的值选取多行数据
2023-02-16 04:17:59