【问题标题】:access image from URL in pushbutton in pyqt4 [Python]从 pyqt4 [Python] 中按钮中的 URL 访问图像
【发布时间】:2026-01-06 00:00:01
【问题描述】:

我正在尝试使用 pyqt4 中的 URL 直接将图像添加到按钮。我不成功。从存储在系统中的图像中添加图像是可能的。因此,任何建议或建议将不胜感激。

谢谢

`import sys
 from PyQt4 import QtGui, QtCore
 from PyQt4.QtGui import *
 from PyQt4.QtCore import *  

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)


class Entry_view(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.setGeometry(25, 25, 800, 480)

        timer = QTimer()

        t1 = QtGui.QPushButton("Tool1",self)

        font = QtGui.QFont()

        t1.setFont(font)
        t1.setObjectName(_fromUtf8("t1"))
        icon1 = QtGui.QIcon()                # Image has been added to the tool
        icon1.addPixmap(QtGui.QPixmap(_fromUtf8('http://www.siliconsemiconductor.net/images/news/195481055397707.jpg')), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        t1.setIcon(icon1)
        t1.setIconSize(QtCore.QSize(170, 170))
        t1.setObjectName(_fromUtf8("t1"))
        # t1.clicked.connect(self.Tool_Image_Ext)
        t1.resize(400,255)
        t1.move(0, 0)
        self.Tool_Image_Ext()
        self.show()`

【问题讨论】:

  • 先下载文件到本地磁盘。
  • 问题是我想直接从 url 访问,并且在那个特定的 url 图像已经改变然后如何更新它。
  • QPixmap 无法从服务器读取/下载,因此您必须自己读取/下载。您可以从服务器读取(使用urllibrequests 等)并且:(a)保存在本地磁盘上,并从此文件打开,(b)在内存中创建类似文件的对象,您可以像本地一样使用文件 - 请参阅:io.StringIOio.BytesIO
  • 顺便说一句:见:*.com/a/20273362/1832058
  • 您好,我尝试使用上述过程使用但无法显示图像,以下是我尝试的代码

标签: python pyqt4


【解决方案1】:

解决方案添加在以下代码中。url中的图像应以单独的文件名下载。

import sys
from PyQt4 import QtGui, QtCore
from PyQt4.QtGui import *
from PyQt4.QtCore import *  
from PyQt4.QtGui import QPixmap, QIcon
import urllib
import cStringIO
from urllib import urlopen
import requests

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
    return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
    return QtGui.QApplication.translate(context, text, disambig)


class Entry_view(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.setGeometry(25, 25, 800, 480)

        t1 = QtGui.QPushButton("Tool1",self)
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Times New Roman"))
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        t1.setFont(font)
        t1.setObjectName(_fromUtf8("t1"))
        icon1 = QtGui.QIcon()  # Image has been added to the tool
        data = urllib.urlretrieve("https://upload.wikimedia.org/wikipedia/en/b/be/Google_Chrome_for_Android-_Android_5.0_Logo.png","image1.png")
        icon1.addPixmap(QtGui.QPixmap("D:PythonFolder\Codes\image1.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        t1.setIcon(icon1)
        t1.setIconSize(QtCore.QSize(170, 170))
        t1.setObjectName(_fromUtf8("t1"))
        t1.resize(400,255)
        t1.move(0, 0)
        self.show()            

 if __name__ == "__main__":
     app = QtGui.QApplication(sys.argv)

     myapp = Entry_view()
     sys.exit(app.exec_())

【讨论】:

    最近更新 更多