当我添加按钮时,KeyPressEvent()不能与标签一起使用

时间:2019-11-05 20:40:06

标签: python python-3.x pyqt pyqt5 qlabel

`

from PyQt5.QtCore import Qt
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtWidgets import QLabel, QPushButton


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

    def initUI(self):
        self.setGeometry(300, 300, 1000, 1000)
        self.setWindowTitle('Example')

        self.label_backround = QLabel(self)
        self.label_backround.move(100, 100)
        self.label_backround.resize(800, 800)

        self.label = QLabel(self)
        self.label.setText("xxxxx")
        self.label.move(340, 340)

        self.Button1 = QPushButton('1', self)
        self.Button1.move(580, 250)

        self.Button2 = QPushButton('2', self)
        self.Button2.move(590, 560)

        self.Button3 = QPushButton('3', self)
        self.Button3.move(210, 660)

    def keyPressEvent(self, event):
        x = self.label.x()
        y = self.label.y()
        if event.key() == Qt.Key_Left:
            self.label.move(x - 15, y)
        elif event.key() == Qt.Key_Up:
            self.label.move(x, y - 15)
        elif event.key() == Qt.Key_Right:
            self.label.move(x + 15, y)
        elif event.key() == Qt.Key_Down:
            self.label.move(x, y + 15)


app = QApplication(sys.argv)
ex = Example()
ex.show()
sys.exit(app.exec_())

`我有一个窗口,当我按下键盘上的上,下,右和左按钮时,该标签应移动。它可以工作,但是当我添加一些按钮时,标签不会移动。

?有人知道这是怎么回事吗?

1 个答案:

答案 0 :(得分:1)

仅接收具有焦点的窗口小部件来接收按键事件的窗口小部件,默认情况下,像QPushButtons这样的许多窗口小部件都像QWidget一样获得焦点。在这种情况下,您不应使用keyPressEvent,而应使用QShorcut,它可以使您独立于小部件捕获键盘事件(显然,您可以通过上下文设置限制)。考虑到上述情况,解决方案是:

import sys

from PyQt5.QtCore import QPoint, Qt
from PyQt5.QtGui import QKeySequence
from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow, QPushButton, QShortcut


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

    def initUI(self):
        self.setGeometry(300, 300, 1000, 1000)
        self.setWindowTitle("Example")

        self.label_backround = QLabel(self)
        self.label_backround.move(100, 100)
        self.label_backround.resize(800, 800)

        self.label = QLabel(self)
        self.label.setText("xxxxx")
        self.label.move(340, 340)

        self.Button1 = QPushButton("1", self)
        self.Button1.move(580, 250)

        self.Button2 = QPushButton("2", self)
        self.Button2.move(590, 560)

        self.Button3 = QPushButton("3", self)
        self.Button3.move(210, 660)

        QShortcut(QKeySequence(Qt.Key_Left), self, activated=self.move_left)
        QShortcut(QKeySequence(Qt.Key_Up), self, activated=self.move_up)
        QShortcut(QKeySequence(Qt.Key_Right), self, activated=self.move_right)
        QShortcut(QKeySequence(Qt.Key_Down), self, activated=self.move_down)

    def move_left(self):
        self.label.move(self.label.pos() + QPoint(-15, 0))

    def move_up(self):
        self.label.move(self.label.pos() + QPoint(0, -15))

    def move_right(self):
        self.label.move(self.label.pos() + QPoint(15, 0))

    def move_down(self):
        self.label.move(self.label.pos() + QPoint(0, 15))


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