PyQt Web Browser: Focus on the webpage without a mouse click - python

I am writing an application for an embedded device that has no mouse input. The web browser is very very basic and does not have any buttons, address bar, file bar , etc. For now, it simply just loads a web page. This web page uses javascript to catch key press events for actions.
The problem I am having is that when the browser loads, the key presses are not caught. I have tracked this problem down to what I believe is a focus problem. When the browser loads, it does not have focus until a mouse click occurs on the application. Since I do not have a mouse, that initial click cannot happen.
How can I make sure that the browser application gets focused correctly such that when I launch it from a terminal or script it will gain immediate focus and the key events can happen accordingly?
My code is as follows:
#!/usr/bin/env python
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
app = QApplication(sys.argv)
web = QWebView()
web.showFullScreen()
web.load(QUrl("http://www.google.com"))
sys.exit(app.exec_())
The QWidget.setFocus() did not work, assuming I used it correctly. Any help is appreciated. Thanks

I tried your code on my laptop and the QWebView had focus already - once Google had loaded I could type and my text appeared in the text box.
If it is a focus problem then as QWebView inherits QWidget you can call its setFocus() method. Here is your code with an extra line calling the QWebView's setFocus method once a page has loaded:
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
app = QApplication(sys.argv)
web = QWebView()
web.loadFinished.connect(web.setFocus)
web.showFullScreen()
web.load(QUrl("http://www.google.com/"))
sys.exit(app.exec_())

You can set focus on the main frame:
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
if __name__ == '__main__':
app = QApplication(sys.argv)
web = QWebView()
frame = web.page().mainFrame()
frame.setFocus()
web.showFullScreen()
web.load(QUrl("http://www.google.com"))
sys.exit(app.exec_())

Related

Enable secure playback, Widevine DRM in python webbrowsers (Protected content)

I'm trying to use the spotify web player on a python browser windows.
I try it in two ways:
import webview
webview.create_window('spotify', 'https://open.spotify.com')
webview.start()
import sys
import time
from PyQt5.Qt import *
from PyQt5.QtWebEngineWidgets import *
from PyQt5.QtWidgets import QApplication
app = QApplication(sys.argv)
web = QWebEngineView()
web.load(QUrl("https://open.spotify.com/"))
web.showFullScreen()
sys.exit(app.exec_())
After I log in with my account I get:
Enable secure playback in your browser.
Is there way I can do this? In the spotify support page it tells me to enable protected content playback or play drm. I am not sure if it is possible.
By #metatoaster 's suggestion, I tried to set the QTWEBENGINE_CHROMIUM_FLAGS environment variable to --widevine-path="/home/<myusername>/.mozilla/firefox/237oaqbb.default-release/gmp-widevinecdm/4.10.2449.0/libwidevinecdm.so"
After I got the location from locate libwidevinecdm.so, and it worked!
I tried with the spotify's path for the library but that didn't work.

Pyside WebView example showing blank screen

I'm starting to play with QWebView in PySide.
I can load HTML into the web view and view it, however I can't seem to load online pages or set urls. Here is the example I've got:
import sys
from PySide.QtCore import *
from PySide.QtGui import *
from PySide.QtWebKit import *
app = QApplication(sys.argv)
web = QWebView()
web.setUrl(QUrl("https://www.google.com"))
web.show()
sys.exit(app.exec_())
All I get from this is a nice blank white window. Anyone know what's missing?
Maybe you are behind a proxy.
I've experienced the same behavior and setting a Proxy for the Application helped.
from PySide.QtNetwork import QNetworkProxy
proxy = QNetworkProxy()
proxy.setType(QNetworkProxy.Socks5Proxy);
proxy.setType(QNetworkProxy.HttpProxy);
proxy.setHostName("*********");
proxy.setPort(****);
QNetworkProxy.setApplicationProxy(proxy);

How to double buffering a WebKit page using QWebView and PySide?

