Как сделать так, чтобы при нажатии определенной клавиши стали видны линии, которые я нанес в прямоугольнике?
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_())
Чтобы после нажатия была вот такая вот картина:
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_())