# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'Demo.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
import math
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import Qt, QPoint, QPointF
from PyQt5.QtGui import QPainter, QPen, QPainterPath, QPolygonF, QColor, QPixmap
from PyQt5.QtWidgets import QApplication, QWidget, QFrame, QSizePolicy, QColorDialog, QHBoxLayout, QPushButton, \
QComboBox, QSpinBox, QFormLayout, QLabel
class MyLabel(QLabel):
def __init__(self, parent=None):
super(MyLabel, self).__init__(parent)
self.flag = None
self.setLineWidth(1)
self.setMidLineWidth(0)
self.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred)
self.x0 = 0
self.y0 = 0
self.x1 = 0
self.y1 = 0
self.penColor = (QColor(255, 0, 0, 40))
self.lastpenColor = (QColor(255, 0, 0, 40))
self.penWidth = 30
self.penStyle = Qt.SolidLine
self.capStyle = Qt.RoundCap
def setPenColor(self, color):
self.lastpenColor = color
self.penColor = color
def setDrawStatus(self, status):
if status == True:
self.penColor = self.lastpenColor
elif status == False:
self.penColor = (QColor(0, 0, 0, 0))
def widthChange(self, value):
self.penWidth = value
def paintEvent(self, event):
painter = QPainter(self)
painter.setRenderHint(QPainter.Antialiasing, True)
# 绘制边框线
painter.drawRect(self.rect())
painter.setPen(QPen(self.penColor, self.penWidth, self.penStyle, self.capStyle))
painter.drawLine(self.x0, self.y0, self.x1, self.y1)
def mousePressEvent(self, event):
self.x1 = event.x()
self.y1 = event.y()
self.flag = True
def mouseMoveEvent(self, event):
if self.flag:
self.x0 = self.x1
self.y0 = self.y1
self.x1 = event.x()
self.y1 = event.y()
self.update()
def mouseReleaseEvent(self, event):
self.flag = False
class Widget(QWidget):
def __init__(self,parent=None):
super(Widget,self).__init__(parent)
self.resize(1000, 400)
self.initUi()
def initUi(self):
self.canvas = MyLabel()
layout = QHBoxLayout()
btnPen = QPushButton('画笔')
btnPen.clicked.connect(self.DrawOn)
penColor = QComboBox()
penColor.addItems(['红色 肿瘤','绿色 间质', '紫色 坏死', '蓝色 淋巴'])
penColor.currentTextChanged.connect(self.ColorChange)
btnEraser = QPushButton('橡皮擦')
btnEraser.clicked.connect(self.DrawOff)
penWidth = QSpinBox()
penWidth.resize(20,30)
penWidth.setRange(20,60)
penWidth.setValue(30)
penWidth.valueChanged.connect(self.canvas.widthChange)
fLayout = QFormLayout()
fLayout.setContentsMargins(0, 0, 0, 0)
fLayout.addRow(btnPen)
fLayout.addRow(penColor)
fLayout.addRow(btnEraser)
fLayout.addRow(penWidth)
wid_left = QWidget()
wid_left.setMaximumWidth(200)
wid_left.setLayout(fLayout)
layout.addWidget(wid_left)
layout.addWidget(self.canvas)
self.setLayout(layout)
def DrawOn(self):
self.canvas.setDrawStatus(True)
def ColorChange(self, color):
if color == '红色 肿瘤':
self.canvas.setPenColor(QColor(255, 0, 0, 40))
elif color == '绿色 间质':
self.canvas.setPenColor(QColor(0, 255, 0, 40))
elif color == '紫色 坏死':
self.canvas.setPenColor(QColor(153, 0, 255, 40))
elif color == '蓝色 淋巴':
self.canvas.setPenColor(QColor(0, 0, 255, 40))
def DrawOff(self):
self.canvas.setDrawStatus(False)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Widget()
window.show()
sys.exit(app.exec())
只能出现一个点,想要画出自由的线。
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'Demo.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
import math
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import Qt, QPoint, QPointF
from PyQt5.QtGui import QPainter, QPen, QPainterPath, QPolygonF, QColor, QPixmap
from PyQt5.QtWidgets import QApplication, QWidget, QFrame, QSizePolicy, QColorDialog, QHBoxLayout, QPushButton, \
QComboBox, QSpinBox, QFormLayout, QLabel
class MyLabel(QLabel):
def __init__(self, parent=None):
super(MyLabel, self).__init__(parent)
self.flag = None
self.setLineWidth(1)
self.setMidLineWidth(0)
self.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred)
self.penColor = (QColor(255, 0, 0, 40))
self.lastpenColor = (QColor(255, 0, 0, 40))
self.penWidth = 30
self.penStyle = Qt.SolidLine
self.capStyle = Qt.RoundCap
self.pathList = []
self.path = QPainterPath()
def setPenColor(self, color):
self.lastpenColor = color
self.penColor = color
def setDrawStatus(self, status):
if status == True:
self.penColor = self.lastpenColor
elif status == False:
self.penColor = (QColor(0, 0, 0, 0))
def widthChange(self, value):
self.penWidth = value
def paintEvent(self, event):
painter = QPainter(self)
painter.setRenderHint(QPainter.Antialiasing, True)
# 绘制边框线
painter.drawRect(self.rect())
for p,d in self.pathList:
painter.setPen(QPen(*d))
painter.drawPath(p)
def mousePressEvent(self, event):
self.path = QPainterPath()
self.pathList.append([self.path, (self.penColor, self.penWidth, self.penStyle, self.capStyle)])
self.path.moveTo(event.pos())
self.flag = True
self.update()
def mouseMoveEvent(self, event):
if self.flag:
self.path.lineTo(event.pos())
self.update()
def mouseReleaseEvent(self, event):
self.flag = False
class Widget(QWidget):
def __init__(self,parent=None):
super(Widget,self).__init__(parent)
self.resize(1000, 400)
self.initUi()
def initUi(self):
self.canvas = MyLabel()
layout = QHBoxLayout()
btnPen = QPushButton('画笔')
btnPen.clicked.connect(self.DrawOn)
penColor = QComboBox()
penColor.addItems(['红色 肿瘤','绿色 间质', '紫色 坏死', '蓝色 淋巴'])
penColor.currentTextChanged.connect(self.ColorChange)
btnEraser = QPushButton('橡皮擦')
btnEraser.clicked.connect(self.DrawOff)
penWidth = QSpinBox()
penWidth.resize(20,30)
penWidth.setRange(20,60)
penWidth.setValue(30)
penWidth.valueChanged.connect(self.canvas.widthChange)
fLayout = QFormLayout()
fLayout.setContentsMargins(0, 0, 0, 0)
fLayout.addRow(btnPen)
fLayout.addRow(penColor)
fLayout.addRow(btnEraser)
fLayout.addRow(penWidth)
wid_left = QWidget()
wid_left.setMaximumWidth(200)
wid_left.setLayout(fLayout)
layout.addWidget(wid_left)
layout.addWidget(self.canvas)
self.setLayout(layout)
def DrawOn(self):
self.canvas.setDrawStatus(True)
def ColorChange(self, color):
if color == '红色 肿瘤':
self.canvas.setPenColor(QColor(255, 0, 0, 40))
elif color == '绿色 间质':
self.canvas.setPenColor(QColor(0, 255, 0, 40))
elif color == '紫色 坏死':
self.canvas.setPenColor(QColor(153, 0, 255, 40))
elif color == '蓝色 淋巴':
self.canvas.setPenColor(QColor(0, 0, 255, 40))
def DrawOff(self):
self.canvas.setDrawStatus(False)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Widget()
window.show()
sys.exit(app.exec())
如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!
将self.update()改成:self.update(self.x0,self.y0,self.x1,self.y1)
def mouseMoveEvent(self, event):
if self.flag:
self.x0 = self.x1
self.y0 = self.y1
self.x1 = event.x()
self.y1 = event.y()
self.update(self.x0,self.y0,self.x1,self.y1)
如有帮助,请点采纳。
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!