python实现简单画板_python画板⽰例源码(可做签名涂鸦)【实例简介】
【实例截图】
【核⼼代码】
'''
Created on 2018年8⽉8⽇
@author: Freedom
'''
from PyQt5.Qt import QWidget, QColor, QPixmap, QIcon, QSize, QCheckBox
from PyQt5.QtWidgets import QHBoxLayout, QVBoxLayout, QPushButton, QSplitter,\
QComboBox, QLabel, QSpinBox, QFileDialog
from PaintBoard import PaintBoard
linux怎么下载安装软件class MainWidget(QWidget):
def __init__(self, Parent=None):
'''
Constructor
'''
super().__init__(Parent)
self.__InitData() #先初始化数据,再初始化界⾯
self.__InitView()
def __InitData(self):
'''
初始化成员变量
'''
self.__paintBoard = PaintBoard(self)
self.__colorList = lorNames() #获取颜⾊列表(字符串类型)
def __InitView(self):
'''
初始化界⾯
'''
self.setFixedSize(640,480)
self.setWindowTitle("PaintBoard Example PyQt5")
main_layout = QHBoxLayout(self) #新建⼀个⽔平布局作为本窗体的主布局
main_layout.setSpacing(10) #设置主布局内边距以及控件间距为10px
main_layout.addWidget(self.__paintBoard) #在主界⾯左侧放置画板
sub_layout = QVBoxLayout() #新建垂直⼦布局⽤于放置按键
sub_layout.setContentsMargins(10, 10, 10, 10) #设置此⼦布局和内部控件的间距为10px
self.__btn_Clear = QPushButton("清空画板")
self.__btn_Clear.setParent(self) #设置⽗对象为本界⾯
self.__btn_t(self.__paintBoard.Clear) #将按键按下信号与画板清空函数相关联
sub_layout.addWidget(self.__btn_Clear)
self.__btn_Quit = QPushButton("退出")
网页下载不了软件怎么办self.__btn_Quit.setParent(self) #设置⽗对象为本界⾯
self.__btn_t(self.Quit)
sub_layout.addWidget(self.__btn_Quit)
self.__btn_Save = QPushButton("保存作品")
self.__btn_Save.setParent(self)
self.__btn__btn_Save_Clicked)
sub_layout.addWidget(self.__btn_Save)
self.__cbtn_Eraser = QCheckBox(" 使⽤橡⽪擦")
self.__cbtn_Eraser.setParent(self)
self.__cbtn__cbtn_Eraser_clicked)
sub_layout.addWidget(self.__cbtn_Eraser)
splitter = QSplitter(self) #占位符
sub_layout.addWidget(splitter)
self.__label_penThickness = QLabel(self)
self.__label_penThickness.setText("画笔粗细")
self.__label_penThickness.setFixedHeight(20)
unity官方下载sub_layout.addWidget(self.__label_penThickness)
self.__spinBox_penThickness = QSpinBox(self)
self.__spinBox_penThickness.setMaximum(20)
self.__spinBox_penThickness.setMinimum(2)
self.__spinBox_penThickness.setValue(10) #默认粗细为10
self.__spinBox_penThickness.setSingleStep(2) #最⼩变化值为1
self.__spinBox__PenThicknessChange)#关联spinBox值变化信号和函数on_PenThicknessChange
sub_layout.addWidget(self.__spinBox_penThickness)
self.__label_penColor = QLabel(self)
self.__label_penColor.setText("画笔颜⾊")
self.__label_penColor.setFixedHeight(20)
sub_layout.addWidget(self.__label_penColor)
self.__comboBox_penColor = QComboBox(self)
self.__fillColorList(self.__comboBox_penColor) #⽤各种颜⾊填充下拉列表
self.__comboBox__PenColorChange) #关联下拉列表的当前索引变更信号与函数on_PenColorChange
sub_layout.addWidget(self.__comboBox_penColor)
main_layout.addLayout(sub_layout) #将⼦布局加⼊主布局
def __fillColorList(self, comboBox):
免费app源码index_black = 0
index = 0
for color in self.__colorList:
if color == "black":
index_black = index
index = 1
pix = QPixmap(70,20)
pix.fill(QColor(color))
comboBox.addItem(QIcon(pix),None)
comboBox.setIconSize(QSize(70,20))
comboBox.setSizeAdjustPolicy(QComboBox.AdjustToContents)
comboBox.setCurrentIndex(index_black)
def on_PenColorChange(self):
color_index = self.__comboBox_penColor.currentIndex()python代码画图案
color_str = self.__colorList[color_index]
self.__paintBoard.ChangePenColor(color_str)
def on_PenThicknessChange(self):新乡模板建站
penThickness = self.__spinBox_penThickness.value()
self.__paintBoard.ChangePenThickness(penThickness)
def on_btn_Save_Clicked(self):
savePath = SaveFileName(self, 'Save Your Paint', '.\\', '*.png')
print(savePath)
if savePath[0] == "":
print("Save cancel")
return
image = self.__paintBoard.GetContentAsQImage() image.save(savePath[0])
def on_cbtn_Eraser_clicked(self):
if self.__cbtn_Eraser.isChecked():
self.__paintBoard.EraserMode = True #进⼊橡⽪擦模式else:
self.__paintBoard.EraserMode = False #退出橡⽪擦模式def Quit(self):
self.close()
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论