【发布时间】:2020-12-14 14:44:58
【问题描述】:
如何设置带有随机数的图像我的意思是如果出现 1 则将加载 1dot 图像。请给我一个解决方案。它会生成数字并加载图像,但我不知道如何为每次按下滚动时创建逻辑它会根据随机数 1 到 6 更改图像。
import random
import sys
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import (QApplication,QMainWindow, QPushButton,QTextEdit,QLabel,QFileDialog)
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setFixedSize(400,400)
self.setWindowTitle("Simple Dice roller")
self.button = QPushButton('Roll', self) #button connection
self.button.clicked.connect(self.clickmethod) #methodclicked button connection
self.button.clicked.connect(self.imageview) # buttonimageview connection
self.msg = QTextEdit(self) #for showing text while clicking on button in box
self.msg.resize(100,32)
self.msg.move(100,100)
self.button.resize(100,32)
self.button.move(50,50)
self.imageview()
def clickmethod(self):
ran = str(random.randint(1,6))
self.msg.setText(ran)
def imageview(self):
label = QLabel(self)
label.move(100, 110)
label.setFixedSize(500, 300)
pixmap = QPixmap(r'S:\Dice sumilator\diceimage\1dot.jpg')
#pixmap = QPixmap(r'S:\Dice sumilator\diceimage\2dots.jpg')
label.setPixmap(pixmap)
if __name__ == "__main__":
app = QApplication(sys.argv)
Diceroll = MainWindow()
Diceroll.show()
sys.exit(app.exec_())
【问题讨论】: