怎么通过python对pyqt5升级_如何更新PyQt5中的QLabel?

在32位Windows7环境下,使用Python3.5和PyQt5创建骰子模拟器时,作者遇到了QLabel无法更新和程序崩溃的问题。点击'Roll'按钮时,应用显示'Python has stopped working'错误。尽管代码看似正确,但问题依然存在。寻求解决方案。
摘要由CSDN通过智能技术生成

import random

import sys

from PyQt5.QtCore import (Qt)

from PyQt5.QtWidgets import (QHBoxLayout, QToolTip, QPushButton, QApplication, QWidget, QLabel)

from PyQt5.QtGui import (QIcon, QPixmap, QFont)

class dicesimulator(QWidget):

def __init__(self):

super().__init__()

self.initUI()

def initUI(self):

QToolTip.setFont(QFont('SansSerif', 10))

dice = QLabel(self)

smaller_pixmap = QPixmap('dice ' + str(random.randint(1,6)) +'.png').scaled(160, 300, Qt.KeepAspectRatio, Qt.FastTransformation)

dice.setPixmap(smaller_pixmap)

dice.move(1, 1)

btn = QPushButton('Roll', self)

btn.setFont(QFont('SansSerif', 20))

btn.setToolTip('Click to Roll Die')

btn.clicked.connect(self.rolldice)

btn.resize(162, 40)

btn.move(0, 161)

self.setGeometry(1427, 30, 162, 201)

self.setFixedSize(self.size())

self.setWindowTitle('Dice Simulator')

self.setWindowIcon(QIcon('icon.png'))

self.show()

def rolldice(self):

new_dice = QPixmap('dice ' + str(random.randint(1,6)) + '.png').scaled(160, 300, Qt.KeepAspectRatio, Qt.FastTransformation)

dice.setPixmap(new_dice)

QApplication.processEvents()

if __name__ == '__main__':

app = QApplication(sys.argv)

ex = dicesimulator()

ex.show()

sys.exit(app.exec_())

我试图在32位Windows7机器上使用PyQt5和Python3.5创建一个掷骰子模拟器。我遇到的问题是,我不能更新QLabel/QPixmap,以便在单击“Roll”按钮时显示不同的随机骰子图像。当点击“Roll”按钮时,我收到一条“Python has stopped working”错误消息,程序关闭。我试图解决这个问题已经有一段时间了,根据我读过的所有东西,我当前的代码应该可以工作,但它没有。在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值