gpt4 book ai didi

python - 当我添加按钮时,KeyPressEvent() 不适用于标签

转载 作者:行者123 更新时间:2023-12-01 06:58:48 26 4
gpt4 key购买 nike

`

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

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

有人知道这是关于什么的吗?

最佳答案

接收按键事件的小部件仅是具有焦点的小部件,默认情况下,许多小部件(例如 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_())

关于python - 当我添加按钮时,KeyPressEvent() 不适用于标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58719148/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com