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

发布于 2024-10-01 18:14:53 字数 2410 浏览 2 评论 0原文

我想使用 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_())

它说:

回溯(最近一次调用最后一次):
文件 “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(128,128, 200, 200) AttributeError: 'QIcon' 对象没有属性“setGeometry”

如果我删除该 setGeometry 行,应用程序将启动,但图片不会显示在任何地方。感谢您的帮助!

I'd like to display a picture on my form using PyQt4.

Here is my code:

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_())

It says that:

Traceback (most recent call last):
File
"C:\Users\Sergio.Tapia\Documents\NetBeansProjects\PyQTTests\src\pyqttests.py",
line 47, in
mainForm = myWindow() File "C:\Users\Sergio.Tapia\Documents\NetBeansProjects\PyQTTests\src\pyqttests.py",
line 25, in init
self.pictureA.setGeometry(128,128, 200, 200) AttributeError: 'QIcon'
object has no attribute 'setGeometry'

If I remove that setGeometry line, the application launches but the picture isn't displayed anywhere. Thanks for the help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

厌味 2024-10-08 18:14:53

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

There are many ways, for example using the QPixmap class. Once again, I encourage you to take a look at the examples available with PyQt. For example, examples\animation\appchooser\, or examples\widgets\icons\

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文