【问题标题】:Python PyQt4 change image pressing buttonPython PyQt4改变图像按下按钮
【发布时间】:2017-07-26 01:07:07
【问题描述】:

我正在尝试生成一个可视化工具,每次按下按钮时,窗口中的图像都会发生变化。图片必须在按钮所在的同一个窗口中,并且它必须替换之前的图片。

所以我能够显示按钮和第一张图片。但我无法将点击按钮与图像更新过程联系起来。 到目前为止,这是我的代码:

author__ = 'lpp'
#!/usr/bin/python

import os,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
        pic = QtGui.QLabel(self)
        pic.setGeometry(10, 10, 800, 800)
        pic.setPixmap(QtGui.QPixmap( "/home/lpp/Desktop/Image1.png"))

        # 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):
       #print("Test!!!")
        pic = QtGui.QLabel(self)
        pic.setGeometry(100, 10, 800, 800)
        pic.setPixmap(QtGui.QPixmap( "/home/lpp/Desktop/image2.png"))


def main():

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


if __name__ == '__main__':
    main()

除了“def fun”之外的所有东西都有效。 我也试过这些功能,但没有用:

    def fun(self):
        pic = QtGui.QLabel(self)
        pic.setGeometry(100, 10, 800, 800)
        pic.setPixmap(QtGui.QPixmap( "/home/lpp/Desktop/image2.png"))
        return (pic)

    def fun(self):
       #print("Test!!!")
        pic = QtGui.QLabel(self)
        pic.setGeometry(100, 10, 800, 800)
        pic.setPixmap(QtGui.QPixmap( "/home/lpp/Desktop/image2.png"))
        return self.show()

【问题讨论】:

    标签: python pyqt pyqt4 qlabel


    【解决方案1】:

    如果您想要以前的图像,则不应创建新的 QLabel,而只需更新 QPixmap。

    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, 800, 800)
            self.pic.setPixmap(QtGui.QPixmap("/home/lpp/Desktop/image1.png"))
    
            # 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( "/home/lpp/Desktop/image2.png"))
    
    def main():
    
        app = QtGui.QApplication(sys.argv)
        ex = Example()
        sys.exit(app.exec_())
    
    
    if __name__ == '__main__':
        main()
    

    【讨论】:

      猜你喜欢
      • 2011-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-06
      相关资源
      最近更新 更多