Z
Z
ZheD1s2021-04-02 02:25:38
Python
ZheD1s, 2021-04-02 02:25:38

How can I make it so that when I press a certain key, the lines that I drew in the rectangle become visible?

How can I make it so that when I press a certain key, the lines that I drew in the rectangle become visible?

from PyQt5.Qt import *
from random import randint


class Example(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setFixedSize(700, 700)
        self.setMouseTracking(True)
        self.x = randint(260, 440)
        self.y = 200


    def up(self):
        self.update()

    def keyPressEvent(self, event):
        if event.key() == Qt.Key_Left:
            self.x += 2
        elif event.key() == Qt.Key_Right:
            self.x -= 2
        elif  event.key() == Qt.Key_F:            # Я сделал так, но что-то неправильно
            self.qp.setPen(QColor(13, 239, 66))
        QTimer.singleShot(100, self.up)

    def paintEvent(self, event):
        self.qp = QPainter()
        self.qp.begin(self)
        self.drawFlag()
        self.qp.end()

    def drawFlag(self):
        self.qp.setBrush(QColor(0, 0, 0))
        self.qp.drawRect(260, 455, 180, 110)

        self.qp.drawLine(260, 510, 440, 510)
        self.qp.drawLine(350, 455, 350, 570)
        if self.x > 440 or self.x < 260:             
            self.qp.setPen(QColor(190, 190, 190))
        self.qp.drawLine(self.x, 505, self.x, 515)
        self.qp.drawLine(self.x + 5, 505, self.x + 5, 515)
        self.qp.drawLine(self.x + 10, 505, self.x + 10, 515)
        self.qp.drawLine(self.x + 15, 505, self.x + 15, 515)
        self.qp.drawLine(self.x - 5, 505, self.x - 5, 515)
        self.qp.drawLine(self.x - 10, 505, self.x - 10, 515)
        self.qp.drawLine(self.x - 15, 505, self.x - 15, 515)



StyleSheet = """
QMainWindow{
    border-image: url(11.png) 0 0 0 0 stretch stretch;
}
"""

if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    app.setStyleSheet(StyleSheet)
    w = Example()
    w.show()
    sys.exit(app.exec_())


So that after clicking, there is such a picture:
from PyQt5.Qt import *
from random import randint


class Example(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setFixedSize(700, 700)
        self.setMouseTracking(True)
        self.x = randint(260, 440)
        self.y = 200


    def up(self):
        self.update()

    def keyPressEvent(self, event):
        if event.key() == Qt.Key_Left:
            self.x += 2
        elif event.key() == Qt.Key_Right:
            self.x -= 2
            
        QTimer.singleShot(100, self.up)

    def paintEvent(self, event):
        self.qp = QPainter()
        self.qp.begin(self)
        self.drawFlag()
        self.qp.end()

    def drawFlag(self):
        self.qp.setBrush(QColor(0, 0, 0))
        self.qp.drawRect(260, 455, 180, 110)
        self.qp.setPen(QColor(13, 239, 66))
        self.qp.drawLine(260, 510, 440, 510)
        self.qp.drawLine(350, 455, 350, 570)
        if self.x > 440 or self.x < 260:             
            self.qp.setPen(QColor(190, 190, 190))
        self.qp.drawLine(self.x, 505, self.x, 515)
        self.qp.drawLine(self.x + 5, 505, self.x + 5, 515)
        self.qp.drawLine(self.x + 10, 505, self.x + 10, 515)
        self.qp.drawLine(self.x + 15, 505, self.x + 15, 515)
        self.qp.drawLine(self.x - 5, 505, self.x - 5, 515)
        self.qp.drawLine(self.x - 10, 505, self.x - 10, 515)
        self.qp.drawLine(self.x - 15, 505, self.x - 15, 515)



StyleSheet = """
QMainWindow{
    border-image: url(11.png) 0 0 0 0 stretch stretch;
}
"""

if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    app.setStyleSheet(StyleSheet)
    w = Example()
    w.show()
    sys.exit(app.exec_())

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question