class MainWidget(QWidget): def __init__(self, Parent=None): ''' Constructor ''' super().__init__(Parent) self.__InitData() #先初始化数据,再初始化界面 self.__InitView() self.svmModel = model.loadModel() def __InitData(self): ''' 初始化成员变量 ''' self.__paintBoard = PaintBoard(self) #获取颜色列表(字符串类型) self.__colorList = QColor.colorNames() def __InitView(self): ''' 初始化界面 ''' self.setFixedSize(640, 480) self.setWindowTitle("PaintBoard Example PyQt5") #新建一个水平布局作为本窗体的主布局 main_layout = QHBoxLayout(self) #设置主布局内边距以及控件间距为10px main_layout.setSpacing(10) #在主界面左侧放置画板 main_layout.addWidget(self.__paintBoard) #新建垂直子布局用于放置按键 sub_layout = QVBoxLayout() #设置此子布局和内部控件的间距为10px sub_layout.setContentsMargins(10, 10, 10, 10) self.__btn_Clear = QPushButton("清空画板") self.__btn_Clear.setParent(self) #设置父对象为本界面 self.__btn_Clear.clicked.connect(self.on_clear) #将按键按下信号与画板清空函数相关联 sub_layout.addWidget(self.__btn_Clear) self.__btn_Save = QPushButton("识别") self.__btn_Save.setParent(self) self.__btn_Save.clicked.connect(self.on_btn_Save_Clicked) sub_layout.addWidget(self.__btn_Save) self.__text_predict = QLineEdit(self) sub_layout.addWidget(self.__text_predict) self.__cbtn_Eraser = QCheckBox(" 使用橡皮擦") self.__cbtn_Eraser.setParent(self) self.__cbtn_Eraser.clicked.connect(self.on_cbtn_Eraser_clicked) sub_layout.addWidget(self.__cbtn_Eraser) splitter = QSplitter(self) #占位符 sub_layout.addWidget(splitter) self.__btn_Quit = QPushButton("退出") self.__btn_Quit.setParent(self) #设置父对象为本界面 self.__btn_Quit.clicked.connect(self.Quit) sub_layout.addWidget(self.__btn_Quit) self.__label_penThickness = QLabel(self) self.__label_penThickness.setText("画笔粗细") self.__label_penThickness.setFixedHeight(20) 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) #最小变化值为2 self.__spinBox_penThickness.valueChanged.connect( self.on_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_penColor.currentIndexChanged.connect( self.on_PenColorChange) #关联下拉列表的当前索引变更信号与函数on_PenColorChange sub_layout.addWidget(self.__comboBox_penColor) main_layout.addLayout(sub_layout) #将子布局加入主布局 def __fillColorList(self, comboBox): 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() 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_clear(self): self.__paintBoard.Clear() self.__text_predict.setText("") def on_btn_Save_Clicked(self): image = self.__paintBoard.GetContentAsQImage() image.save("tmpimageforsvm.png") img = cv2.imread("tmpimageforsvm.png") img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) img = cv2.resize(img, (28, 28)) Array = np.empty((1, 28 * 28)) Array[0] = img.reshape((1, 28 * 28)) ret = self.svmModel.predict(model.reNormalize(Array)) print(ret) self.__text_predict.setText("预测值{}".format(int(ret))) 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()
class write_num(QWidget): def __init__(self): super(write_num, self).__init__() self.__InitData() # 先初始化数据,再初始化界面 self.__InitView() def __InitData(self): ''' 初始化成员变量 ''' self.__paintBoard = PaintBoard(self) # 获取颜色列表(字符串类型) self.__colorList = QColor.colorNames() def __InitView(self): ''' 初始化界面 ''' self.setWindowIcon(QtGui.QIcon('./icon.jpg')) self.resize(640, 600) self.setFixedSize(self.width(), self.height()) self.setWindowTitle("手写数字识别") self.label_name = QLabel('哔哩哔哩大学', self) self.label_name.setGeometry(500, 5, 120, 30) self.label_name = QLabel('知识学院', self) self.label_name.setGeometry(500, 35, 100, 30) self.label_name = QLabel('野生技术协会', self) self.label_name.setGeometry(500, 65, 100, 30) self.label_name = QLabel('王荣胜', self) self.label_name.setGeometry(500, 95, 100, 30) self.edit = QTextEdit(self) self.edit.setGeometry(510, 160, 110, 60) # 新建一个水平布局作为本窗体的主布局 main_layout = QHBoxLayout(self) # 设置主布局内边距以及控件间距为10px main_layout.setSpacing(10) # 在主界面左侧放置画板 main_layout.addWidget(self.__paintBoard) # 新建垂直子布局用于放置按键 sub_layout = QVBoxLayout() # 设置此子布局和内部控件的间距为5px sub_layout.setContentsMargins(5, 5, 5, 5) splitter = QSplitter(self) # 占位符 sub_layout.addWidget(splitter) self.__btn_Recognize = QPushButton("开始识别") self.__btn_Recognize.setParent(self) self.__btn_Recognize.clicked.connect(self.on_btn_Recognize_Clicked) sub_layout.addWidget(self.__btn_Recognize) self.__btn_Clear = QPushButton("清空画板") self.__btn_Clear.setParent(self) # 设置父对象为本界面 # 将按键按下信号与画板清空函数相关联 self.__btn_Clear.clicked.connect(self.__paintBoard.Clear) sub_layout.addWidget(self.__btn_Clear) self.__btn_return = QPushButton("返回") self.__btn_return.setParent(self) # 设置父对象为本界面 self.__btn_return.clicked.connect(self.slot_btn_function) sub_layout.addWidget(self.__btn_return) self.__btn_Quit = QPushButton("退出") self.__btn_Quit.setParent(self) # 设置父对象为本界面 self.__btn_Quit.clicked.connect(self.Quit) sub_layout.addWidget(self.__btn_Quit) self.__btn_Save = QPushButton("保存作品") self.__btn_Save.setParent(self) self.__btn_Save.clicked.connect(self.on_btn_Save_Clicked) sub_layout.addWidget(self.__btn_Save) self.__cbtn_Eraser = QCheckBox("使用橡皮擦") self.__cbtn_Eraser.setParent(self) self.__cbtn_Eraser.clicked.connect(self.on_cbtn_Eraser_clicked) sub_layout.addWidget(self.__cbtn_Eraser) self.__label_penThickness = QLabel(self) self.__label_penThickness.setText("画笔粗细") self.__label_penThickness.setFixedHeight(20) 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) # 最小变化值为2 self.__spinBox_penThickness.valueChanged.connect(self.on_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_penColor.currentIndexChanged.connect(self.on_PenColorChange) # 关联下拉列表的当前索引变更信号与函数on_PenColorChange sub_layout.addWidget(self.__comboBox_penColor) main_layout.addLayout(sub_layout) # 将子布局加入主布局 def __fillColorList(self, comboBox): 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() 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 = QFileDialog.getSaveFileName(self, 'Save Your Paint', '.\\', '*.png') print(savePath) if savePath[0] == "": print("Save cancel") return image = self.__paintBoard.GetContentAsQImage() image.save(savePath[0]) print(savePath[0]) def Quit(self): self.close() def on_cbtn_Eraser_clicked(self): if self.__cbtn_Eraser.isChecked(): self.__paintBoard.EraserMode = True # 进入橡皮擦模式 else: self.__paintBoard.EraserMode = False # 退出橡皮擦模式 def on_btn_Recognize_Clicked(self): config = tf.compat.v1.ConfigProto(allow_soft_placement=True) config.gpu_options.per_process_gpu_memory_fraction = 0.3 tf.compat.v1.keras.backend.set_session(tf.compat.v1.Session(config=config)) savePath = "./text.png" image = self.__paintBoard.GetContentAsQImage() image.save(savePath) print(savePath) # 加载图像 img = keras.preprocessing.image.load_img(savePath, target_size=(28, 28)) img = img.convert('L') x = keras.preprocessing.image.img_to_array(img) x = abs(255-x) #x = x.reshape(28,28) x = np.expand_dims(x, axis=0) x=x/255.0 new_model = keras.models.load_model('./my_model.h5') prediction = new_model.predict(x) output = np.argmax(prediction, axis=1) #print("手写数字识别为:" + str(output[0])) self.edit.setText('识别的手写数字为:' + str(output[0])) def slot_btn_function(self): self.hide() # 隐藏此窗口 self.f = FirstUi() # 将第一个窗口换个名字 self.f.show() # 将第一个窗口显示出来
class MainWidget(QWidget): signal_cut = pyqtSignal(str) def __init__(self,sizeX,sizeY, Parent=None): ''' Constructor ''' super().__init__(Parent) self.sizeX = sizeX self.sizeY = sizeY self.__InitData() # 先初始化数据,再初始化界面 self.__InitView() def refresh(self): QApplication.processEvents() self.__InitData() # 先初始化数据,再初始化界面 self.__InitView() def __InitData(self): ''' 初始化成员变量 ''' self.paintBoard = PaintBoard(self.sizeX, self.sizeY) # 获取颜色列表(字符串类型) self.__colorList = QColor.colorNames() def __InitView(self): ''' 初始化界面 ''' self.setFixedSize(640-480+self.sizeX, 480-460+self.sizeY) self.setWindowTitle("切割") # 新建一个水平布局作为本窗体的主布局 main_layout = QHBoxLayout(self) # 设置主布局内边距以及控件间距为10px main_layout.setSpacing(10) # 在主界面左侧放置画板 main_layout.addWidget(self.paintBoard) # 新建垂直子布局用于放置按键 sub_layout = QVBoxLayout() # 设置此子布局和内部控件的间距为10px sub_layout.setContentsMargins(10, 10, 10, 10) self.__btn_Clear = QPushButton("清空画板") self.__btn_Clear.setParent(self) # 设置父对象为本界面 # 将按键按下信号与画板清空函数相关联 self.__btn_Clear.clicked.connect(self.paintBoard.Clear) sub_layout.addWidget(self.__btn_Clear) self.__btn_Quit = QPushButton("确认") self.__btn_Quit.setParent(self) # 设置父对象为本界面 self.__btn_Quit.clicked.connect(self.Quit) sub_layout.addWidget(self.__btn_Quit) self.__btn_Save = QPushButton("保存作品") self.__btn_Save.setParent(self) self.__btn_Save.clicked.connect(self.on_btn_Save_Clicked) sub_layout.addWidget(self.__btn_Save) self.__cbtn_Eraser = QCheckBox(" 使用橡皮擦") self.__cbtn_Eraser.setParent(self) self.__cbtn_Eraser.clicked.connect(self.on_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(50) sub_layout.addWidget(self.__label_penThickness) self.__spinBox_penThickness = QSpinBox(self) self.__spinBox_penThickness.setMaximum(100) self.__spinBox_penThickness.setMinimum(2) self.__spinBox_penThickness.setValue(10) # 默认粗细为10 self.__spinBox_penThickness.setSingleStep(2) # 最小变化值为2 self.__spinBox_penThickness.valueChanged.connect( self.on_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_penColor.currentIndexChanged.connect( self.on_PenColorChange) # 关联下拉列表的当前索引变更信号与函数on_PenColorChange sub_layout.addWidget(self.__comboBox_penColor) main_layout.addLayout(sub_layout) # 将子布局加入主布局 def __fillColorList(self, comboBox): 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() 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 = QFileDialog.getSaveFileName(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.signal_cut.emit(" ") self.close()
class MainWidget(QWidget): def __init__(self, Parent=None): super().__init__(Parent) self.__InitData() self.__InitView() f = open("params15000.json", "r") data = json.load(f) f.close() self.biase = np.array(data["biase"]) self.w_in = np.array(data["w_in"]) self.w_out = np.array(data["w_out"]) def __InitData(self): self.__paintBoard = PaintBoard(self) self.__colorList = QColor.colorNames() def __InitView(self): # self.setFixedSize(640,480) # self.setFixedSize(480,410) self.setFixedSize(800, 618) self.setWindowTitle("基于ELM的快速手写识别软件NeuHandWriting (NeuHWR1.0)") self.setWindowIcon(QIcon('ICON.ico')) main_layout = QHBoxLayout(self) main_layout.setSpacing(10) main_layout.addWidget(self.__paintBoard) self.font = QtGui.QFont() self.font.setFamily("Consolas") self.font.setPointSize(15) sub_layout = QVBoxLayout() sub_layout.setContentsMargins(10, 10, 10, 10) self.__btn_Clear = QPushButton("Clear Board") self.__btn_Clear.setParent(self) self.__btn_Clear.setFixedSize(146, 70) self.__btn_Clear.setFont(self.font) self.__btn_Clear.clicked.connect(self.__paintBoard.Clear) sub_layout.addWidget(self.__btn_Clear) self.__btn_Quit = QPushButton("Exit") self.__btn_Quit.setParent(self) self.__btn_Quit.setFixedSize(146, 70) self.__btn_Quit.setFont(self.font) self.__btn_Quit.clicked.connect(self.Quit) sub_layout.addWidget(self.__btn_Quit) self.__btn_Save = QPushButton("Save Board") self.__btn_Save.setParent(self) self.__btn_Save.setFixedSize(146, 70) self.__btn_Save.setFont(self.font) self.__btn_Save.clicked.connect(self.on_btn_Save_Clicked) sub_layout.addWidget(self.__btn_Save) self.__cbtn_Eraser = QCheckBox("Eraser") self.__cbtn_Eraser.setParent(self) self.__cbtn_Eraser.setFont(self.font) self.__cbtn_Eraser.clicked.connect(self.on_cbtn_Eraser_clicked) sub_layout.addWidget(self.__cbtn_Eraser) self.__label_result = QLabel(self) self.__label_result.setAlignment(Qt.AlignCenter) self.__label_result.setFixedWidth(146) self.__label_result.setFixedHeight(80) self.__label_result.setFrameStyle(PyQt5.QtWidgets.QFrame.Box) self.__label_result.setFrameShadow(PyQt5.QtWidgets.QFrame.Raised) self.__label_result.setLineWidth(6) self.__label_result.setMidLineWidth(5) self.__label_result.setStyleSheet('background-color: rgb(255,123,100)') sub_layout.addWidget(self.__label_result) self.__btn_Recognize = QPushButton("Recognition") self.__btn_Recognize.setParent(self) self.__btn_Recognize.setFixedSize(146, 70) self.__btn_Recognize.setFont(self.font) self.__btn_Recognize.clicked.connect(self.on_btn_Recognize_clicked) sub_layout.addWidget(self.__btn_Recognize) self.__label_timage = QLabel(self) self.__label_timage.setAlignment(Qt.AlignCenter) self.__label_timage.setFixedWidth(146) self.__label_timage.setFixedHeight(80) self.__label_timage.setFrameStyle(PyQt5.QtWidgets.QFrame.Box) self.__label_timage.setFrameShadow(PyQt5.QtWidgets.QFrame.Raised) self.__label_timage.setLineWidth(6) self.__label_timage.setMidLineWidth(5) self.__label_timage.setStyleSheet('background-color: rgb(125,143,50)') sub_layout.addWidget(self.__label_timage) self.__btn_Random = QPushButton("RandomDigit \nFrom TestSet") self.__btn_Random.setParent(self) self.__btn_Random.setFixedSize(146, 70) self.font.setPointSize(13) self.__btn_Random.setFont(self.font) self.__btn_Random.clicked.connect(self.on_btn_RandomDigit_Clicked) sub_layout.addWidget(self.__btn_Random) main_layout.addLayout(sub_layout) def __fillColorList(self, comboBox): 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() 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 = QFileDialog.getSaveFileName(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 on_btn_RandomDigit_Clicked(self): f = open('x_test_10000.pkl', 'rb') x_test = pickle.load(f, encoding='bytes') f.close() ff = open('y_test_10000.pkl', 'rb') y_test = pickle.load(ff, encoding='bytes') ff.close() random_id = random.randint(0, 9999) image = Image.fromarray(np.reshape(x_test[random_id], (28, 28)) * 255).convert('L') new_image = image.resize((44, 44)) # new_image.show() new_image.save('tpic.png') pixmap = QPixmap('tpic.png') self.__label_timage.setPixmap(pixmap) print("begin") res = self.judge(np.reshape(x_test[random_id], (28, 28))) myres = "{0}|{1}".format(res, int(y_test[random_id])) # show my result font = QtGui.QFont() font.setFamily('Consolas') font.setBold(True) font.setPointSize(18) font.setWeight(75) self.__label_result.setFont(font) self.__label_result.setText(myres) def sigmoid(self, x): return 1.0 / (1.0 + np.exp(-x)) def judge(self, input_v): fd = hog(input_v, orientations=5, pixels_per_cell=(4, 4), cells_per_block=(3, 3), block_norm='L1-sqrt', transform_sqrt=True, feature_vector=True, visualise=False) length = len(fd) y_hat = np.dot( self.w_out, self.sigmoid( np.dot(self.w_in, np.reshape(fd, (length, 1))) + self.biase.transpose())) res_id = np.argmax(y_hat) y_hat[y_hat < 0] = 0 prapability = y_hat[res_id] / np.sum(y_hat) myres = "{0}({1}%)".format(res_id, int(prapability * 100)) print(myres) return myres def on_btn_Recognize_clicked(self): savePath = "tmp.png" image = self.__paintBoard.GetContentAsQImage() image.save(savePath) t_image = Image.open(savePath).convert('L') newt_image = t_image.resize((28, 28)) # newt_image.save("tmp1.png") np_image = np.array(newt_image) / 255.0 # print (np_image.shape) # print (np_image) myres = self.judge(np_image) # show my result font = QtGui.QFont() font.setFamily('Consolas') font.setBold(True) font.setPointSize(20) font.setWeight(75) self.__label_result.setFont(font) self.__label_result.setText(myres) # print (myres) def Quit(self): self.close()
class writeIn(QWidget): # 手写板识别 def __init__(self): super(writeIn, self).__init__() self.__InitData() # 先初始化数据,再初始化界面 self.__InitView() self.detect_model = DBNet() def __InitData(self): ''' 初始化成员变量 ''' self.__paintBoard = PaintBoard(self) # 获取颜色列表(字符串类型) self.__colorList = QColor.colorNames() def __InitView(self): ''' 初始化界面 ''' self.setWindowIcon(QtGui.QIcon('./icon.jpg')) # self.resize(640, 560) # self.setFixedSize(self.width(), self.height()) self.setWindowTitle("手写板识别") self.label_name = QLabel('识别结果:', self) self.label_name.setGeometry(530, 10, 120, 30) self.edit = QTextEdit(self) self.edit.setGeometry(530, 50, 120, 90) # 新建一个水平布局作为本窗体的主布局 main_layout = QHBoxLayout(self) # 设置主布局内边距以及控件间距为10px main_layout.setSpacing(10) img_layout = QVBoxLayout() img_layout.setContentsMargins(10, 10, 10, 10) # 在主界面左侧放置画板 img_layout.addWidget(self.__paintBoard) # # self.__btn_Pic = QLabel("过程演示图") # self.__btn_Pic.setParent(self) # img_layout.addWidget(self.__btn_Pic) self.label_name6 = QLabel('过程演示图', self) self.label_name6.setGeometry(10, 300, 480, 280) self.label_name6.setStyleSheet("QLabel{background:white;}" "QLabel{color:rgb(0,0,0,120);font-size:15px;font-weight:bold;font-family:宋体;}" ) self.label_name6.setAlignment(QtCore.Qt.AlignCenter) img_layout.addWidget(self.label_name6) main_layout.addLayout(img_layout) # 将子布局加入主布局 # 新建垂直子布局用于放置按键 sub_layout = QVBoxLayout() # 设置此子布局和内部控件的间距为5px sub_layout.setContentsMargins(5, 5, 5, 5) splitter = QSplitter(self) # 占位符 sub_layout.addWidget(splitter) sub_layout.addWidget(self.label_name) sub_layout.addWidget(self.edit) self.__btn_Recognize = QPushButton("开始识别") self.__btn_Recognize.setParent(self) self.__btn_Recognize.clicked.connect(self.on_btn_Recognize_Clicked) sub_layout.addWidget(self.__btn_Recognize) self.__btn_Clear = QPushButton("清空画板") self.__btn_Clear.setParent(self) # 设置父对象为本界面 # 将按键按下信号与画板清空函数相关联 self.__btn_Clear.clicked.connect(self.__paintBoard.Clear) sub_layout.addWidget(self.__btn_Clear) self.__btn_return = QPushButton("返回") self.__btn_return.setParent(self) self.__btn_return.clicked.connect(self.slot_btn_function) sub_layout.addWidget(self.__btn_return) self.__btn_Quit = QPushButton("退出") self.__btn_Quit.setParent(self) self.__btn_Quit.clicked.connect(self.Quit) sub_layout.addWidget(self.__btn_Quit) self.__btn_Save = QPushButton("保存作品") self.__btn_Save.setParent(self) self.__btn_Save.clicked.connect(self.on_btn_Save_Clicked) sub_layout.addWidget(self.__btn_Save) self.__cbtn_Eraser = QCheckBox("使用橡皮擦") self.__cbtn_Eraser.setParent(self) self.__cbtn_Eraser.clicked.connect(self.on_cbtn_Eraser_clicked) sub_layout.addWidget(self.__cbtn_Eraser) self.__label_penThickness = QLabel(self) self.__label_penThickness.setText("画笔粗细") self.__label_penThickness.setFixedHeight(20) 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) # 最小变化值为2 self.__spinBox_penThickness.valueChanged.connect( self.on_PenThicknessChange) # 关联spinBox值变化信号和函数on_PenThicknessChange sub_layout.addWidget(self.__spinBox_penThickness) main_layout.addLayout(sub_layout) # 将子布局加入主布局 def on_PenThicknessChange(self): penThickness = self.__spinBox_penThickness.value() self.__paintBoard.ChangePenThickness(penThickness) def on_btn_Save_Clicked(self): savePath = QFileDialog.getSaveFileName(self, 'Save Your Paint', '.\\', '*.png') print(savePath) if savePath[0] == "": print("Save cancel") return image = self.__paintBoard.GetContentAsQImage() image.save(savePath[0]) print(savePath[0]) def Quit(self): self.close() def on_cbtn_Eraser_clicked(self): if self.__cbtn_Eraser.isChecked(): self.__paintBoard.EraserMode = True # 进入橡皮擦模式 else: self.__paintBoard.EraserMode = False # 退出橡皮擦模式 def on_btn_Recognize_Clicked(self): # 识别过程** savePath = "./text.png" image = self.__paintBoard.GetContentAsQImage() image.save(savePath) print(savePath) resultPath, box_list = self.detect_model.predict(savePath) jpg = QtGui.QPixmap(resultPath).scaled(self.label_name6.width(), self.label_name6.height()) self.label_name6.setPixmap(jpg) print(savePath) self.edit.setText('测试文本1') def slot_btn_function(self): self.hide() self.f = FirstUi() self.f.show()
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 = QColor.colorNames() def __InitView(self): ''' 初始化界面 ''' self.setFixedSize(640, 480) self.setWindowTitle("PaintBoard Example PyQt5") #新建一个水平布局作为本窗体的主布局 main_layout = QHBoxLayout(self) #设置主布局内边距以及控件间距为10px main_layout.setSpacing(10) #在主界面左侧放置画板 main_layout.addWidget(self.__paintBoard) #新建垂直子布局用于放置按键 sub_layout = QVBoxLayout() #设置此子布局和内部控件的间距为5px sub_layout.setContentsMargins(5, 5, 5, 5) splitter = QSplitter(self) #占位符 sub_layout.addWidget(splitter) self.__btn_Recognize = QPushButton("开始识别") self.__btn_Recognize.setParent(self) self.__btn_Recognize.clicked.connect(self.on_btn_Recognize_Clicked) sub_layout.addWidget(self.__btn_Recognize) self.__btn_Clear = QPushButton("清空画板") self.__btn_Clear.setParent(self) #设置父对象为本界面 #将按键按下信号与画板清空函数相关联 self.__btn_Clear.clicked.connect(self.__paintBoard.Clear) sub_layout.addWidget(self.__btn_Clear) self.__btn_Quit = QPushButton("退出") self.__btn_Quit.setParent(self) #设置父对象为本界面 self.__btn_Quit.clicked.connect(self.Quit) sub_layout.addWidget(self.__btn_Quit) self.__btn_Save = QPushButton("保存作品") self.__btn_Save.setParent(self) self.__btn_Save.clicked.connect(self.on_btn_Save_Clicked) sub_layout.addWidget(self.__btn_Save) self.__cbtn_Eraser = QCheckBox(" 使用橡皮擦") self.__cbtn_Eraser.setParent(self) self.__cbtn_Eraser.clicked.connect(self.on_cbtn_Eraser_clicked) sub_layout.addWidget(self.__cbtn_Eraser) self.__label_penThickness = QLabel(self) self.__label_penThickness.setText("画笔粗细") self.__label_penThickness.setFixedHeight(20) 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) #最小变化值为2 self.__spinBox_penThickness.valueChanged.connect( self.on_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_penColor.currentIndexChanged.connect( self.on_PenColorChange) #关联下拉列表的当前索引变更信号与函数on_PenColorChange sub_layout.addWidget(self.__comboBox_penColor) main_layout.addLayout(sub_layout) #将子布局加入主布局 def __fillColorList(self, comboBox): 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() 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 = QFileDialog.getSaveFileName(self, 'Save Your Paint', '.\\', '*.png') print(savePath) if savePath[0] == "": print("Save cancel") return image = self.__paintBoard.GetContentAsQImage() image.save(savePath[0]) print(savePath[0]) def on_cbtn_Eraser_clicked(self): if self.__cbtn_Eraser.isChecked(): self.__paintBoard.EraserMode = True #进入橡皮擦模式 else: self.__paintBoard.EraserMode = False #退出橡皮擦模式 def on_btn_Recognize_Clicked(self): savePath = "text.png" image = self.__paintBoard.GetContentAsQImage() image.save(savePath) print(savePath) # 加载图像 img = keras.preprocessing.image.load_img(savePath, target_size=(28, 28)) img = img.convert('L') x = keras.preprocessing.image.img_to_array(img) x = abs(255 - x) #x = x.reshape(28,28) x = np.expand_dims(x, axis=0) x = x / 255.0 new_model = keras.models.load_model('my_model.h5') prediction = new_model.predict(x) output = np.argmax(prediction, axis=1) print("letter written:" + str(chr(output[0]))) def Quit(self): self.close()
class MyWidget(QWidget): log = Logger('logs/main/main.log', level='info') METHOD_KERNEL = Kernel(log.logger) def __init__(self, Parant=None): super().__init__(Parant) self.__eraser_mode_set = False self.__init_data() self.__init_view() def __init_data(self): self.__canvas = PaintBoard(self) self.__colorList = QColor.colorNames() def __init_view(self): self.setFixedSize(720, 530) self.label_university = QLabel("学校:\t华南理工大学", self) self.label_university.setStyleSheet("font-size:12px") self.label_university.setGeometry(460, 10, 120, 35) self.label_school = QLabel("专业:\t控制科学与工程", self) self.label_school.setStyleSheet("font-size:12px") self.label_school.setGeometry(460, 40, 140, 35) self.label_name = QLabel("姓名:\t***", self) self.label_name.setStyleSheet("font-size:12px") self.label_name.setGeometry(460, 70, 100, 35) self.label_number = QLabel("学号:\t***", self) self.label_number.setStyleSheet("font-size:12px") self.label_number.setGeometry(460, 100, 120, 35) self.label_im = QLabel(self) self.label_im.setGeometry(600, 20, 105, 105) self.label_im.setPixmap(QPixmap("./im_scut.jpg").scaled(105, 105)) self.label_log = QTextEdit(self) self.label_log.setReadOnly(True) # self.label_log.moveCursor(QTextCursor.Down) self.label_log.setStyleSheet( "background:transparent; font-size:15px; font-family:Roman times") # self.label_log.resize(220, 200) self.label_log.setGeometry(460, 140, 245, 220) self.label_log.setFrameStyle(QFrame.Panel | QFrame.Sunken) self.label_log.setAlignment(QtCore.Qt.AlignLeft | QtCore.Qt.AlignTop) # create a horizontal layout as the main layout of the GUI main_layout = QHBoxLayout(self) # set the distance between widgets to 10px main_layout.setSpacing(10) left_layout = QVBoxLayout() left_layout.setContentsMargins(5, 5, 5, 5) left_layout.addWidget(self.__canvas) path_layout = QHBoxLayout() path_layout.setContentsMargins(5, 5, 5, 5) self.__lab_path = QLabel(self) self.__lab_path.setText("Save Path") self.__lab_path.setFixedHeight(10) path_layout.addWidget(self.__lab_path) left_layout.addLayout(path_layout) self.__txt_path = QLineEdit("./data/1") path_layout.addWidget(self.__txt_path) self.__choose_path = QPushButton("index") self.__choose_path.setParent(self) # self.__choose_path.setShortcut("Ctrl+S") self.__choose_path.clicked.connect(self.btn_save_clicked) path_layout.addWidget(self.__choose_path) btn_layout = QHBoxLayout() btn_layout.setContentsMargins(5, 5, 5, 5) self.__btn_save = QPushButton("Save") self.__btn_save.setParent(self) self.__btn_save.setShortcut("Ctrl+A") self.__btn_save.clicked.connect(self.save_image) btn_layout.addWidget(self.__btn_save) self.__btn_recognize = QPushButton("Predict") self.__btn_recognize.setParent(self) self.__btn_recognize.setShortcut("Ctrl+R") self.__btn_recognize.clicked.connect(self.btn_recog_clicked) btn_layout.addWidget(self.__btn_recognize) self.__btn_clear = QPushButton("Clear") self.__btn_clear.setShortcut("Ctrl+X") self.__btn_clear.setParent(self) self.__btn_clear.clicked.connect(self.btn_clear_clicked) btn_layout.addWidget(self.__btn_clear) self.__btn_exit = QPushButton("EXIT") self.__btn_exit.setParent(self) # self.__btn_exit.setShortcut("Ctrl+Q") self.__btn_exit.clicked.connect(self.btn_exit_clicked) btn_layout.addWidget(self.__btn_exit) left_layout.addLayout(btn_layout) main_layout.addLayout(left_layout) # put canvas in the left GUI # main_layout.addWidget(self.__canvas) # main_layout.addWidget(left_layout) # set the right layout right_layout = QVBoxLayout() # set the space between right contents right_layout.setContentsMargins(5, 5, 5, 14) splitter = QSplitter(self) right_layout.addWidget(splitter) method_layout = QHBoxLayout() self.__lab_method = QLabel(self) self.__lab_method.setText("Recognition Method") self.__lab_method.setFixedHeight(30) method_layout.addWidget(self.__lab_method) self.kernel_list = [ 'NaiveBayesian', 'VoteFisher', 'MultiFisher', 'SklearnFisher', 'CNN' ] self.__box_method = QComboBox(self) self.__box_method.addItems(self.kernel_list) self.__box_method.setCurrentIndex(0) method_layout.addWidget(self.__box_method) right_layout.addLayout(method_layout) show_layout = QHBoxLayout() canv_layout = QVBoxLayout() self.__cbtn_eraser = QCheckBox("Eraser") self.__cbtn_eraser.setParent(self) self.__cbtn_eraser.clicked.connect(self.btn_eraser_clicked) canv_layout.addWidget(self.__cbtn_eraser) pen_size_layout = QHBoxLayout() self.__lab_pen_size = QLabel(self) self.__lab_pen_size.setText("Pen Size") self.__lab_pen_size.setFixedHeight(20) # self.__lab_pen_size.setFixedSize(50, 20) pen_size_layout.addWidget(self.__lab_pen_size) self.__box_pen_size = QSpinBox(self) self.__box_pen_size.setMaximum(40) self.__box_pen_size.setMinimum(20) self.__box_pen_size.setValue(30) self.__box_pen_size.setSingleStep(2) self.__box_pen_size.setFixedSize(50, 20) self.__box_pen_size.valueChanged.connect(self.box_pen_size_change) pen_size_layout.addWidget(self.__box_pen_size) canv_layout.addLayout(pen_size_layout) pen_color_layout = QHBoxLayout() self.__label_pen_color = QLabel(self) self.__label_pen_color.setText("Pen Color") self.__label_pen_color.setFixedHeight(20) pen_color_layout.addWidget(self.__label_pen_color) self.__combo_pen_color = QComboBox(self) self.__combo_pen_color.setFixedSize(50, 20) self.__fillColorList(self.__combo_pen_color) self.__combo_pen_color.currentIndexChanged.connect( self.pen_color_changed) pen_color_layout.addWidget(self.__combo_pen_color) canv_layout.addLayout(pen_color_layout) show_layout.addLayout(canv_layout) self.feature_map = QLabel() self.feature_map.setFixedSize(100, 100) self.feature_map.setText("") self.feature_map.setObjectName('feature map') show_layout.addWidget(self.feature_map) right_layout.addLayout(show_layout) main_layout.addLayout(right_layout) def btn_recog_clicked(self): self.update() savePath = "./recog.jpg" image = self.__canvas.get_current_image() image.save(savePath) save_path = os.path.abspath(savePath) self.label_log.append("image saved in path:\n{}".format(save_path)) method_text = self.__box_method.currentText() self.label_log.append('method: {}'.format(method_text)) predict = self.METHOD_KERNEL.set_kernel(savePath, method_text) # pic = QTextImageFormat() # pic.setName('./feature_map.jpg') # pic.setHeight(100) # pic.setWidth(100) showImage = QImage('./feature_map.jpg').scaled(100, 100) self.feature_map.setPixmap(QPixmap.fromImage(showImage)) # self.label_log.append('feature map:\n') # self.label_log.textCursor().insertImage('./feature_map.jpg') self.label_log.append("recognition result is: {}".format(predict)) # self.label_log.moveCursor(QTextCursor.End) # self.label_log.textCursor().clearSelection() # del pic message = QMessageBox() message.setText("recognition result is: {}".format(predict)) # message.addButton() message.exec_() def btn_clear_clicked(self): self.__canvas.clear() self.label_log.append("Canvas is clean now!") def btn_save_clicked(self): save_path = QFileDialog.getSaveFileName(self, 'save your paint', '.\\', '*.jpg') if save_path[0] == "": self.label_log.append("save cancel") return self.__txt_path.setText(save_path[0]) save_image = self.__canvas.get_current_image() save_image.save(save_path[0]) save_path_ = os.path.abspath(save_path[0]) self.label_log.append("image saved in path:\n{}".format(save_path_)) def save_image(self): self.__txt_path.setText(self.__txt_path.displayText()) save_path = self.__txt_path.text() created = create_dir_path(save_path) if created: self.label_log.append("create an directory:\n{}".format(save_path)) file_path = create_file_path(save_path) save_image = self.__canvas.get_current_image() save_image.save(file_path) file_path_ = os.path.abspath(file_path) self.label_log.append("image saved in path:\n{}".format(file_path_)) def btn_exit_clicked(self): self.close() QApplication.quit() def btn_eraser_clicked(self): self.__eraser_mode_set = ~self.__eraser_mode_set if self.__eraser_mode_set: self.label_log.append("Attention! Eraser Mode!") else: self.label_log.append("Writing Mode!") if self.__cbtn_eraser.isChecked(): self.__canvas.EraserMode = True else: self.__canvas.EraserMode = False def box_pen_size_change(self): pen_size = self.__box_pen_size.value() self.__canvas.pen_size(pen_size) def __fillColorList(self, comboBox): 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 pen_color_changed(self): color_index = self.__combo_pen_color.currentIndex() color_str = self.__colorList[color_index] self.__canvas.pen_color(color_str)
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 = QColor.colorNames() #获取颜色列表(字符串类型) 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_Clear.clicked.connect( self.__paintBoard.Clear) #将按键按下信号与画板清空函数相关联 sub_layout.addWidget(self.__btn_Clear) self.__label_digit = QLabel(self) self.__label_digit.resize(100, 30) self.__label_digit.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter) # label1.setAutoFillBackground(True) # self.__btn_Quit = QPushButton("退出") # self.__btn_Quit.setParent(self) #设置父对象为本界面 # self.__btn_Quit.clicked.connect(self.Quit) sub_layout.addWidget(self.__label_digit) self.__btn_Save = QPushButton("开始识别") self.__btn_Save.setParent(self) self.__btn_Save.clicked.connect(self.on_btn_Save_Clicked) sub_layout.addWidget(self.__btn_Save) self.__cbtn_Eraser = QCheckBox(" 使用橡皮擦") self.__cbtn_Eraser.setParent(self) self.__cbtn_Eraser.clicked.connect(self.on_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) 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_penThickness.valueChanged.connect( self.on_PenThicknessChange ) #关联spinBox值变化信号和函数on_PenThicknessChange sub_layout.addWidget(self.__spinBox_penThickness) main_layout.addLayout(sub_layout) #将子布局加入主布局 def on_PenThicknessChange(self): penThickness = self.__spinBox_penThickness.value() self.__paintBoard.ChangePenThickness(penThickness) def on_btn_Save_Clicked(self): # savePath = QFileDialog.getSaveFileName(self, 'Save Your Paint', '.\\', '*.png') # print(savePath) # if savePath[0] == "": # print("Save cancel") # return image = self.__paintBoard.GetContentAsQImage() image.save("data/digit.png") t = recognition.recognition() self.digit = t.output() font = QtGui.QFont() # 字体 font.setFamily('微软雅黑') # 加粗 font.setBold(True) # 大小 font.setPointSize(30) font.setWeight(75) self.__label_digit.setFont(font) self.__label_digit.setText("<font color=%s>%s</font>" % ('#7EC7FF', str(self.digit))) #self.__label_digit.setText(self.digit) 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()
class PaintPanel(QWidget): def __init__(self, signals, Parent=None): ''' Constructor ''' super().__init__(Parent) self.InitData(signals) # 先初始化数据,再初始化界面 self.InitView() def InitData(self, signals): ''' 初始化成员变量 ''' self.PaintBoard = PaintBoard(signals, Parent=self) # 获取颜色列表(字符串类型) self.colorList = QColor.colorNames() def InitView(self): ''' 初始化界面 ''' self.setFixedSize(*panel_resolution) self.setWindowTitle("PaintBoard Example PyQt5") # 新建一个水平布局作为本窗体的主布局 main_layout = QVBoxLayout(self) # 设置主布局内边距以及控件间距为10px main_layout.setSpacing(10) # 新建垂直子布局用于放置按键 sub_layout = QHBoxLayout() sub_layout.setContentsMargins(100, 0, 100, 0) # 设置此子布局和内部控件的间距为10px # sub_layout.setContentsMargins(10, 10, 10, 10) # ---------------------通知栏和时钟-------------------------- self.InformLayout = QHBoxLayout() self.InformWidget = QLabel('') self.InformWidget.setStyleSheet( get_qlabel_font_stylesheet(color=INFORM_MSG_COLOR, size=25)) # 设置通告栏的字体格式 self.InformLayout.addWidget(self.InformWidget) self.InformClock = QLCDNumber(self) self.InformClock.setDigitCount(3) self.InformClock.setMode(QLCDNumber.Dec) self.InformClock.setSegmentStyle(QLCDNumber.Flat) self.InformLayout.addWidget(self.InformClock) main_layout.addLayout(self.InformLayout) # --------------------------------------------------- # 在主界面左上侧放置画板 main_layout.addWidget(self.PaintBoard) # ---------------------橡皮擦-------------------------- self.EraserCheckbox = QCheckBox("使用橡皮擦") self.EraserCheckbox.setParent(self) sub_layout.addWidget(self.EraserCheckbox) # --------------------------------------------------- # splitter = QSplitter(self) # 占位符 # sub_layout.addWidget(splitter) # ------------------------笔画粗细----------------------- self.pen_thick_child_panel = QHBoxLayout() self.label_penThickness = QLabel(self) self.label_penThickness.setText("画笔粗细") self.label_penThickness.setAlignment(core.Qt.AlignRight | core.Qt.AlignVCenter) self.pen_thick_child_panel.addWidget(self.label_penThickness) self.spinBox_penThickness = QSpinBox(self) self.spinBox_penThickness.setMaximum(20) self.spinBox_penThickness.setMinimum(2) self.spinBox_penThickness.setValue( config.paint.DefaultThickness) # 默认粗细为10 self.spinBox_penThickness.setSingleStep(2) # 最小变化值为2 self.pen_thick_child_panel.addWidget(self.spinBox_penThickness) sub_layout.addLayout(self.pen_thick_child_panel) # --------------------------------------------------- # -----------------------笔划颜色------------------------- self.pen_color_child_panel = QHBoxLayout() self.label_penColor = QLabel(self) self.label_penColor.setAlignment(core.Qt.AlignRight | core.Qt.AlignVCenter) self.label_penColor.setText("画笔颜色") self.pen_color_child_panel.addWidget(self.label_penColor) self.comboBox_penColor = QComboBox(self) self.fill_color_list(self.comboBox_penColor, self.colorList, default_color) # 用各种颜色填充下拉列表 # --------------------------------------------------- # -------------------------清空画板按钮---------------------- self.pen_color_child_panel.addWidget(self.comboBox_penColor) sub_layout.addLayout(self.pen_color_child_panel) self.ctrl_child_panel = QHBoxLayout() self.btn_Clear = QPushButton("清空画板") self.btn_Clear.setParent(self) # 设置父对象为本界面 self.ctrl_child_panel.addWidget(self.btn_Clear) sub_layout.addLayout(self.ctrl_child_panel) # --------------------------------------------------- # -----------------------退出按钮----------------------- # self.btn_Quit = QPushButton("退出") # self.btn_Quit.setParent(self) # 设置父对象为本界面 # self.ctrl_child_panel.addWidget(self.btn_Quit) # --------------------------------------------------- self.InitLogic() main_layout.addLayout(sub_layout) # 将子布局加入主布局 def InitLogic(self): ''' 初始化所有组件的事件处理逻辑 ''' # 橡皮擦点击事件处理 self.EraserCheckbox.clicked.connect(self.on_eraser_checkbox_clicked) # 笔划粗细改变事件处理 self.spinBox_penThickness.valueChanged.connect( self.on_pen_thickness_change ) # 关联spinBox值变化信号和函数on_PenThicknessChange # 笔划颜色改变事件处理 self.comboBox_penColor.currentIndexChanged.connect( self.on_pen_color_change) # 关联下拉列表的当前索引变更信号与函数on_PenColorChange # 清空画板按钮事件处理 self.btn_Clear.clicked.connect(self.PaintBoard.clear) # 退出事件处理 # self.btn_Quit.clicked.connect(self.Quit) def update_inform(self, inform): self.InformWidget.setText(inform) def fill_color_list(self, comboBox, valList, defVal): ''' 填充下拉菜单中的菜单项,使用colorList填充 ''' index_default = self.colorList.index(config.paint.DefaultColor) index = 0 for color in valList: if color == defVal: index_default = 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) assert default_color != -1, '默认颜色在颜色列表中不存在!' comboBox.setCurrentIndex(index_default) def set_painting(self, painting): self.PaintBoard.set_paint.emit(painting) def set_clock_digit(self, number): self.InformClock.display(str(number)) def set_pen_color(self, color): self.PaintBoard.set_pen_color(color) self.comboBox_penColor.setCurrentIndex(self.colorList.index(color)) def set_pen_thickness(self, thickness): self.PaintBoard.set_pen_thickness(thickness) self.spinBox_penThickness.setValue(int(thickness)) def set_eraser(self, e): self.EraserCheckbox.setChecked(bool(e)) self.PaintBoard.set_eraser(bool(e)) def set_setting_visible(self, v): self.spinBox_penThickness.setEnabled(v) #.setVisible(v) self.comboBox_penColor.setEnabled(v) self.EraserCheckbox.setEnabled(v) self.btn_Clear.setEnabled(v) def extern_click(self, x, y): self.PaintBoard.extern_click(x, y) def extern_paint(self, ps): self.PaintBoard.extern_paint(ps) # self.update() def extern_clear(self, *args, **kwargs): self.PaintBoard.clear() def resetLastPoint(self, x, y): self.PaintBoard.reset_last_point(x, y) def on_pen_color_change(self): color_index = self.comboBox_penColor.currentIndex() color_str = self.colorList[color_index] self.PaintBoard.change_pen_color(color_str) def on_pen_thickness_change(self): penThickness = self.spinBox_penThickness.value() # print('thick change to ', penThickness) self.PaintBoard.change_pen_thickness(penThickness) def on_eraser_checkbox_clicked(self): e = self.EraserCheckbox.isChecked() self.PaintBoard.set_eraser(e) def Quit(self): self.close()
class writeIn(QWidget): def __init__(self, server): super(writeIn, self).__init__() self.__InitData() self.__InitView() self.server = server def __InitData(self): ''' 初始化成员变量 ''' self.__paintBoard = PaintBoard(self) # 获取颜色列表(字符串类型) self.__colorList = QColor.colorNames() def __InitView(self): ''' 初始化界面 ''' self.resize(1000, 600) self.setWindowIcon(QtGui.QIcon('./icon.jpg')) self.setWindowTitle("手写板识别") self.recognition_result = QLabel('识别结果', self) self.recognition_result.setGeometry(530, 50, 430, 200) self.recognition_result.setAlignment(QtCore.Qt.AlignCenter) # 新建一个水平布局作为本窗体的主布局 main_layout = QHBoxLayout(self) # 设置主布局内边距以及控件间距为10px main_layout.setSpacing(10) img_layout = QVBoxLayout() img_layout.setContentsMargins(10, 10, 10, 10) # 在主界面左侧放置画板 img_layout.addWidget(self.__paintBoard) self.detection_result = QLabel('检测结果', self) self.detection_result.setGeometry(10, 300, 480, 280) self.detection_result.setStyleSheet("QLabel{background:white;}" "QLabel{color:rgb(0,0,0,120);font-size:15px;font-weight:bold;font-family:宋体;}" ) self.detection_result.setAlignment(QtCore.Qt.AlignCenter) img_layout.addWidget(self.detection_result) main_layout.addLayout(img_layout) # 将子布局加入主布局 # 新建垂直子布局用于放置按键 sub_layout = QVBoxLayout() # 设置此子布局和内部控件的间距为5px sub_layout.setContentsMargins(5, 5, 5, 5) splitter = QSplitter(self) # 占位符 sub_layout.addWidget(splitter) # sub_layout.addWidget(self.label_name) # sub_layout.addWidget(self.recognition_result) self.__btn_Recognize = QPushButton("开始处理") self.__btn_Recognize.setParent(self) self.__btn_Recognize.clicked.connect(self.on_btn_Recognize_Clicked) sub_layout.addWidget(self.__btn_Recognize) self.__btn_Clear = QPushButton("清空画板") self.__btn_Clear.setParent(self) # 设置父对象为本界面 # 将按键按下信号与画板清空函数相关联 self.__btn_Clear.clicked.connect(self.__paintBoard.Clear) sub_layout.addWidget(self.__btn_Clear) self.__btn_return = QPushButton("返回") self.__btn_return.setParent(self) self.__btn_return.clicked.connect(self.slot_btn_function) sub_layout.addWidget(self.__btn_return) self.__btn_quit = QPushButton("退出") self.__btn_quit.setParent(self) self.__btn_quit.clicked.connect(self.quit) sub_layout.addWidget(self.__btn_quit) self.__btn_Save = QPushButton("保存作品") self.__btn_Save.setParent(self) self.__btn_Save.clicked.connect(self.on_btn_Save_Clicked) sub_layout.addWidget(self.__btn_Save) self.__cbtn_Eraser = QCheckBox("使用橡皮擦") self.__cbtn_Eraser.setParent(self) self.__cbtn_Eraser.clicked.connect(self.on_cbtn_Eraser_clicked) sub_layout.addWidget(self.__cbtn_Eraser) self.__label_penThickness = QLabel(self) self.__label_penThickness.setText("画笔粗细") self.__label_penThickness.setFixedHeight(20) 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) # 最小变化值为2 self.__spinBox_penThickness.valueChanged.connect( self.on_PenThicknessChange) # 关联spinBox值变化信号和函数on_PenThicknessChange sub_layout.addWidget(self.__spinBox_penThickness) main_layout.addLayout(sub_layout) # 将子布局加入主布局 def on_PenThicknessChange(self): penThickness = self.__spinBox_penThickness.value() self.__paintBoard.ChangePenThickness(penThickness) def on_btn_Save_Clicked(self): savePath = QFileDialog.getSaveFileName(self, 'Save Your Paint', '.\\', '*.png') print(savePath) if savePath[0] == "": print("Save cancel") return image = self.__paintBoard.GetContentAsQImage() image.save(savePath[0]) print(savePath[0]) def quit(self): self.close() def on_cbtn_Eraser_clicked(self): if self.__cbtn_Eraser.isChecked(): self.__paintBoard.EraserMode = True # 进入橡皮擦模式 else: self.__paintBoard.EraserMode = False # 退出橡皮擦模式 def on_btn_Recognize_Clicked(self): # 识别过程** savePath = "./img/text.png" image = self.__paintBoard.GetContentAsQImage() image.save(savePath) detection_result_path, recognition_result_path = self.server.handle(savePath) result_jpg = QtGui.QPixmap(detection_result_path) result_width = self.detection_result.width() result_height = result_jpg.height() * (self.detection_result.width()) / result_jpg.width() jpg = QtGui.QPixmap(detection_result_path).scaled(int(result_width), int(result_height)) self.detection_result.setPixmap(jpg) recognition_result_txt = '' if os.path.isfile(recognition_result_path): fp = open(recognition_result_path, 'r', encoding='utf-8') line = fp.readline() while line: recognition_result_txt += line line = fp.readline() self.recognition_result.setText(recognition_result_txt) def slot_btn_function(self): self.hide() self.f = FirstUi(self.server) self.f.show()
class MainWidget(QWidget): def __init__(self, Parent=None): ''' Constructor ''' super().__init__(Parent) self.__InitData() # 先初始化数据,再初始化界面 self.__InitView() def __InitData(self): ''' 初始化成员变量 ''' self.filename = '' #self.__paintBoard = Qgraph(self) self.__paintBoard = PaintBoard(self) # 获取颜色列表(字符串类型) self.__colorList = QColor.colorNames() def __InitView(self): ''' 初始化界面 ''' self.setWindowFlags(QtCore.Qt.WindowTitleHint | QtCore.Qt.WindowCloseButtonHint) #self.setFixedSize(920, 780) self.setWindowTitle("PaintBoard Example PyQt5") # 新建一个水平布局作为本窗体的主布局 main_layout = QHBoxLayout(self) # 设置主布局内边距以及控件间距为10px main_layout.setSpacing(10) # 在主界面左侧放置画板 main_layout.addWidget(self.__paintBoard) # 新建垂直子布局用于放置按键 sub_layout = QVBoxLayout() # 设置此子布局和内部控件的间距为10px sub_layout.setSpacing(10) self.__btn_Open = QPushButton('打开文件') self.__btn_Open.setParent(self) self.__btn_Open.clicked.connect(self.openfile) sub_layout.addWidget(self.__btn_Open) self.__btn_Clear = QPushButton("清空画板") self.__btn_Clear.setParent(self) # 设置父对象为本界面 # 将按键按下信号与画板清空函数相关联 self.__btn_Clear.clicked.connect(self.__paintBoard.Clear) sub_layout.addWidget(self.__btn_Clear) self.__btn_Quit = QPushButton("退出") self.__btn_Quit.setParent(self) # 设置父对象为本界面 self.__btn_Quit.clicked.connect(self.Quit) sub_layout.addWidget(self.__btn_Quit) self.__btn_Save = QPushButton("保存作品") self.__btn_Save.setParent(self) self.__btn_Save.clicked.connect(self.on_btn_Save_Clicked) sub_layout.addWidget(self.__btn_Save) self.__cbtn_Eraser = QCheckBox(" 使用橡皮擦") self.__cbtn_Eraser.setParent(self) self.__cbtn_Eraser.clicked.connect(self.on_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) 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) # 最小变化值为2 self.__spinBox_penThickness.valueChanged.connect( self.on_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_penColor.currentIndexChanged.connect( self.on_PenColorChange) # 关联下拉列表的当前索引变更信号与函数on_PenColorChange sub_layout.addWidget(self.__comboBox_penColor) main_layout.addLayout(sub_layout) # 将子布局加入主布局 def openfile(self): openfile_name = QFileDialog.getOpenFileName(self, '选择文件', '', 'Excel files(*)') self.filename = openfile_name[0] print(self.filename) self.__paintBoard.fillwithpic(self.filename) def __fillColorList(self, comboBox): 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() 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 = QFileDialog.getSaveFileName(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()
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 = QColor.colorNames() #获取颜色列表(字符串类型) def __InitView(self): ''' 初始化界面 ''' self.setFixedSize(2560,1600) self.setWindowTitle("图形学画板") main_layout = QHBoxLayout(self) #新建一个水平布局作为本窗体的主布局 main_layout.setSpacing(10) #设置主布局内边距以及控件间距为10px sub_layout_LEVEL1 = QVBoxLayout() #新建垂直子布局用于放置按键 sub_layout_LEVEL1.setContentsMargins(10, 10, 10, 10) #设置此子布局和内部控件的间距为10px main_layout.addLayout(sub_layout_LEVEL1) #将子布局1加入主布局 sub_layout_LEVEL2 = QVBoxLayout() # 新建垂直子布局用于放置按键 sub_layout_LEVEL2.setContentsMargins(10, 10, 10, 10) # 设置此子布局和内部控件的间距为10px main_layout.addLayout(sub_layout_LEVEL2) # 将子布局2加入主布局 sub_layout_LEVEL3 = QVBoxLayout() # 新建垂直子布局用于放置按键 sub_layout_LEVEL3.setContentsMargins(10, 10, 10, 10) # 设置此子布局和内部控件的间距为10px main_layout.addLayout(sub_layout_LEVEL3) # 将子布局2加入主布局 sub_layout_LEVEL2_1 = QHBoxLayout() # 新建水平子布局用于放置按键 sub_layout_LEVEL2_1.setContentsMargins(10, 10, 10, 10) # 设置此子布局和内部控件的间距为10px sub_layout_LEVEL2.addLayout(sub_layout_LEVEL2_1) sub_layout_LEVEL2_2 = QHBoxLayout() # 新建水平子布局用于放置按键 sub_layout_LEVEL2_2.setContentsMargins(10, 10, 10, 10) # 设置此子布局和内部控件的间距为10px sub_layout_LEVEL2.addLayout(sub_layout_LEVEL2_2) sub_layout_LEVEL2_3 = QHBoxLayout() # 新建水平子布局用于放置按键 sub_layout_LEVEL2_3.setContentsMargins(10, 10, 10, 10) # 设置此子布局和内部控件的间距为10px sub_layout_LEVEL2.addLayout(sub_layout_LEVEL2_3) sub_layout_LEVEL2_4 = QHBoxLayout() # 新建水平子布局用于放置按键 sub_layout_LEVEL2_4.setContentsMargins(10, 10, 10, 10) # 设置此子布局和内部控件的间距为10px sub_layout_LEVEL2.addLayout(sub_layout_LEVEL2_4) #放置左侧图标 self.__btn_Quit = QPushButton(" 退出") self.__btn_Quit.setIcon(QIcon("1.png")) # 设置图标 self.__btn_Quit.setFixedHeight(60) self.__btn_Quit.setFixedWidth(200) self.__btn_Quit.setParent(self) #设置父对象为本界面 self.__btn_Quit.clicked.connect(self.Quit) sub_layout_LEVEL1.addWidget(self.__btn_Quit) self.__btn_Save = QPushButton("保存作品") self.__btn_Save.setIcon(QIcon("1.png")) # 设置图标 self.__btn_Save.setFixedHeight(60) self.__btn_Save.setFixedWidth(200) self.__btn_Save.setParent(self) self.__btn_Save.clicked.connect(self.on_btn_Save_Clicked) sub_layout_LEVEL1.addWidget(self.__btn_Save) self.__btn_Load = QPushButton("读取作品") self.__btn_Load.setIcon(QIcon("1.png")) # 设置图标 self.__btn_Load.setFixedHeight(60) self.__btn_Load.setFixedWidth(200) self.__btn_Load.setParent(self) self.__btn_Load.clicked.connect(self.on_btn_Load_Clicked) sub_layout_LEVEL1.addWidget(self.__btn_Load) splitter = QSplitter(self) #占位符 sub_layout_LEVEL1.addWidget(splitter) self.__cbtn_drawline=QPushButton(" 直线") self.__cbtn_drawline.setIcon(QIcon("1.png")) # 设置图标 self.__cbtn_drawline.setFixedHeight(60) self.__cbtn_drawline.setFixedWidth(200) self.__cbtn_drawline.setParent(self) self.__cbtn_drawline.clicked.connect(self.on_cbtn_drawline_clicked) sub_layout_LEVEL1.addWidget(self.__cbtn_drawline) self.__cbtn_zhexian = QPushButton(" 折线") self.__cbtn_zhexian.setIcon(QIcon("1.png")) # 设置图标 self.__cbtn_zhexian.setFixedHeight(60) self.__cbtn_zhexian.setFixedWidth(200) self.__cbtn_zhexian.setParent(self) self.__cbtn_zhexian.clicked.connect(self.on_cbtn_zhexian_clicked) sub_layout_LEVEL1.addWidget(self.__cbtn_zhexian) self.__cbtn_zhijiaoxian = QPushButton(" 直角线") self.__cbtn_zhijiaoxian.setIcon(QIcon("1.png")) # 设置图标 self.__cbtn_zhijiaoxian.setFixedHeight(60) self.__cbtn_zhijiaoxian.setFixedWidth(200) self.__cbtn_zhijiaoxian.setParent(self) self.__cbtn_zhijiaoxian.clicked.connect(self.on_cbtn_zhijiaoxian_clicked) sub_layout_LEVEL1.addWidget(self.__cbtn_zhijiaoxian) self.__cbtn_duobianxing = QPushButton(" 多边形") self.__cbtn_duobianxing.setIcon(QIcon("1.png")) # 设置图标 self.__cbtn_duobianxing.setFixedHeight(60) self.__cbtn_duobianxing.setFixedWidth(200) self.__cbtn_duobianxing.setParent(self) self.__cbtn_duobianxing.clicked.connect(self.on_cbtn_duobianxing_clicked) sub_layout_LEVEL1.addWidget(self.__cbtn_duobianxing) self.__cbtn_circle = QPushButton(" 圆 ") self.__cbtn_circle.setIcon(QIcon("1.png")) # 设置图标 self.__cbtn_circle.setFixedHeight(60) self.__cbtn_circle.setFixedWidth(200) self.__cbtn_circle.setParent(self) self.__cbtn_circle.clicked.connect(self.on_cbtn_circle_clicked) sub_layout_LEVEL1.addWidget(self.__cbtn_circle) self.__cbtn_oval = QPushButton(" 椭圆") self.__cbtn_oval.setIcon(QIcon("1.png")) # 设置图标 self.__cbtn_oval.setFixedHeight(60) self.__cbtn_oval.setFixedWidth(200) self.__cbtn_oval.setParent(self) self.__cbtn_oval.clicked.connect(self.on_cbtn_oval_clicked) sub_layout_LEVEL1.addWidget(self.__cbtn_oval) self.__cbtn_yuanhu = QPushButton(" 圆弧") self.__cbtn_yuanhu.setIcon(QIcon("1.png")) # 设置图标 self.__cbtn_yuanhu.setFixedHeight(60) self.__cbtn_yuanhu.setFixedWidth(200) self.__cbtn_yuanhu.setParent(self) self.__cbtn_yuanhu.clicked.connect(self.on_cbtn_yuanhu_clicked) sub_layout_LEVEL1.addWidget(self.__cbtn_yuanhu) self.__cbtn_tuoyuanhu = QPushButton(" 椭圆弧") self.__cbtn_tuoyuanhu.setIcon(QIcon("1.png")) # 设置图标 self.__cbtn_tuoyuanhu.setFixedHeight(60) self.__cbtn_tuoyuanhu.setFixedWidth(200) self.__cbtn_tuoyuanhu.setParent(self) self.__cbtn_tuoyuanhu.clicked.connect(self.on_cbtn_tuoyuanhu_clicked) sub_layout_LEVEL1.addWidget(self.__cbtn_tuoyuanhu) self.__cbtn_renyiquxian = QPushButton("任意曲线") self.__cbtn_renyiquxian.setIcon(QIcon("1.png")) # 设置图标 self.__cbtn_renyiquxian.setFixedHeight(60) self.__cbtn_renyiquxian.setFixedWidth(200) self.__cbtn_renyiquxian.setParent(self) self.__cbtn_renyiquxian.clicked.connect(self.on_cbtn_renyiquxian_clicked) sub_layout_LEVEL1.addWidget(self.__cbtn_renyiquxian) self.__cbtn_smooth_rectangle = QPushButton("圆角矩形") self.__cbtn_smooth_rectangle.setIcon(QIcon("1.png")) # 设置图标 self.__cbtn_smooth_rectangle.setFixedHeight(60) self.__cbtn_smooth_rectangle.setFixedWidth(200) self.__cbtn_smooth_rectangle.setParent(self) self.__cbtn_smooth_rectangle.clicked.connect(self.on_cbtn_smooth_rectangle_clicked) sub_layout_LEVEL1.addWidget(self.__cbtn_smooth_rectangle) self.__cbtn_rectangle = QPushButton(" 矩形") self.__cbtn_rectangle.setIcon(QIcon("1.png")) # 设置图标 self.__cbtn_rectangle.setFixedHeight(60) self.__cbtn_rectangle.setFixedWidth(200) self.__cbtn_rectangle.setParent(self) self.__cbtn_rectangle.clicked.connect(self.on_cbtn_rectangle_clicked) sub_layout_LEVEL1.addWidget(self.__cbtn_rectangle) self.__cbtn_character = QPushButton(" 字符") self.__cbtn_character.setIcon(QIcon("1.png")) # 设置图标 self.__cbtn_character.setFixedHeight(60) self.__cbtn_character.setFixedWidth(200) self.__cbtn_character.setParent(self) self.__cbtn_character.clicked.connect(self.on_cbtn_character_clicked) sub_layout_LEVEL1.addWidget(self.__cbtn_character) splitter = QSplitter(self) #占位符 sub_layout_LEVEL1.addWidget(splitter) self.__cbtn_Eraser = QCheckBox("橡皮擦") self.__cbtn_Eraser.setIcon(QIcon("1.png")) # 设置图标 self.__cbtn_Eraser.setFixedHeight(60) self.__cbtn_Eraser.setFixedWidth(200) self.__cbtn_Eraser.setParent(self) self.__cbtn_Eraser.clicked.connect(self.on_cbtn_Eraser_clicked) sub_layout_LEVEL1.addWidget(self.__cbtn_Eraser) self.__btn_Clear = QPushButton("清空画板") self.__btn_Clear.setParent(self) # 设置父对象为本界面 self.__btn_Clear.setIcon(QIcon("1.png")) # 设置图标 self.__btn_Clear.setFixedHeight(60) self.__btn_Clear.setFixedWidth(200) self.__btn_Clear.clicked.connect(self.__paintBoard.Clear) # 将按键按下信号与画板清空函数相关联 sub_layout_LEVEL1.addWidget(self.__btn_Clear) splitter = QSplitter(self) #占位符 sub_layout_LEVEL1.addWidget(splitter) self.__label_penThickness = QLabel(self) self.__label_penThickness.setText("画笔粗细") self.__label_penThickness.setFixedHeight(60) self.__label_penThickness.setFixedWidth(200) sub_layout_LEVEL1.addWidget(self.__label_penThickness) self.__spinBox_penThickness = QSpinBox(self) self.__spinBox_penThickness.setFixedHeight(60) self.__spinBox_penThickness.setFixedWidth(200) self.__spinBox_penThickness.setMaximum(20) self.__spinBox_penThickness.setMinimum(2) self.__spinBox_penThickness.setValue(10) #默认粗细为10 self.__spinBox_penThickness.setSingleStep(2) #最小变化值为1 self.__spinBox_penThickness.valueChanged.connect(self.on_PenThicknessChange)#关联spinBox值变化信号和函数on_PenThicknessChange sub_layout_LEVEL1.addWidget(self.__spinBox_penThickness) self.__label_penThickness = QLabel(self) self.__label_penThickness.setText("选择颜色") self.__label_penThickness.setFixedHeight(60) self.__label_penThickness.setFixedWidth(200) sub_layout_LEVEL1.addWidget(self.__label_penThickness) self.__comboBox_penColor = QComboBox(self) self.__comboBox_penColor.setFixedHeight(60) self.__comboBox_penColor.setFixedWidth(200) self.__fillColorList(self.__comboBox_penColor) #用各种颜色填充下拉列表 self.__comboBox_penColor.currentIndexChanged.connect(self.on_PenColorChange) #关联下拉列表的当前索引变更信号与函数on_PenColorChange sub_layout_LEVEL1.addWidget(self.__comboBox_penColor) self.__btn_fillcolor = QPushButton("填充颜色") self.__btn_fillcolor.setIcon(QIcon("1.png")) # 设置图标 self.__btn_fillcolor.setFixedHeight(60) self.__btn_fillcolor.setFixedWidth(200) self.__btn_fillcolor.setParent(self) self.__btn_fillcolor.clicked.connect(self.on_btn_fillcolor_Clicked) sub_layout_LEVEL1.addWidget(self.__btn_fillcolor) #放置中间图标 sub_layout_LEVEL2_2.addWidget(self.__paintBoard)#放置画板 self.__cbtn_recall = QPushButton(" 撤回") self.__cbtn_recall.setIcon(QIcon("1.png")) # 设置图标 self.__cbtn_recall.setFixedHeight(60) self.__cbtn_recall.setFixedWidth(200) self.__cbtn_recall.setParent(self) self.__cbtn_recall.clicked.connect(self.on_cbtn_recall_clicked) sub_layout_LEVEL2_3.addWidget(self.__cbtn_recall) self.__cbtn_repetition = QPushButton(" 重复") self.__cbtn_repetition.setIcon(QIcon("1.png")) # 设置图标 self.__cbtn_repetition.setFixedHeight(60) self.__cbtn_repetition.setFixedWidth(200) self.__cbtn_repetition.setParent(self) self.__cbtn_repetition.clicked.connect(self.on_cbtn_repetition_clicked) sub_layout_LEVEL2_3.addWidget(self.__cbtn_repetition) self.__cbtn_aim_left = QPushButton(" 左对齐") self.__cbtn_aim_left.setIcon(QIcon("1.png")) # 设置图标 self.__cbtn_aim_left.setFixedHeight(60) self.__cbtn_aim_left.setFixedWidth(200) self.__cbtn_aim_left.setParent(self) self.__cbtn_aim_left.clicked.connect(self.on_cbtn_aim_left_clicked) sub_layout_LEVEL2_1.addWidget(self.__cbtn_aim_left) self.__cbtn_aim_middle = QPushButton("中间对齐") self.__cbtn_aim_middle.setIcon(QIcon("1.png")) # 设置图标 self.__cbtn_aim_middle.setFixedHeight(60) self.__cbtn_aim_middle.setFixedWidth(200) self.__cbtn_aim_middle.setParent(self) self.__cbtn_aim_middle.clicked.connect(self.on_cbtn_aim_middle_clicked) sub_layout_LEVEL2_1.addWidget(self.__cbtn_aim_middle) self.__cbtn_aim_right = QPushButton(" 右对齐") self.__cbtn_aim_right.setIcon(QIcon("1.png")) # 设置图标 self.__cbtn_aim_right.setFixedHeight(60) self.__cbtn_aim_right.setFixedWidth(200) self.__cbtn_aim_right.setParent(self) self.__cbtn_aim_right.clicked.connect(self.on_cbtn_aim_right_clicked) sub_layout_LEVEL2_1.addWidget(self.__cbtn_aim_right) self.__cbtn_cut_inside = QPushButton(" 内裁剪") self.__cbtn_cut_inside.setIcon(QIcon("1.png")) # 设置图标 self.__cbtn_cut_inside.setFixedHeight(60) self.__cbtn_cut_inside.setFixedWidth(200) self.__cbtn_cut_inside.setParent(self) self.__cbtn_cut_inside.clicked.connect(self.on_cbtn_cut_inside_clicked) sub_layout_LEVEL2_4.addWidget(self.__cbtn_cut_inside) self.__cbtn_cut_outside = QPushButton(" 外裁剪") self.__cbtn_cut_outside.setIcon(QIcon("1.png")) # 设置图标 self.__cbtn_cut_outside.setFixedHeight(60) self.__cbtn_cut_outside.setFixedWidth(200) self.__cbtn_cut_outside.setParent(self) self.__cbtn_cut_outside.clicked.connect(self.on_cbtn_cut_outside_clicked) sub_layout_LEVEL2_4.addWidget(self.__cbtn_cut_outside) #放置右侧图标 self.__cbtn_select = QPushButton(" 选择") self.__cbtn_select.setIcon(QIcon("1.png")) # 设置图标 self.__cbtn_select.setFixedHeight(60) self.__cbtn_select.setFixedWidth(200) self.__cbtn_select.setParent(self) self.__cbtn_select.clicked.connect(self.on_cbtn_select_clicked) sub_layout_LEVEL3.addWidget(self.__cbtn_select) def __fillColorList(self, comboBox): 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() 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 = QFileDialog.getSaveFileName(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 on_cbtn_drawline_clicked(self): print("划线函数") def on_btn_Load_Clicked(self): print("读取作品") def on_cbtn_zhexian_clicked(self): print("折线") def on_cbtn_zhijiaoxian_clicked(self): print("直角线") def on_cbtn_duobianxing_clicked(self): print("多边形") def on_cbtn_circle_clicked(self): print("圆") def on_cbtn_oval_clicked(self): print("椭圆") def on_cbtn_yuanhu_clicked(self): print("圆弧") def on_cbtn_tuoyuanhu_clicked(self): print("椭圆弧") def on_cbtn_renyiquxian_clicked(self): print("任意曲线") def on_cbtn_smooth_rectangle_clicked(self): print("圆角矩形") def on_cbtn_rectangle_clicked(self): print("矩形") def on_cbtn_character_clicked(self): print("字符") def on_cbtn_recall_clicked(self): print("撤回") def on_cbtn_repetition_clicked(self): print("重复") def on_cbtn_aim_left_clicked(self): print("左对齐") def on_cbtn_aim_middle_clicked(self): print("中间对齐") def on_cbtn_aim_right_clicked(self): print("右对齐") def on_cbtn_cut_inside_clicked(self): print("内裁剪") def on_cbtn_cut_outside_clicked(self): print("外裁剪") def on_cbtn_select_clicked(self): print("选择") def on_btn_fillcolor_Clicked(self): print("填充颜色") # def on_cbtn_drawline_clicked(self): # print("划线函数") # # def on_cbtn_drawline_clicked(self): # print("划线函数") # # def on_cbtn_drawline_clicked(self): # print("划线函数") # # def on_cbtn_drawline_clicked(self): # print("划线函数") # # def on_cbtn_drawline_clicked(self): # print("划线函数") # # def on_cbtn_drawline_clicked(self): # print("划线函数") def on_cbtn_drawline_clicked(self): print("划线函数") def Quit(self): self.close()