Python 如何在PyQt4中的控件上显示图片?

Python 如何在PyQt4中的控件上显示图片?,python,image,pyqt,Python,Image,Pyqt,我想使用PyQt4在表单上显示图片 这是我的密码: import sys from PyQt4 import QtGui, QtCore class myWindow(QtGui.QWidget): def __init__(self, parent=None): QtGui.QWidget.__init__(self, parent) #The setGeometry method is used to position the control.

我想使用PyQt4在表单上显示图片

这是我的密码:

import sys
from PyQt4 import QtGui, QtCore

class myWindow(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)

        #The setGeometry method is used to position the control.
        #Order: X, Y position - Width, Height of control.
        self.resize(500,350)
        self.center()
        self.setWindowTitle("Sergio's QT Application.")
        self.setWindowIcon(QtGui.QIcon('menuScreenFolderShadow.png'))

        self.setToolTip('<i>Welcome</i> to the <b>first</b> app ever!')
        QtGui.QToolTip.setFont(QtGui.QFont('Helvetica', 12))

        self.txtFirstName = QtGui.QLineEdit('', self)
        self.txtFirstName.setGeometry(35, 35, 150, 20)

        self.txtLastName = QtGui.QLineEdit('', self)
        self.txtLastName.setGeometry(35, 60, 150, 20)

        self.pictureA = QtGui.QIcon("C:\Users\Sergio.Tapia\Downloads\Palm.png")
        self.pictureA.setGeometry(128,128, 200, 200)

        btnSubmit = QtGui.QPushButton('Say hello.', self)
        btnSubmit.setGeometry(340, 250, 150, 35)
        self.connect(btnSubmit, QtCore.SIGNAL("clicked()"), self.clicked)

        btnQuit = QtGui.QPushButton('Exit Application', self)
        btnQuit.setGeometry(340, 300, 150, 35)

        self.connect(btnQuit, QtCore.SIGNAL('clicked()'),
                    QtGui.qApp, QtCore.SLOT('quit()'))

    def clicked(self):
        QtGui.QMessageBox.about(self, "Just dropped by to say hi!", "Welcome to this tutorial %s %s!" % (
            self.txtFirstName.text(), self.txtLastName.text()))

    def center(self):
        screen = QtGui.QDesktopWidget().screenGeometry()
        size =  self.geometry()
        self.move((screen.width()-size.width())/2, (screen.height()-size.height())/2)

app = QtGui.QApplication(sys.argv)
mainForm = myWindow()
mainForm.show()
sys.exit(app.exec_())
导入系统 从PyQt4导入QtGui、QtCore 类myWindow(QtGui.QWidget): def uuu init uuu(self,parent=None): QtGui.QWidget.\uuuuu init\uuuuuu(self,parent) #setGeometry方法用于定位控件。 #顺序:X,Y位置-控制的宽度和高度。 自我调整大小(500350) self.center() setWindowTitle(“Sergio的QT应用程序”) self.setWindowIcon(QtGui.QIcon('menuScreenFolderShadow.png')) self.setToolTip('欢迎使用有史以来第一个应用!') QtGui.QToolTip.setFont(QtGui.QFont('Helvetica',12)) self.txtFirstName=QtGui.QLineEdit(“”,self) self.txtFirstName.setGeometry(35,35,150,20) self.txtLastName=QtGui.QLineEdit(“”,self) self.txtLastName.setGeometry(35,60,150,20) self.pictureA=QtGui.QIcon(“C:\Users\Sergio.Tapia\Downloads\Palm.png”) self.pictureA.setGeometry(128128200200) btnSubmit=QtGui.QPushButton('Say hello',self) btnSubmit.setGeometry(34025015035) self.connect(btnSubmit,QtCore.SIGNAL(“clicked()”),self.clicked) btnQuit=QtGui.QPushButton('Exit Application',self) b查询集合几何(34030015035) self.connect(btnQuit,QtCore.SIGNAL('clicked()'), QtGui.qApp,QtCore.SLOT('quit()')) 已单击的定义(自我): QtGui.QMessageBox.about(self),“只是顺便过来打个招呼!”,“欢迎来到本教程%s%s!”%( self.txtLastName.text(),self.txtLastName.text()) def中心(自我): screen=QtGui.QDesktopWidget().screenGeometry() size=self.geometry() self.move((screen.width()-size.width())/2,(screen.height()-size.height())/2) app=QtGui.QApplication(sys.argv) mainForm=myWindow() mainForm.show() sys.exit(app.exec_()) 它说:

回溯(最近一次呼叫最后一次):
文件 “C:\Users\Sergio.Tapia\Documents\NetBeansProjects\PyQTTests\src\PyQTTests.py”, 第47行,在 mainForm=myWindow()文件“C:\Users\Sergio.Tapia\Documents\NetBeansProjects\PyQTTests\src\PyQTTests.py”, 第25行,在init self.pictureA.setGeometry(128128200200)AttributeError:'QIcon' 对象没有属性“setGeometry”


如果删除setGeometry行,应用程序将启动,但图片不会显示在任何位置。谢谢你的帮助

有很多方法,例如使用
QPixmap
类。我再次鼓励您查看PyQt的可用示例。例如,
examples\animation\appchooser\
,或
examples\widgets\icons\