原理:使用requests
获取图片,加载图片内容到控件中即可。
import sys
import requests
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import (QWidget, QLabel, QVBoxLayout)
from PyQt5.QtGui import QPixmap
class Picture(QWidget):
def __init__(self, parent=None, url=None):
super().__init__(parent)
self.url = url
self.ui()
def ui(self):
# Get Pictures
req = requests.get(self.url)
photo = QPixmap()
photo.loadFromData(req.content)
label= QLabel()
label.setPixmap(photo)
layout =QVBoxLayout()
layout.addWidget(label)
self.setLayout(layout)
if __name__ == '__main__':
app = QApplication(sys.argv)
url='https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=415293130,2419074865&fm=27&gp=0.jpg'
pic = Picture(url=url)
pic.show()
sys.exit(app.exec_())
req = requests.get(self.url)
这一步会造成UI阻塞,建议使用twisted之类的库异步获取图片。