可单击的Qlabel
我是Python的新手,但是我已经尝试了数十种变体来使它起作用,到目前为止没有运气。
背景:我正在使用PYQT5编写一个简单的数字幻灯游戏作为练习项目。 GUI显示了15个数字瓷砖,我希望玩家能够单击一个瓷砖,并且它将进入空白点(游戏逻辑都没有在这里,我仍在制作GUI)。如果重要的话,我在Pycharm 2022.1.2(社区)中进行编码。
运行代码时我不会遇到任何错误,但是当我单击瓷砖时,它不会移动,然后游戏退出“使用退出代码-1073740791”(没有错误),
我知道我知道可单击的Qlabels不是真的一件事,您必须做一个替代。但是我不确定我的代码在哪里失败。我怀疑它与覆盖(ClicableQlabel)或我的MOVE_TILE函数有关。甚至我的工作方式。我在
class ClickableQLabel(QLabel):
def __init__(self, when_clicked, parent=None):
QLabel.__init__(self, parent)
self._when_clicked = when_clicked
def mousePressEvent(self, ev):
self._when_clicked(ev)
class NewGame(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Number Slide Game")
self.gameTiles = []
for tile_no in range(0, 15):
tile_no_img_num = tile_no + 1
num_img = QPixmap('images\\numTile{0}.png'.format(str(tile_no_img_num)))
num_label = ClickableQLabel(self.move_tile)
num_label.setPixmap(num_img)
self.gameTiles.append(num_label)
random.shuffle(self.gameTiles)
game_layout = QGridLayout()
game_layout.setHorizontalSpacing(0)
game_layout.setVerticalSpacing(0)
tile_num = 0
for rowNum in range(0, 4):
for colNum in range(0, 4):
if tile_num < 15:
game_layout.addWidget(self.gameTiles[tile_num], rowNum, colNum)
tile_num += 1
self.setLayout(game_layout)
def move_tile(self, tile_to_move):
game_tile_to_move = self.gameTiles[tile_to_move]
game_tile_to_move.anim = QPropertyAnimation(game_tile_to_move, b"pos")
game_tile_to_move.anim.setEndValue(QPoint(220, 221))
game_tile_to_move.anim.setDuration(200)
game_tile_to_move.anim.start()
if __name__ == "__main__":
app = QApplication(sys.argv)
gameWindow = NewGame()
gameWindow.show()
sys.exit(app.exec_())
I'm fairly new to Python, but I've tried dozens of variations to get this to work, no luck so far.
Background: I'm writing a simple number slide game, as a practice project, using PyQt5. The gui displays 15 number tiles, and I want the player to be able to click a tile and it will move into the blank spot (none of the game logic is here yet, I'm still prototyping the gui). If it matters, I'm coding in PyCharm 2022.1.2 (community).
I'm not getting any errors when I run the code, but when I click a tile it doesn't move and then the game exits with "Process finished with exit code -1073740791" (no errors)
I know clickable QLabels are not really a thing, you have to do an override. But I'm not sure where my code is failing. I suspect it has something to do with the override (ClicableQLabel), or my move_tile function. Or even how I'm doing QGridLayout. I tried all of the answers in this question but none of them worked for me.
class ClickableQLabel(QLabel):
def __init__(self, when_clicked, parent=None):
QLabel.__init__(self, parent)
self._when_clicked = when_clicked
def mousePressEvent(self, ev):
self._when_clicked(ev)
class NewGame(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Number Slide Game")
self.gameTiles = []
for tile_no in range(0, 15):
tile_no_img_num = tile_no + 1
num_img = QPixmap('images\\numTile{0}.png'.format(str(tile_no_img_num)))
num_label = ClickableQLabel(self.move_tile)
num_label.setPixmap(num_img)
self.gameTiles.append(num_label)
random.shuffle(self.gameTiles)
game_layout = QGridLayout()
game_layout.setHorizontalSpacing(0)
game_layout.setVerticalSpacing(0)
tile_num = 0
for rowNum in range(0, 4):
for colNum in range(0, 4):
if tile_num < 15:
game_layout.addWidget(self.gameTiles[tile_num], rowNum, colNum)
tile_num += 1
self.setLayout(game_layout)
def move_tile(self, tile_to_move):
game_tile_to_move = self.gameTiles[tile_to_move]
game_tile_to_move.anim = QPropertyAnimation(game_tile_to_move, b"pos")
game_tile_to_move.anim.setEndValue(QPoint(220, 221))
game_tile_to_move.anim.setDuration(200)
game_tile_to_move.anim.start()
if __name__ == "__main__":
app = QApplication(sys.argv)
gameWindow = NewGame()
gameWindow.show()
sys.exit(app.exec_())
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论