I am playing with PySide and QWebView to provide a WebKit version of a webapp on Windows.
Simple and easy to install in a complex working Windows environment where only Internet Explorer exists.
More over using QWebKit it is quite simple :
#!/usr/bin/env python
#-*- coding:utf-8 -*-
# hellowebkit.py
# Copyright 2009 Piotr MaliƄski, riklaunim#gmail.com
#
# <Under GPL licence>
import sys
from PySide.QtCore import *
from PySide.QtGui import *
from PySide.QtWebKit import *
app = QApplication(sys.argv)
web = QWebView()
web.load(QUrl("http://myapp.example.com"))
web.show()
sys.exit(app.exec_())
I would like to enable double buffering so that there is no drawing until the next page is fully loaded.
Do you know how I should do that?
I guess maybe using web.loadFinished() signal?
Cheers,
Natim
You can use a QStackedWidget and a QSignalMapper to do that:
import sys
from PySide.QtCore import *
from PySide.QtGui import *
from PySide.QtWebKit import *
app = QApplication(sys.argv)
# Create a stack with 2 webviews
stack = QStackedWidget()
mapper = QSignalMapper(stack)
mapper.mapped[int].connect(stack.setCurrentIndex)
for i in range(2):
web = QWebView(stack)
stack.addWidget(web)
# When a webview finishes loading, switch to it
web.loadFinished[bool].connect(mapper.map)
mapper.setMapping(web, i)
# load the page in the non visible webview
stack.widget(1).load(QUrl("http://myapp.example.com"))
stack.show()
sys.exit(app.exec_())

Is it possible to get QWebKit to display pdf files?

i have a link in my QWebkit, which points to a pdf file.
But when the link is clicked, it can't display the pdf file.
is there a way to make it happen?
If you enable plugins through QWebSettings and have a PDF viewer installed that provides a browser plugin such as Acrobat then you should see the PDF rendered using the plugin inside your QWebView:
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
app = QApplication(sys.argv)
web = QWebView()
web.settings().setAttribute(QWebSettings.PluginsEnabled, True)
web.show()
web.load(QUrl('file:///C:/test/test.pdf')) # Change path to actual file.
sys.exit(app.exec_())
This code isn't working for me on Windows with the latest version of Acrobat X (it just shows a progress bar but no PDF - proof that the plugin is loading, just not working) but I'm sure this is how I've done it before. Give it a try and let me know.
Webkit doesn't include a PDF viewer. You'll need to have some way of rendering it - whether you pass it off to a different viewer (Adobe PDF viewer or something else), render it in the control in some way you devise (you could even try rendering it in JavaScript for fun).
This is Gary Hudges code for PyQt5:
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtWebKitWidgets import *
from PyQt5.QtWebKit import *
from PyQt5.QtCore import *
app = QApplication(sys.argv)
web = QWebView()
web.settings().setAttribute(QWebSettings.PluginsEnabled, True)
web.show()
web.load(QUrl('file:///C:/data/progetti_miei/python/test.pdf')) # Change path to actual file.
sys.exit(app.exec_())

PyQt4.QtWebKit + Background Transparency

If you have a compositing window manager, this will give you a transparent window:
import PyQt4.QtGui
a = PyQt4.QtGui.QApplication([])
import PyQt4.QtCore
import PyQt4.QtWebKit
w = PyQt4.QtWebKit.QWebView()
w.setAttribute(PyQt4.QtCore.Qt.WA_TranslucentBackground)
w.show()
a.exec_()
But this will not have a transparent window:
import PyQt4.QtGui
a = PyQt4.QtGui.QApplication([])
import PyQt4.QtCore
import PyQt4.QtWebKit
w = PyQt4.QtWebKit.QWebView()
w.setAttribute(PyQt4.QtCore.Qt.WA_TranslucentBackground)
w.page().currentFrame().documentElement().setInnerXml("text")
w.show()
a.exec_()
I want to be able to have text floating over the background... Is this possible?
This seems to work:
from PyQt4 import QtGui, QtWebKit
from PyQt4.QtCore import Qt
a = QtGui.QApplication([])
view = QtWebKit.QWebView()
view.setAttribute(Qt.WA_TranslucentBackground)
# Works with and without that following line
view.setAttribute(Qt.WA_OpaquePaintEvent, False)
page = view.page()
palette = page.palette()
palette.setBrush(QtGui.QPalette.Base, Qt.transparent)
page.setPalette(palette)
page.currentFrame().documentElement().setInnerXml("text")
view.show()
a.exec_()
Source: http://labs.qt.nokia.com/2009/06/30/transparent-qwebview-or-qwebpage/
The problem here is lazy instantiation. Initially, the WebKit view is not instantiated, and so you get a transparent background. As soon as you access w.page(), the WebKit view is actually instantiated, and so the default white background appears. I am not aware of any way of getting transparency then from inside the WebKit view; there may be a way, but it's not "normal web stuff". For example, w.page().currentFrame().documentElement().setInnerXml('<style>html,body{background:transparent}</style>') doesn't help: the default of white is rendered underneath the transparency.
Possible help: Is it possible to render web content over a clear background using WebKit?

Categories

Resources