pyqt5加载网路图片,不本地下载。

时间:2021-02-05 10:06:24

 

 依赖组件:

requests

 

 

class webImg:
    pass
    
    

if __name__ == '__main__':  

    import sys
    from PyQt5.QtWidgets import QApplication
    from PyQt5.QtWidgets import (QWidget,  QLabel, QVBoxLayout)

    from PyQt5.QtGui import  QPixmap
            
    app = QApplication(sys.argv) 


    import requests
     
    url='http://www.test.com/uploads/1.jpg' 
    req = requests.get(url)

    print(type(req.content))
    print(dir(req))
    
    photo = QPixmap()
    #photo.loadFromData(req.content, "JPG")
    photo.loadFromData(req.content)
    
    label= QLabel()
    label.setPixmap(photo)

    widget = QWidget()
    layout =QVBoxLayout()
    widget.setLayout(layout)
    layout.addWidget(label)
    
    widget.show()

#####################################################   
    sys.exit(app.exec_())