图像未显示PyQt4

时间:2017-08-21 13:03:31

标签: python python-2.7 image-processing pyqt4

我编写了这段代码,实际上在单击按钮时会更改图像。

#Make sure you have Winter.jpg image file relative to this script file.
#Code runs fine in python idle, eric.


import sys
from PyQt4 import QtGui

class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()
        self.initUI()

    def initUI(self):
        QtGui.QToolTip.setFont(QtGui.QFont('Test', 10))
        self.setToolTip('This is a <b>QWidget</b> widget')

        # Show  image
        self.pic = QtGui.QLabel(self)
        self.pic.setGeometry(10, 10, 400, 400)
        self.pic.setPixmap(QtGui.QPixmap("Picture1.png").scaledToWidth(64))


        # Show button
        btn = QtGui.QPushButton('Button', self)
        btn.setToolTip('This is a <b>QPushButton</b> widget')
        btn.resize(btn.sizeHint())
        btn.clicked.connect(self.fun)
        btn.move(50, 50)


        self.setGeometry(300, 300, 2000, 1500)
        self.setWindowTitle('Tooltips')
        self.show()

    # Connect button to image updating
    def fun(self):
        self.pic.setPixmap(QtGui.QPixmap( "icon_contribution.png"))

def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

Picture 1

图片1

enter image description here

图片2

此代码运行正常并显示图片1的图像但是当我指定图片2时它没有显示图像。我不知道究竟是什么问题。如何使用PyQt4显示第二个图像。

1 个答案:

答案 0 :(得分:0)

这是我的编译器的一个问题。我使用PyScripter导致了这个问题,当我更改编译器时它运行正常。

相关问题