When I try to set my icon from url which contain favicon.ico it work fine, but when I try to set my icon from url which contain **.jpg or .png ** format it does not work and return none. Kindly help me to solve the problem.
you can check the following code.
# url = "http://www.google.com/favicon.icon
url = 'http://www.geo.tv/assets/front/images/gn-icons/256x256.jpg'
self.nam.finished.connect(self.finishRequest)
self.nam.get(QNetworkRequest(QUrl(url)))
def finishRequest(self,reply):
img = QImage()
img.loadFromData(reply.readAll())
self.setWindowIcon(QIcon(img))
I want to get url of image(png,jpg) mean url of icon of a website. and set to a label, windowIcon etc.
but it works only with image(.ico) format and does not work with other format and return nothing.
Analyzing the url I have seen that redirection from http to https so the reply does not have the bytes of the image but the following url. One possible solution is to have Qt handle redirects using request.setAttribute(QNetworkRequest.FollowRedirectsAttribute, True).
import sys
from functools import cached_property
from PySide2.QtCore import Signal, QObject, Qt, QUrl
from PySide2.QtGui import QImage, QPixmap, QIcon
from PySide2.QtNetwork import QNetworkAccessManager, QNetworkReply, QNetworkRequest
from PySide2.QtWidgets import QApplication, QWidget
class ImageDownloader(QObject):
finished = Signal(QImage)
def __init__(self, parent=None):
super().__init__(parent)
self.manager.finished.connect(self.handle_finished)
#cached_property
def manager(self):
return QNetworkAccessManager()
def start_download(self, url):
request = QNetworkRequest(url)
request.setAttribute(QNetworkRequest.FollowRedirectsAttribute, True)
self.manager.get(request)
def handle_finished(self, reply):
if reply.error() != QNetworkReply.NoError:
print("error: ", reply.errorString())
return
image = QImage()
ok = image.loadFromData(reply.readAll())
if not ok:
print("error")
return
self.finished.emit(image)
class Widget(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.downloader = ImageDownloader()
self.downloader.finished.connect(self.handle_finished)
url = QUrl("http://www.geo.tv/assets/front/images/gn-icons/256x256.jpg")
self.downloader.start_download(url)
self.resize(640, 480)
def handle_finished(self, image):
pixmap = QPixmap.fromImage(image)
self.setWindowIcon(QIcon(pixmap))
def main():
app = QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
Related
I want to open self written html code in PyQt5 QWebEngineView. The html code should contain links, which are not url pages but have other information. Below a created a simple example. When clicking on the link named Link, python does not return the parameter inside the link. When I change the value of the link to a url, I get the QUrl as a result. Is there a way to get the content of the link (not a url) when clicked?
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEnginePage
import sys
class MyWebEnginePage(QWebEnginePage):
def acceptNavigationRequest(self, url, _type, isMainFrame):
if _type == QWebEnginePage.NavigationTypeLinkClicked:
print(url)
return False
return super().acceptNavigationRequest(url, _type, isMainFrame)
class App(QMainWindow):
def __init__(self):
super(App, self).__init__()
browser = QWebEngineView()
browser.setPage(MyWebEnginePage(self))
link = "Hallo Welt"
text = f'<a style="text-decoration:none;" href="{link}">Link</a>'
browser.setHtml(text)
self.setCentralWidget(browser)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = App()
window.setGeometry(800, 100, 1000, 800)
window.show()
sys.exit(app.exec_())
You can use a url with the data: scheme to send the link value. When the url is received, you can then check that it has the correct scheme, and emit a signal with only the url's path (which does not include the scheme).
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEnginePage
import sys
class MyWebEnginePage(QWebEnginePage):
dataLinkClicked = pyqtSignal(str)
def acceptNavigationRequest(self, url, _type, isMainFrame):
if (_type == QWebEnginePage.NavigationTypeLinkClicked and
url.scheme() == 'data'):
# send only the url path
self.dataLinkClicked.emit(url.path())
return False
return super().acceptNavigationRequest(url, _type, isMainFrame)
class App(QMainWindow):
def __init__(self):
super(App, self).__init__()
browser = QWebEngineView()
page = MyWebEnginePage(self)
# connect to the signal
page.dataLinkClicked.connect(self.handleDataLink)
browser.setPage(page)
# use a data-url
link = "data:Hallo Welt"
text = f'<a style="text-decoration:none;" href="{link}">Link</a>'
browser.setHtml(text)
self.setCentralWidget(browser)
def handleDataLink(self, text):
print(text) # prints "Hallo Welt"
if __name__ == '__main__':
app = QApplication(sys.argv)
window = App()
window.setGeometry(800, 100, 1000, 800)
window.show()
sys.exit(app.exec_())
I am new to python and I am makeing a simple appliacation where the value of bitcoin is displayed in the system tray. I am useing PyQt5.
My question is: How do I refresh the value and icon every minute. When I try it my menu wont work anymore. Is there a simple fix?
This is my code:
import sys
import time
import math
import json
from PyQt5.QtWidgets import QApplication, QSystemTrayIcon, QMenu
from PyQt5.QtGui import QIcon
from urllib.request import urlopen
from time import sleep
app = QApplication(sys.argv)
while True:
# Connecting the api
with urlopen("https://api.alternative.me/v2/ticker/1/") as response:
source = response.read()
data = json.loads(source)
price = (json.dumps(data['data']['1']['quotes']['USD']['price']))
change = (json.dumps(data['data']['1']['quotes']['USD']['percentage_change_1h']))
intPrice = float(price)
intChange = float(change)
roundPrice = round(intPrice,2)
roundStringPrice = str(roundPrice)
# Icon change
if intChange <=0.00:
trayIcon = QSystemTrayIcon(QIcon('icons/down.png'), parent=app)
else:
trayIcon = QSystemTrayIcon(QIcon('icons/up.png'), parent=app)
trayIcon.setToolTip(roundStringPrice + ' USD')
trayIcon.show()
time.sleep(5)
trayIcon.update()
menu = QMenu()
exitAction = menu.addAction('Quit app')
exitAction.triggered.connect(app.quit)
trayIcon.setContextMenu(menu)
sys.exit(app.exec_())
Don't use while True, urlopen or time.sleep as they block the eventloop, instead use a QTimer with QNetworkAccessManager:
import os.path
import json
from functools import cached_property
from PyQt5.QtCore import pyqtSignal, QObject, QTimer, QUrl
from PyQt5.QtNetwork import QNetworkAccessManager, QNetworkReply, QNetworkRequest
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QMenu, QMessageBox, QSystemTrayIcon
CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))
class ApiManager(QObject):
infoChanged = pyqtSignal(float, float)
def __init__(self, parent=None):
super().__init__(parent)
self.manager.finished.connect(self.handle_finished)
#cached_property
def manager(self):
return QNetworkAccessManager()
def start_request(self):
url = QUrl("https://api.alternative.me/v2/ticker/1/")
qrequest = QNetworkRequest(url)
self.manager.get(qrequest)
def handle_finished(self, reply):
if reply.error() != QNetworkReply.NoError:
print(f"code: {reply.error()} message: {reply.errorString()}")
else:
print("successful")
source = reply.readAll().data()
data = json.loads(source)
r = data["data"]["1"]["quotes"]["USD"]
price = r["price"]
change = r["percentage_change_1h"]
self.infoChanged.emit(price, change)
class SystemTrayIcon(QSystemTrayIcon):
def __init__(self, parent=None):
super().__init__(parent)
self.setIcon(QIcon(self.get_resource("icons/up.png")))
self.menu = QMenu()
exitAction = self.menu.addAction("Quit app")
exitAction.triggered.connect(QApplication.quit)
self.setContextMenu(self.menu)
self.manager = ApiManager()
self.manager.infoChanged.connect(self.handle_info_changed)
timer = QTimer(self, timeout=self.manager.start_request, interval=60 * 1000)
timer.start()
self.manager.start_request()
def get_resource(self, path):
return os.path.join(CURRENT_DIR, path)
def handle_info_changed(self, price, change):
icon = (
QIcon(self.get_resource("icons/down.png"))
if change < 0
else QIcon(self.get_resource("icons/up.png"))
)
self.setIcon(icon)
self.setToolTip(f"{price:.2f} USD")
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
if not QSystemTrayIcon.isSystemTrayAvailable():
QMessageBox.critical(
None, "Systray", "I couldn't detect any system tray on this system."
)
sys.exit(1)
QApplication.setQuitOnLastWindowClosed(False)
trayIcon = SystemTrayIcon(parent=app)
trayIcon.show()
sys.exit(app.exec_())
=== SUMMARY ===========================================
I use QNetworkAccessManager and QNetworkRequests to download two images simultaneously. How can I know for sure that two downloads finished?
=== DETAILED DESCRITION ===============================
I have URLs of two images and I want to download them asynchronously. To do that I initialize QNetworkAccessManager and use two QNetworkRequests. When finished, each request writes the contents of an image into a file.
The problem is that both requests know nothing of each other and, therefore, cannot verify whether the other one is finished.
Could you please tell me how can I wait for both requests to finish?
Here is the complete code:
import sys
from PyQt5.QtCore import QUrl, QFile, QIODevice
from PyQt5.QtNetwork import QNetworkAccessManager, QNetworkRequest
from PyQt5.QtWidgets import QApplication, QWidget
class MainWindow(QWidget):
def __init__(self):
super(MainWindow, self).__init__()
img_url1 = "https://somesite.com/image1.jpg"
img_url2 = "https://somesite.com/image2.jpg"
self.downloader = QNetworkAccessManager()
self.requests = []
self.temp_files = []
for index, mediafile_url in enumerate([img_url1, img_url2]):
self.requests.append(self.downloader.get(QNetworkRequest(QUrl(mediafile_url))))
request = self.requests[index]
self.temp_files.append(QFile(f'{mediafile_url.split("/")[-1]}'))
temp_file = self.temp_files[index]
request.finished.connect(lambda *args, r=request, tf=temp_file: self.download_image(r, tf))
self.show()
#staticmethod
def download_image(request, temp_file):
image_data = request.readAll()
temp_file.open(QIODevice.WriteOnly)
temp_file.write(image_data)
temp_file.close()
app = QApplication(sys.argv)
window = MainWindow()
app.exec_()
You have to create a class that tracks downloads:
from dataclasses import dataclass
import sys
from PyQt5.QtCore import pyqtSignal, QObject, QUrl, QFile, QIODevice
from PyQt5.QtNetwork import QNetworkAccessManager, QNetworkRequest, QNetworkReply
from PyQt5.QtWidgets import QApplication, QWidget
#dataclass
class DownloadRequest:
url: QUrl
filename: str
class DownloadManager(QObject):
finished = pyqtSignal()
def __init__(self, parent=None):
super().__init__(parent)
self._manager = QNetworkAccessManager()
self._pending_downloads = 0
self.manager.finished.connect(self.handle_finished)
#property
def manager(self):
return self._manager
#property
def pending_downloads(self):
return self._pending_downloads
def download(self, requests):
for request in requests:
qrequest = QNetworkRequest(request.url)
qrequest.setAttribute(QNetworkRequest.User, request.filename)
self.manager.get(qrequest)
self._pending_downloads += 1
def handle_finished(self, reply):
self._pending_downloads -= 1
if reply.error() != QNetworkReply.NoError:
print(f"code: {reply.error()} message: {reply.errorString()}")
else:
print("successful")
filename = reply.attribute(QNetworkRequest.User)
file = QFile(filename)
if file.open(QIODevice.WriteOnly):
file.write(reply.readAll())
print(f"pending downloads {self.pending_downloads}")
if self.pending_downloads == 0:
self.finished.emit()
class MainWindow(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self._manager = DownloadManager()
self._manager.finished.connect(self.handle_finished)
img_url1 = "https://docs.python.org/3/_static/py.png"
img_url2 = "https://somesite.com/image2.jpg"
request1 = DownloadRequest(QUrl(img_url1), img_url1.split("/")[-1])
request2 = DownloadRequest(QUrl(img_url2), img_url2.split("/")[-1])
self._manager.download([request1, request2])
def handle_finished(self):
print("finished")
def main():
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()
if __name__ == "__main__":
main()
I'm trying to create a print preview of a QWebEngineView but I can't get it to work.
Here's my code:
...
self.view = QWebEngineView()
...
def handle_preview(self):
dialog = QPrintPreviewDialog()
dialog.paintRequested.connect(self.view.print_)
dialog.exec_()
The code gives me this error:
AttributeError: 'QWebEngineView' object has no attribute 'print_'
The code works perfectly when I use QTextEdit. But that's not what I want. I want to use QWebEngineView.
Based on the official example: WebEngine Widgets PrintMe Example you can implement the preview using the following code.
from PyQt5.QtCore import (QCoreApplication, QEventLoop, QObject, QPointF, Qt,
QUrl, pyqtSlot)
from PyQt5.QtGui import QKeySequence, QPainter
from PyQt5.QtPrintSupport import QPrintDialog, QPrinter, QPrintPreviewDialog
from PyQt5.QtWebEngineWidgets import QWebEnginePage, QWebEngineView
from PyQt5.QtWidgets import QApplication, QDialog, QLabel, QProgressBar, QProgressDialog, QShortcut
class PrintHandler(QObject):
def __init__(self, parent = None):
super().__init__(parent)
self.m_page = None
self.m_inPrintPreview = False
def setPage(self, page):
assert not self.m_page
self.m_page = page
self.m_page.printRequested.connect(self.printPreview)
#pyqtSlot()
def print(self):
printer = QPrinter(QPrinter.HighResolution)
dialog = QPrintDialog(printer, self.m_page.view())
if dialog.exec_() != QDialog.Accepted:
return
self.printDocument(printer)
#pyqtSlot()
def printPreview(self):
if not self.m_page:
return
if self.m_inPrintPreview:
return
self.m_inPrintPreview = True
printer = QPrinter()
preview = QPrintPreviewDialog(printer, self.m_page.view())
preview.paintRequested.connect(self.printDocument)
preview.exec()
self.m_inPrintPreview = False
#pyqtSlot(QPrinter)
def printDocument(self, printer):
loop = QEventLoop()
result = False
def printPreview(success):
nonlocal result
result = success
loop.quit()
progressbar = QProgressDialog(self.m_page.view())
progressbar.findChild(QProgressBar).setTextVisible(False)
progressbar.setLabelText("Wait please...")
progressbar.setRange(0, 0)
progressbar.show()
progressbar.canceled.connect(loop.quit)
self.m_page.print(printer, printPreview)
loop.exec_()
progressbar.close()
if not result:
painter = QPainter()
if painter.begin(printer):
font = painter.font()
font.setPixelSize(20)
painter.setFont(font)
painter.drawText(QPointF(10, 25), "Could not generate print preview.")
painter.end()
def main():
import sys
QCoreApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
app = QApplication(sys.argv)
app.setApplicationName("Previewer")
view = QWebEngineView()
view.setUrl(QUrl("https://stackoverflow.com/questions/59438021"))
view.resize(1024, 750)
view.show()
handler = PrintHandler()
handler.setPage(view.page())
printPreviewShortCut = QShortcut(QKeySequence(Qt.CTRL + Qt.Key_P), view)
printShortCut = QShortcut(QKeySequence(Qt.CTRL + Qt.SHIFT + Qt.Key_P), view)
printPreviewShortCut.activated.connect(handler.printPreview)
printShortCut.activated.connect(handler.print)
sys.exit(app.exec_())
if __name__ == "__main__":
main()
Note: For PySide2 you only have to change pyqtSlot to Slot.
I want to set different QWebEngineProfiles to different QWebEngineViews meaning each view will have its own cookie store. I cant find any documentation on it therefore all help would be greatly appreciated.
Any suggestion of another method of setting independent cookie stores to independent webviews will also help. Cheers.
Code is below (connecting the signal did not format properly here but rest assured it is correct in the real code):
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtWebEngineWidgets import *
import sys
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args,**kwargs)
self.browser={}
self.cookiestore={}
self.page={}
No = input("No: ")
for i in range(int(No)):
self.browser[str(i)] = QWebEngineView()
storagename = str(i)
self.cookiestore[str(i)] = QWebEngineProfile(storagename, self.browser[str(i)])
self.page[str(i)] = QWebEnginePage(self.cookiestore[str(i)], self.browser[str(i)])
self.browser[str(i)].setPage(self.page[str(i)])
self.browser[str(i)].load(QUrl("https://www.google.com"))
self.browser[str(i)].loadFinished.connect(lambda:self._loaded(str(i)))
def _loaded(self, No):
self.browser[No].page().toHtml(self._callable)
def _callable(self, data):
self.html = data
if "" in self.html:
print("Done")
else:
print("wait")
app = QApplication(sys.argv)
window = MainWindow()
app.exec_()
If you want to establish a QWebEngineProfile to a QWebEngineView you must do it through a QWebEnginePage as I show below:
webview = QWebEngineView()
profile = QWebEngineProfile("somestorage", webview)
webpage = QWebEnginePage(profile, webview)
webview.setPage(webpage)
Example:
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEngineProfile, QWebEnginePage
from PyQt5.QtWidgets import QApplication
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
views = []
for i in range(5):
webview = QWebEngineView()
profile = QWebEngineProfile(f"storage-{i}", webview)
webpage = QWebEnginePage(profile, webview)
webview.setPage(webpage)
webview.load(QUrl("https://stackoverflow.com/questions/48142341/how-to-set-a-qwebengineprofile-to-a-qwebengineview"))
webview.show()
views.append(webview)
sys.exit(app.exec_())