Python and webkit Qt4 - python

i'm trying to show a webpage in a frame, but i can't figure out how to do it, also because i can't find the right documentation and/or tutorial for the QtWebkit.
Thanks.
import sys
from PyQt4 import QtGui, QtCore, QtWebKit
class MainWindow(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.resize(350, 250)
self.setWindowTitle('MainWindow')
self.statusBar().showMessage('Loading...')
self.web = QtWebKit.QWebView()
self.web.load(QtCore.QUrl('google.com'))
self.web.show()
app = QtGui.QApplication(sys.argv)
main = MainWindow()
main.show()
sys.exit(app.exec_())

for some doc, you can try the riverbank documentation (though code examples are still in C…)
It seems that your code is fine (maybe add http://?. But did you try to do that without classes? This should work:
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
app = QApplication(sys.argv)
web = QWebView()
web.load(QUrl("http://google.com"))
web.show()
sys.exit(app.exec_())
Otherwise, there is a problem somewhere else (a proxy maybe?)

Related

QGraphicsSVGItem ignores (some) clipping paths. Why?

This SVG image is correctly rendered in Firefox and Inkscape, but for some reason, when using QGraphicsSVGItem without anything fancy, it renders this way:
For reference, this is what it looks like on firefox:
As you can see, the back of the card is not supposed to go beyond the white border.
Am I doing something wrong? Is there a (preferably easy) fix?
MWE:
import sys
from PyQt5 import QtWidgets, QtCore, Qt, QtGui
app = QtWidgets.QApplication(sys.argv)
scene = QtWidgets.QGraphicsScene()
scene.addItem(Qt.QGraphicsSvgItem("back-red.svg"))
graphics_view = QtWidgets.QGraphicsView()
graphics_view.setScene(scene)
graphics_view.show()
sys.exit(app.exec())
Probably your svg does not meet the characteristics that Qt uses (for more information read here). One possible solution is to use QWebEngineView(python -m pip install pyqtwebengine):
import os
import sys
from PyQt5 import QtCore, QtGui, QtWidgets, QtSvg, QtWebEngineWidgets
CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
filename = os.path.join(CURRENT_DIR, "back-red.svg")
scene = QtWidgets.QGraphicsScene()
renderer = QtSvg.QSvgRenderer(filename)
graphics_view = QtWidgets.QGraphicsView()
graphics_view.setScene(scene)
graphics_view.show()
view = QtWebEngineWidgets.QWebEngineView()
view.setContextMenuPolicy(QtCore.Qt.NoContextMenu)
# view.page().setBackgroundColor(QtGui.QColor("transparent"))
view.resize(renderer.viewBox().size())
view.load(QtCore.QUrl.fromLocalFile(filename))
item = scene.addWidget(view)
graphics_view.resize(640, 480)
sys.exit(app.exec())

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

How to get the current url of QwebView?

i use python and pyqt4 for web view.
here my simple code :
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
import os
app = QApplication(sys.argv)
web_view= QWebView()
google='https://www.google.com'
web_view.load(QUrl(google))
web_view.show()
sys.exit(app.exec_())
i want to create a new button where that button any time to execute i want to take current url text link and store it in some variable.
how to do that ?how to take current url from web view ?
QWebView has the url() method that returns the current url, you must call it when you want to get the url.
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
app = QApplication(sys.argv)
w = QWidget()
lay = QVBoxLayout(w)
button = QPushButton("Click Me")
web_view= QWebView()
lay.addWidget(button)
lay.addWidget(web_view)
def foo():
print(web_view.url().toString())
button.clicked.connect(foo)
google='https://www.google.com'
web_view.load(QUrl(google))
w.show()
sys.exit(app.exec_())

Displaying websites with WebGL in Python / PyQT

I'd like to write an app in Python that will display website with WebGL content. That's my script:
import sys
from PyQt4 import QtCore, QtGui, QtWebKit
from PyQt4.QtCore import QFileInfo
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
webView = QtWebKit.QWebView()
webView.setGeometry(QtCore.QRect(30, 20, 591, 511))
webView.settings().setAttribute(QtWebKit.QWebSettings.WebGLEnabled, True)
webView.settings().setAttribute(QtWebKit.QWebSettings.AcceleratedCompositingEnabled, True)
webView.load(QtCore.QUrl('http://webglsamples.org/aquarium/aquarium.html'))
webView.show()
sys.exit(app.exec_())
It's like the "WebGLEnabled" setting doesn't work.
Do you have any ideas? Maybe you have some example code working in PyQT or PySide?

Creating a transparent overlay with qt

I've been learning python recently and now I wanted to (try to) create my first real application, a subtitle player for Linux. So far I've been using the Greenfish subtitle player, which is aimed at Windows users and not properly working in Linux.
I wanted to create the application in qt, since I discovered that transparent windows are not possible in tkinter, but if anybody knows a better framework please suggest!
Now before starting I've been researching the web for several hours to discover how to get my application to show over a full screened flash video and it seems like this is not possible. However the aforementioned GF subtitle player manages to do so in Windows, but not in Linux(maybe it's also because it's running through wine).
So my question is it possible to create a transparent application that remains over a fullscreened flash video and if so, could you point me in the right direction?
Thanks in advance.
edit:
here some example code I've been trying. The window produced by this piece of code does not stay above a fullscreened video
import sys
from PyQt4 import QtGui, QtCore
class mymainwindow(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self, None, QtCore.Qt.WindowStaysOnTopHint)
app = QtGui.QApplication(sys.argv)
mywindow = mymainwindow()
mywindow.show()
app.exec_()
mywindow.show()
Update for PyQt5 pip install PyQt5
import sys
from PyQt5 import QtGui, QtCore, uic
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QMainWindow, QApplication
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setWindowFlags(
QtCore.Qt.WindowStaysOnTopHint |
QtCore.Qt.FramelessWindowHint |
QtCore.Qt.X11BypassWindowManagerHint
)
self.setGeometry(
QtWidgets.QStyle.alignedRect(
QtCore.Qt.LeftToRight, QtCore.Qt.AlignCenter,
QtCore.QSize(220, 32),
QtWidgets.qApp.desktop().availableGeometry()
))
def mousePressEvent(self, event):
QtWidgets.qApp.quit()
if __name__ == '__main__':
app = QApplication(sys.argv)
mywindow = MainWindow()
mywindow.show()
app.exec_()
The example code below will create a centred, frameless window that should stay on top of all other windows on Linux (you can click on the window to close it).
import sys
from PyQt4 import QtGui, QtCore
class mymainwindow(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.setWindowFlags(
QtCore.Qt.WindowStaysOnTopHint |
QtCore.Qt.FramelessWindowHint |
QtCore.Qt.X11BypassWindowManagerHint
)
self.setGeometry(QtGui.QStyle.alignedRect(
QtCore.Qt.LeftToRight, QtCore.Qt.AlignCenter,
QtCore.QSize(220, 32),
QtGui.qApp.desktop().availableGeometry()))
def mousePressEvent(self, event):
QtGui.qApp.quit()
app = QtGui.QApplication(sys.argv)
mywindow = mymainwindow()
mywindow.show()
app.exec_()

Categories

Resources