PyQt4 - 来自外部网站的setWindowIcon [英] PyQt4 - setWindowIcon from external website
本文介绍了PyQt4 - 来自外部网站的setWindowIcon的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前正在PyQt4中开发我的第一个应用程序,我现在想做的是从网站上的图像源设置应用程序的窗口图标。
Im currently developing my first application in PyQt4, and what i would like to do now is to set my application's window icon from an image source on a website.
我认为这样的事情会起作用,但事实并非如此:
self。
I thought something like this would work, but that is not the case: self.
# Sets the application icon
def set_app_icon(self, icon_url):
self.setWindowIcon(QIcon(QUrl(icon_url)))
self.set_app_icon('http://web.page/icon.png')
我如何实现这一目标?提前谢谢。
How can i achieve this? Thank you in advance.
推荐答案
QIcon
无法从中获取数据网页。您需要自己下载并将其提供给 QIcon
。这样的事情:
QIcon
cannot go and fetch data from web. You need to download it yourself and give it to QIcon
. Something like this:
import sys
from PyQt4 import QtGui, QtCore, QtNetwork
class Main(QtGui.QMainWindow):
def __init__(self, parent=None):
super(Main, self).__init__(parent)
def loadIconFromUrl(self, url):
manager = QtNetwork.QNetworkAccessManager(self)
manager.finished.connect(self._setIconFromReply)
manager.get(QtNetwork.QNetworkRequest(QtCore.QUrl(url)))
def _setIconFromReply(self, reply):
p = QtGui.QPixmap()
p.loadFromData(reply.readAll(), format="ico")
self.setWindowIcon(QtGui.QIcon(p))
app = QtGui.QApplication(sys.argv)
main = Main()
main.loadIconFromUrl("http://en.wikipedia.org/favicon.ico")
main.show()
sys.exit(app.exec_())
这篇关于PyQt4 - 来自外部网站的setWindowIcon的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文