What is the PyQt4 equivalent of GTK set_keep_above(True) method? - python

I would like to keep the PyQt4 window above like I do with GTK with set_keep_above(True).
Is that possible ?
Edit 20111101 : this is my code, I don't know how to force the window "above" :
#!/usr/bin/python2
# -*- coding: utf8 -*-
import os, sys, signal
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
from PyQt4.QtScript import *
from PyQt4.QtNetwork import *
if os.path.exists(".forum_smileys_cache"):
pass
else:
os.mkdir(".forum_smileys_cache")
app = QApplication(sys.argv)
signal.signal(signal.SIGINT, signal.SIG_DFL)
webpage = QWebView()
webpage.setWindowTitle("forums smileys code")
manager = webpage.page().networkAccessManager()
diskCache = QNetworkDiskCache(webpage)
diskCache.setCacheDirectory(".forum_smileys_cache")
manager.setCache(diskCache)
webpage.show()
webpage.setGeometry(0,0, 300, 550)
webpage.resize(250,800)
webpage.load(QUrl("http://www.sputnick-area.net/smileys.html"))
sys.exit(app.exec_())

I use something like this:
from PyQt4 import QtGui as qt
from PyQt4 import QtCore as qc
class MainWin(qt.QMainWindow):
def setKeepAbove(self, above):
if above:
self.setWindowFlags(self.windowFlags() | qc.Qt.WindowStaysOnTopHint)
else:
self.setWindowFlags(self.windowFlags() & ~qc.Qt.WindowStaysOnTopHint)

Related

PyQt with Flask dont show the MainWindow

i'm trayin to create a gui that run a flask application using this example https://github.com/onur2677/PyQt-Flask
in this exemple they use pyqt 4
i'm trying to use pyqt 5 so i made some changes
but the mainwindow open and close in the same time
this is my code for the gui
import sys
from PyQt5 import *
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import psycopg2
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5 import QtCore
from PyQt5 import *
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtCore import QEventLoop
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWidgets import QApplication
PORT = 5000
ROOT_URL = 'http://localhost:{}'.format(PORT)
class FlaskThread(QThread):
def __init__(self, application):
QThread.__init__(self)
self.application = application
def __del__(self):
self.wait()
def run(self):
self.application.run(port=PORT)
def createGuiFor(application):
qtapp = QApplication(sys.argv)
webapp = FlaskThread(application)
webapp.start()
qtapp.aboutToQuit.connect(webapp.terminate)
webview = QWebEngineView()
webview.load(QUrl(ROOT_URL))
webview.show()
webview.setWindowTitle("MyApp")
webview.setContextMenuPolicy(QtCore.Qt.NoContextMenu)
return qtapp.exec_()
if __name__ == '__main__':
from MyWebApp import app
app = QApplication(sys.argv)
sys.exit(app.exec_())
sys.exit(createGuiFor(app))
and this is the flask application that will run in the gui
from flask import Flask ,render_template
app = Flask(__name__)
#app.route("/")
def hello():
return render_template("index.html")
if __name__ == "__main__":
app.run()
for any one ho want to use flask and pyqt there is an example
here https://github.com/smoqadam/PyFladesk
that work for me perfectly
EDIT:
Here is the same code with PySide6
https://github.com/eliaweiss/PySide6Fladesk.git

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

Why this simple example of pyqtgraph is not working?

The following code is just for testing the speed of the pyqtgraph. What I was expected is to get alternating graph forever. However, nothing is shown in the widget after executing this code. What is the problem?
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from random import randint, uniform
from math import *
import pyqtgraph as pg
import time
class Example(QWidget):
def __init__(self):
super().__init__()
self.x=pg.PlotWidget(self)
self.x.setMinimumHeight(400)
self.x.setMinimumWidth(400)
self.setWindowState(Qt.WindowMaximized)
self.u=[i+uniform(1,30) for i in range(1000)]
self.v=[-i+uniform(1,30) for i in range(1000)]
self.show()
def Run(self):
while 1:
self.x.clear()
self.x.plot(self.u)
self.x.clear()
self.x.plot(self.v)
app=QApplication(sys.argv)
ex=Example()
ex.Run()
sys.exit(app.exec_())
It generally a bad idea to use a while loop in a GUI. The problem is that it's preventing the GUI to remain responsive and handle all GUI events.
An option is to use a timer instead, e.g. a simple QTimer. In order to switch between the two different datasets to plot you would also introduce some mechanism as to which one should be shown.
import sys
#from PyQt5.QtWidgets import *
#from PyQt5.QtCore import *
from PyQt4 import QtGui, QtCore
from random import randint, uniform
import pyqtgraph as pg
class Example(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.x=pg.PlotWidget(self)
self.x.setMinimumHeight(400)
self.x.setMinimumWidth(400)
self.setWindowState(QtCore.Qt.WindowMaximized)
self.u=[i+uniform(1,30) for i in range(1000)]
self.v=[-i+uniform(1,30) for i in range(1000)]
self.switch = True
self.show()
def start(self):
self.timer = QtCore.QTimer(self)
self.timer.timeout.connect(self.run)
self.timer.start(500)
def run(self):
if self.switch:
self.x.clear()
self.x.plot(self.u)
else:
self.x.clear()
self.x.plot(self.v)
self.switch = not self.switch
app=QtGui.QApplication(sys.argv)
ex=Example()
ex.start()
sys.exit(app.exec_())

Why should I import QtGui and QtCore in PyQt when it isn't necessary?

I copied a PyQt example from the Web into a file and opened it up in PyCharm. Below is the code:
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from math import *
class Calculator(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.browser = QTextBrowser()
self.expression = QLineEdit()
self.expression.setPlaceholderText("Type an expression and press Enter.")
self.expression.selectAll()
layout = QVBoxLayout()
layout.addWidget(self.browser)
layout.addWidget(self.expression)
self.someWidget = QWidget()
self.someWidget.setLayout(layout)
self.setCentralWidget(self.someWidget)
self.expression.setFocus()
self.expression.returnPressed.connect(self.updateUi)
self.setWindowTitle("Calculator")
def updateUi(self):
try:
text = self.expression.text()
self.browser.append("%s = %s", (text, eval(text)))
except:
self.browser.append("<font color=red>Invalid Expression</font>")
def main():
import sys
app = QApplication(sys.argv)
window = Calculator()
window.show()
sys.exit(app.exec_())
main()
The problem is that the code run well even without adding the following import statements:
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from math import *
I have seen this example being used in many videos and books. If the code works good without the above statements, then why did the author of the example write the statements.
From PyQt4 to PyQt5, a lot of things moved from QtGui, QtCore to QtWidgets. To write a simple application in PyQt5, you're likely to need only QtWidgets.
My guess is that the code was originally written for PyQt4, and "adapted" to PyQt5, without removing useless imports.
The proper way to import would be import PyQt5.QtWidgets as QtWidgets ( see Should wildcard import be avoided ?).
The code then becomes:
class Calculator(QtWidgets.MainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.browser = QtWidgets.QTextBrowser()
self.expression = QtWidgets.QLineEdit()
...

Python and webkit Qt4

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?)

Categories

Resources