PyQt5實(shí)現(xiàn)畫布小程序
本文實(shí)例為大家分享了PyQt5實(shí)現(xiàn)畫布小程序的具體代碼,供大家參考,具體內(nèi)容如下
實(shí)現(xiàn)的效果圖如下:

該實(shí)例中,涉及到的知識(shí)點(diǎn)有:
1.PyQt5 的常用的布局方式,這里使用到了QVBoxLayout,QHBoxLayout,如何靈活運(yùn)用這些布局;
2.常用組件的使用方法,這里使用到了QPushButton, QLabel, QPixmap;
3.使用QPainter進(jìn)行繪制;
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):
"""
松開鼠標(biāo)事件
"""
self.last_x, self.last_y = None, None
def mouseMoveEvent(self, e):
"""
移動(dòng)鼠標(biāo)事件
"""
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):
"""
在水平布局中放入一行調(diào)色板
"""
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_()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python中多進(jìn)程處理的Process和Pool的用法詳解
在Python編程中,多進(jìn)程是一種強(qiáng)大的并行處理技術(shù),Python提供了兩種主要的多進(jìn)程處理方式:Process和Pool,本文將詳細(xì)介紹這兩種方式的使用,希望對(duì)大家有所幫助2024-02-02
Python連接達(dá)夢(mèng)數(shù)據(jù)庫(kù)的實(shí)現(xiàn)示例
本文主要介紹了Python連接達(dá)夢(mèng)數(shù)據(jù)庫(kù)的實(shí)現(xiàn)示例,dmPython是DM提供的依據(jù)Python DB API version 2.0中API使用規(guī)定而開發(fā)的數(shù)據(jù)庫(kù)訪問(wèn)接口,使Python應(yīng)用程序能夠?qū)M數(shù)據(jù)庫(kù)進(jìn)行訪問(wèn)2023-12-12
python學(xué)習(xí) 流程控制語(yǔ)句詳解
下面小編就為大家?guī)?lái)一篇python學(xué)習(xí) 流程控制語(yǔ)句詳解。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-06-06
pandas DataFrame行或列的刪除方法的實(shí)現(xiàn)示例
這篇文章主要介紹了pandas DataFrame行或列的刪除方法的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
pandas的唯一值、值計(jì)數(shù)以及成員資格的示例
今天小編就為大家分享一篇pandas的唯一值、值計(jì)數(shù)以及成員資格的示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
Python JWT認(rèn)證與pyjwt包詳細(xì)介紹
JWT的聲明一般被用來(lái)在身份提供者和服務(wù)提供者間傳遞被認(rèn)證的用戶身份信息,以便于從資源服務(wù)器獲取資源,也增加一些額外的其它業(yè)務(wù)邏輯所必須的聲明信息,該token也可直接被用于認(rèn)證,也可被加密,這篇文章主要介紹了Python JWT認(rèn)證與pyjwt包簡(jiǎn)介,需要的朋友可以參考下2023-05-05
Django 路由層URLconf的實(shí)現(xiàn)
這篇文章主要介紹了Django 路由層URLconf的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12

