PyQt5:如何点击按钮移动油漆?

时间:2017-12-28 06:36:03

标签: python pyqt qt5 pyqt5

在下面的代码中,当" Paint"单击按钮。它还包含一个" Move"按钮。点击"移动"按钮我想在x和y方向上移动矩形1px。但我在这里失败了。

我可以理解这个问题是关于如何在单击按钮的情况下传输变量值。对初学者来说,这对我来说非常困难。任何帮助将非常感谢!

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

1 个答案:

答案 0 :(得分:1)

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

Qt有QRect类有几个方法可以让任务更容易,最初我们创建一个空QRect,当按下btnPaint按钮时,我们将启用QRect到非空的。在按下btnMove按钮的情况下,我们将使用移动矩形的translate方法:

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)