weixin_52310393 2022-05-17 23:18 采纳率: 90%
浏览 53
已结题

一个画笔程序画不出线,只有一个点


# -*- 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())

只能出现一个点,想要画出自由的线。

  • 写回答

3条回答 默认 最新

  • 关注

    img

    # -*- 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())
    
    

    如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 5月26日
  • 已采纳回答 5月18日
  • 创建了问题 5月17日

悬赏问题

  • ¥15 开发一个小区生态的小程序
  • ¥15 MddBootstrapInitialize2失败
  • ¥15 LCD Flicker
  • ¥15 Spring MVC项目,访问不到相应的控制器方法
  • ¥15 esp32在micropython环境下使用ssl/tls连接mqtt服务器出现以下报错Connected on 192.168.154.223发生意外错误: 5无法连接到 MQTT 代理,如何解决?
  • ¥15 关于#genesiscsheel#的问题,如何解决?
  • ¥15 Android aidl for hal
  • ¥15 STM32CubeIDE下载程序报错
  • ¥15 微信好友如何转变为会员系统?(相关搜索:小程序)
  • ¥15 c# 直接使用c++ 类库文件