Python 在pyqt上显示图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28884213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:51:46  来源:igfitidea点击:

Displaying an image on pyqt

pythonimagepyqt

提问by mcseth antwi

I'm trying to display an image in pyqt for my coursework. I'm attempting this in the Handle Question sub routine. here's a sample of it

我正在尝试在 pyqt 中为我的课程显示图像。我正在处理问题子例程中尝试此操作。这是它的一个示例

class IntegrationQuestions(QtGui.QMainWindow):
    def __init__(self, parent = None):
        from equation import IntQuestion, IntAnswer
        QtGui.QDialog.__init__(self, parent)
        self.setWindowTitle('Simple Integration')
        self.setMinimumSize(265,400)

        self.lbl1 = QtGui.QLabel("Integrate the equation below",self)
        self.lbl1.move(0,0)
        self.lbl1.resize(200,20)

        self.lbl2 = QtGui.QLabel(pretty(IntQuestion[0], use_unicode = False), self)
        self.lbl2.resize(200, 80)
        self.lbl2.move(30,30)

        self.lbl3 = QtGui.QLabel("Sketch pad",self)
        self.lbl3.move(0,120)

        self.SketchPad = QtGui.QLineEdit(self)
        self.SketchPad.resize(250,150)
        self.SketchPad.move(0,150)

        self.lbl4 = QtGui.QLabel("Answer",self)
        self.lbl4.move(0,300)

        self.Answer = QtGui.QLineEdit(self)
        self.Answer.move(0,330)
        self.Answer.resize(250,20)


        self.next_question.clicked.connect(self.HandleQuestion)

this is where I'm attempting to add in a question

这是我试图添加一个问题的地方

    def HandleQuestion(self):
        pic = QtGui.QLabel(self)
        pic.setPixmap(QtGui.QPixmap("Q107.png"))

        self.lbl3.move(0,190)
        self.SketchPad.resize(250,80)
        self.SketchPad.move(0,220)

采纳答案by Shawnic Hedgehog

You initialized everything properly, however you never set the label to be shown.

您正确初始化了所有内容,但是您从未设置要显示的标签。

def HandleQuestion(self):
    pic = QtGui.QLabel(self)
    pic.setPixmap(QtGui.QPixmap("Q107.png"))

    pic.show() # You were missing this.

    self.lbl3.move(0,190)
    self.SketchPad.resize(250,80)
    self.SketchPad.move(0,220)