我正在尝试使用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()`
答案 0 :(得分:0)
该解决方案在以下代码中添加。网址中的图像应以单独的文件名下载。
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_())