PYQT5:如何单击按钮移动油漆



在以下代码中,单击"油漆"按钮时将制作一个矩形。它还包含一个"移动"按钮。通过单击"移动"按钮,我想在x和y方向上移动矩形1px。但是我在这里失败了。

我可以理解问题是关于如何传输变量值的wenn按钮。对于初学者来说,这对我来说非常困难。任何帮助将不胜感激!

# -*- coding: utf-8 -*-
from PyQt5.QtWidgets import QWidget, QApplication, QPushButton, QHBoxLayout, QDoubleSpinBox
from PyQt5.QtGui import QPainter, QColor, QBrush
import sys
class Example(QWidget):
    def __init__(self):
        super(Example, self).__init__()
        self.initUI()
        self.flag = False
    def initUI(self):
        self.setFixedSize(400, 400)
        self.setWindowTitle('Colours')
        self.btnPaint = QPushButton("Paint", self)
        self.btnMove = QPushButton("Move x+1 y+1", self)
        self.deltax = 0  # original value
        self.deltay = 0  # original value
        self.btnPaint.clicked.connect(self.onPaint)
        self.btnMove.clicked.connect(self.onMove)

        self.hlayout = QHBoxLayout()
        self.hlayout.addWidget(self.btnPaint)
        self.hlayout.addWidget(self.btnMove)
        self.hlayout.addStretch(1)
        self.setLayout(self.hlayout)

        self.show()
    def onPaint(self):
        self.flag = True
        self.update()
    def onMove(self):
        self.deltax = self.deltax + 1  # button clicked, value +1
        self.deltay = self.deltay + 1  # button clicked, value +1
        self.flag_move = True
        self.update()
    def paintEvent(self, e):
        if self.flag:
            qp = QPainter()
            qp.begin(self)
            self.drawRectangles(qp)
            qp.end()
        if self.flag_move:
            qp = QPainter()
            qp.begin(self)
            self.drawRectangles(qp)
            qp.end()
    def drawRectangles(self, qp):
        x1 = 10
        y1 = 15
        x2 = 90
        y2 = 60
        col = QColor(0, 0, 0)
        col.setNamedColor('#d4d4d4')
        qp.setPen(col)
        qp.setBrush(QColor(200, 0, 0))
        qp.drawRect(x1+self.deltax, y1+self.deltay, x2+self.deltax, y2+self.deltay)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

您的代码有错误,因为self.flag_move最初不是定义的,但也不需要使用这么多变量。

qt具有QRECT类,该类具有几种使任务更容易的方法,最初我们创建一个null QRECT,当按下BTNPaint按钮时,我们将启用QRECT到非null QRECT。在按下btnmove按钮的情况下,我们将使用移动矩形的翻译方法:

import sys
from PyQt5.QtWidgets import QWidget, QApplication, QPushButton, QHBoxLayout
from PyQt5.QtGui import QPainter, QColor, QBrush
from PyQt5.QtCore import QRect, QPoint
class Example(QWidget):
    def __init__(self):
        super(Example, self).__init__()
        self.initUI()
    def initUI(self):
        self.setFixedSize(400, 400)
        self.setWindowTitle('Colours')
        self.btnPaint = QPushButton("Paint", self)
        self.btnMove = QPushButton("Move x+1 y+1", self)
        self.rect = QRect()
        self.btnPaint.clicked.connect(self.onPaint)
        self.btnMove.clicked.connect(self.onMove)
        self.hlayout = QHBoxLayout(self)
        self.hlayout.addWidget(self.btnPaint)
        self.hlayout.addWidget(self.btnMove)
        self.hlayout.addStretch(1)
        self.show()
    def onPaint(self):
        if self.rect.isNull():
            self.rect = QRect(10, 15, 80, 45)
            self.update()
    def onMove(self):
        if not self.rect.isNull():
            self.rect.translate(QPoint(1, 1))
            self.update()
    def paintEvent(self, e):
        qp = QPainter(self)
        qp.setPen(QColor("#d4d4d4"))
        qp.setBrush(QColor(200, 0, 0))
        qp.drawRect(self.rect)

相关内容

  • 没有找到相关文章

最新更新