How to make Recaptcha work with QWebView - python

I'm trying to get PyQt4 to view a web-page that requires a captcha to work, but it says that the browser is unsupported. Here is a screenshot of the message:
Is there any way to fix this?

I was able to get this to work by modifying the user-agent string to include one of the supported browsers:
import sys
from PyQt4 import QtCore, QtGui, QtWebKit
class WebPage(QtWebKit.QWebPage):
def userAgentForUrl(self, url):
return super(WebPage, self).userAgentForUrl(url) + ' Chrome'
class Window(QtWebKit.QWebView):
def __init__(self):
super(Window, self).__init__()
self.setPage(WebPage(self))
self.load(QtCore.QUrl('https://www.google.com/recaptcha/api2/demo'))
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = Window()
window.setGeometry(600, 100, 600, 900)
window.show()
sys.exit(app.exec_())

Related

Pdfjs print button does not work with PyQt5

Straight to issue, when pdf loads with pdfjs into pyqt5, seems print button does not work correctly, also the same for download button.
How could this bug be fixed?
The code:
import sys
from PyQt5 import QtCore, QtWidgets, QtGui, QtWebEngineWidgets
PDFJS = 'file:///pdfjs/web/viewer.html'
PDF = 'file:///file0.pdf'
class PdfReport(QtWebEngineWidgets.QWebEngineView):
def __init__(self, parent=None):
super(PdfReport, self).__init__(parent)
self.load(QtCore.QUrl.fromUserInput('%s?file=%s' % (PDFJS, PDF)))
def sizeHint(self):
return QtCore.QSize(640, 480)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
im = PdfReport()
im.show()
sys.exit(app.exec_())
Display:
Any idea how to fix that?
The print task is not enabled in Qt WebEngine so the fault is displayed (I'm still trying to get the data). But in the case of the download button of the PDF it is possible and for this you must use the downloadRequested signal of the QWebEngineProfile:
import os
import sys
from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets
CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))
PDFJS = QtCore.QUrl.fromLocalFile(
os.path.join(CURRENT_DIR, "pdfjs/web/viewer.html")
).toString()
class PdfReport(QtWebEngineWidgets.QWebEngineView):
def __init__(self, parent=None):
super(PdfReport, self).__init__(parent)
QtWebEngineWidgets.QWebEngineProfile.defaultProfile().downloadRequested.connect(
self.on_downloadRequested
)
def load_pdf(self, filename):
url = QtCore.QUrl.fromLocalFile(filename).toString()
self.load(QtCore.QUrl.fromUserInput("%s?file=%s" % (PDFJS, url)))
def sizeHint(self):
return QtCore.QSize(640, 480)
#QtCore.pyqtSlot(QtWebEngineWidgets.QWebEngineDownloadItem)
def on_downloadRequested(self, download):
path, _ = QtWidgets.QFileDialog.getSaveFileName(
self, "Save File", "sample.pdf", "*.pdf"
)
if path:
download.setPath(path)
download.accept()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = PdfReport()
path = os.path.join(CURRENT_DIR, "file0.pdf")
w.load_pdf(path)
w.show()
sys.exit(app.exec_())
That's not a PyQt5 button, that is a button from your web view. It may not work because of your webView object or because the web part of your code lacks functionality for the button.

Change QLabel color using python code not html?

I have searched for how to change QLabel text however I could not find anything which was not in html. Their was a few things claiming to do this but I could not get it to work really hoping for somthing I can copy and past then work out how it works by playing around with it.
Thank you
This is the code
import sys
from PyQt5 import QtWidgets, QtGui
class Program(QtWidgets.QWidget):
def __init__(self):
super().__init__()
"""expierment"""
test = QtWidgets.QLabel(self)
test.setText("I am trying to make this red?")
self.show()
app = QtWidgets.QApplication(sys.argv)
tradingApp = Program()
sys.exit(app.exec())
Use QPalette:
import sys
from PyQt5 import QtWidgets, QtGui, QtCore
class Program(QtWidgets.QWidget):
def __init__(self):
super().__init__()
"""expierment"""
test = QtWidgets.QLabel(self)
pal = test.palette()
pal.setColor(QtGui.QPalette.WindowText, QtGui.QColor("red"))
test.setPalette(pal)
test.setText("I am trying to make this red?")
self.resize(test.sizeHint())
self.show()
app = QtWidgets.QApplication(sys.argv)
tradingApp = Program()
sys.exit(app.exec_())

PyQt4 - function have not executed which connected to a signal

As title, I use
self.connect(MorePushButton, SIGNAL("clicked"), MorePushButton_toggled)
and
self.connect(MorePushButton, SIGNAL("clicked"), self.MoreFrame.setVisible)
to execute the function "MorePushButton_toggled" and set property when MorePushButton clicked.
But, here's the problem I can't figure out:
when I pushed the button MorePushButton, it did nothing!
And it also had no error message.
Did I put the code on the wrong side?
If someone can help me, I would appreciate!
Here's my code below:
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from ui_extention2 import Ui_Dialog
class Main(QDialog, Ui_Dialog):
def __init__(self, parent=None):
super(Main, self).__init__(parent)
self.connect(MorePushButton, SIGNAL("clicked"), MorePushButton_toggled)
self.connect(MorePushButton, SIGNAL("clicked"), self.MoreFrame.setVisible)
self.setupUi(self)
def MorePushButton_toggled(self):
if self.MorePushButton.isChecked() == True:
#self.Dialog.setGeometry(QtCore.QRect(0,0,400,280))
Dialog.resize(400, 280)
print("Resize 400*280")
else:
#self.Dialog.setGeometry(QtCore.QRect(0,0,400,100))
Dialog.resize(400, 100)
print("Resize 400*100")
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Dialog = QtGui.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
Remark:
(1)the .ui file was designed by QT Designer
(2)Python version:4.3
(3)PyQt version:GPL 4.11.4

How can I make link on web page in window using pyqt4?

I have a problem.
Can I make a link on the web page in the window and when the user clicks on it, the web page will be open in the browser.
For example:
import sys
from PyQt4 import QtGui, QtCore
app = QtGui.QApplication(sys.argv)
main = QtGui.QWidget()
main.setGeometry(200, 200, 200, 100)
label = QtGui.QLabel('Stackoverflow/')
box = QtGui.QVBoxLayout()
box.addWidget(label)
main.setLayout(box)
main.show()
sys.exit(app.exec_())
Is it really?
It is of course good that you found answer, but there is special class, which allows you to open URL in default browser or files in default editors/players etc. It is QDesktopServices. For example:
from PyQt5.QtGui import QDesktopServices
from PyQt5.QtCore import QUrl
class MainWindow(QMainWindow, Ui_MainWindow):
def link(self, linkStr):
QDesktopServices.openUrl(QUrl(linkStr))
def __init__(self):
super(MainWindow, self).__init__()
# Set up the user interface from Designer.
self.setupUi(self)
self.label.linkActivated.connect(self.link)
self.label.setText('Stackoverflow/')
This example is definitely larger, but you should know about QDesktopServices, because it is very useful class.
Sorry. I have already searched the answer.
label.setText('Link')
label.setOpenExternalLinks(True)

PyQt4 - setWindowIcon from external website

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

Categories

Resources