【问题标题】:Dice Roller with Python PyQT5用 Python PyQT5 掷骰子
【发布时间】: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_()) 

【问题讨论】:

    标签: python pyqt pyqt5


    【解决方案1】:

    他们在the other answer 中指出的内容是不正确的,在这种情况下,QGraphicsPixmapItem 和 QLabel 具有相同的容量,因为它们都用于显示 QPixmap。何时难度或其他被错误记录的事情在这里没有区别。

    如果要随机选择图像,则必须创建图像列表并使用 random.choices 随机获取图像并更新 QLabel 显示的 QPixmap:

    class MainWindow(QMainWindow):
        def __init__(self):
            QMainWindow.__init__(self)
            self.setFixedSize(400, 400)
            self.setWindowTitle("Simple Dice roller")
    
            self.button = QPushButton("Roll", self)
            self.button.clicked.connect(self.clickmethod)
            self.msg = QTextEdit(self)
    
            self.msg.resize(100, 32)
            self.msg.move(100, 100)
    
            self.button.resize(100, 32)
            self.button.move(50, 50)
            self.label = QLabel(self)
            self.label.move(100, 110)
            self.label.setFixedSize(500, 300)
    
        def clickmethod(self):
            images = [
                r"S:\Dice sumilator\diceimage\1dot.jpg",
                r"S:\Dice sumilator\diceimage\2dots.jpg",
            ]
            random_image = random.choices(images)
            pixmap = QPixmap(random_image)
            self.label.setPixmap(pixmap)
    

    【讨论】:

      【解决方案2】:

      这是使用 QGraphicScene Widget 进行这项工作的一种廉价/简单的方法。 它创建一个区域来显示图像并根据 if 语句对其进行更新。

      更新:根据我的同行删除了错误的陈述。

      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_())
      

      【讨论】:

      • 关于 QLabel 的注释实际上是不正确的。 QLabel 只需要考虑两个方面:使用 文本时(如 issues section of the layout documentation 中所述),因为存在大小调整问题,以及使用 setScaledContents(True) 时使用图像,因为纵横比被忽略。除此之外,确实没有任何缺点,尤其是在没有指定比例/大小且不涉及布局管理的情况下。
      • 此外,QLabel 不是“唯一与其他小部件工作方式不同的小部件”。一些示例:QStackedWidget/QStackedLayout(minimumSizeHint() 取决于最大页面大小)、QPushButton(有大小限制)、QComboBox(取决于sizeAdjustPolicy)、QSplitter(基于显示的小部件的提示)。 QSizePolicy 的controlType() 函数存在是有原因的。
      • 谢谢你们,这对我帮助很大,而且两个答案都有效。再次感谢您的解释。
      猜你喜欢
      • 1970-01-01
      • 2018-09-08
      • 1970-01-01
      • 2022-01-21
      • 1970-01-01
      • 2013-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多