`
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_())
`我有一个窗口,当我按下键盘上的上,下,右和左按钮时,该标签应移动。它可以工作,但是当我添加一些按钮时,标签不会移动。
?有人知道这是怎么回事吗?