【发布时间】: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无法从服务器读取/下载,因此您必须自己读取/下载。您可以从服务器读取(使用urllib、requests等)并且:(a)保存在本地磁盘上,并从此文件打开,(b)在内存中创建类似文件的对象,您可以像本地一样使用文件 - 请参阅:io.StringIO或io.BytesIO -
顺便说一句:见:*.com/a/20273362/1832058
-
您好,我尝试使用上述过程使用但无法显示图像,以下是我尝试的代码