2017-01-11 43 views
0

我想直接使用pyqt4中的URL將圖像添加到按鈕。我不成功的。添加系統中存儲的圖像的圖像是可能的。因此任何建議或建議將不勝感激。從pyqt4按鈕中的URL訪問圖像[Python]

謝謝

`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()` 
+1

首先在本地磁盤上下載文件。 – furas

+0

問題是,我想直接從URL訪問,並在該特定的網址圖像已經改變,然後如何更新它。 –

+0

'QPixmap'無法從服務器讀取/下載,因此您必須自行讀取/下載。你可以從服務器讀取數據(使用'urllib','requests'等),並且:(a)保存在本地磁盤上,並從這個文件打開,(b)在內存中創建類似文件的對象,你可以像本地一樣使用文件 - 請參閱:'io.StringIO'或'io.BytesIO' – furas

回答

0

的溶液在以下URL中code.The圖像應在一個單獨的文件名進行下載。

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