Issue
How to set up image with random number I mean If 1 comes up then 1dot image will load. please give me a solution.It generating numbers and loading image but i have no idea how to make a logic for every time when I press roll it will change the image according to random number 1 to 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_())
Solution
Here is a cheap/easy way of making this work using the QGraphicScene Widget. It creates an area to display an image and updates it based on a if statement.
UPDATE: Took away erroneous statement according to my peers.
import random
import sys
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import (QApplication, QMainWindow, QPushButton, QTextEdit, QGraphicsScene, QGraphicsPixmapItem, QGraphicsView)
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.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.graphicsView = QGraphicsView(self)
self.scene = QGraphicsScene()
self.pixmap = QGraphicsPixmapItem()
self.scene.addItem(self.pixmap)
self.graphicsView.setScene(self.scene)
self.graphicsView.resize(100, 100)
self.graphicsView.move(200, 200)
def clickmethod(self):
ran = str(random.randint(1, 6))
self.msg.setText(ran)
if ran == '1':
print(ran)
img = QPixmap('dice1.png')
self.pixmap.setPixmap(img)
elif ran == '2':
print(ran)
img = QPixmap('dice2.png')
self.pixmap.setPixmap(img)
if __name__ == "__main__":
app = QApplication(sys.argv)
Diceroll = MainWindow()
Diceroll.show()
sys.exit(app.exec_())
Answered By - Francis Desjardins
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.