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)文章
Python3簡(jiǎn)單爬蟲抓取網(wǎng)頁(yè)圖片代碼實(shí)例
這篇文章主要介紹了Python3簡(jiǎn)單爬蟲抓取網(wǎng)頁(yè)圖片代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08python機(jī)器學(xué)習(xí)案例教程——K最近鄰算法的實(shí)現(xiàn)
本篇文章主要介紹了python機(jī)器學(xué)習(xí)案例教程——K最近鄰算法的實(shí)現(xiàn),詳細(xì)的介紹了K最近鄰算法的概念和示例,具有一定的參考價(jià)值,有興趣的可以了解一下2017-12-12django 中使用DateTime常用的時(shí)間查詢方式
今天小編就為大家分享一篇django 中使用DateTime常用的時(shí)間查詢方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12Python報(bào)錯(cuò)AssertionError:can only test a c
這篇文章主要介紹了Python報(bào)錯(cuò)AssertionError:can only test a child proc問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09Python異步爬蟲實(shí)現(xiàn)原理與知識(shí)總結(jié)
之前有很多小伙伴想看Python異步爬蟲的有關(guān)知識(shí)總結(jié),這次它來(lái)了,文中有非常詳細(xì)的代碼示例與注釋,即使對(duì)剛開始學(xué)python的小伙伴也很友好,,需要的朋友可以參考下2021-05-05