Python PyQt4更改图像按键

2024-10-04 03:19:58 发布

您现在位置:Python中文网/ 问答频道 /正文

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

所以我可以显示按钮和第一个图像。但我无法将点击按钮与图像更新过程连接起来。 到目前为止,我的代码是:

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”之外,其他的都有用。 我也试过这些功能,但没用:

^{pr2}$

Tags: 图像importselfmainexampledefsys按钮
1条回答
网友
1楼 · 发布于 2024-10-04 03:19:58

如果您想要上一个映像,就不应该创建新的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()

相关问题 更多 >