SetWindowFlags immediately closes the program - python

I am trying to run the following script inside a software called Anki. This is basically an addon (a plugin) that makes the program to be always on top. This is the MRE:
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QWidget
import time
app = QApplication([])
window = QWidget()
window.show()
def start():
while True:
window.setWindowFlags(Qt.WindowStaysOnTopHint)
#window.show()
#app.exec()
time.sleep(1)
start()
This is the closer I could get to the actual error I'm getting. Whenever the program runs the line window.setWindowFlags(Qt.WindowStaysOnTopHint), it immediatly closes the window and becomes unable to reach window.show(). Since this is just a plugin to the base app, I assume all plugins are loaded AFTER the main window has opened and within the main window. So if you close the main window, all plugins also stop running. How can I prevent the main window from closing?

There should only be one QApplication, and you don't have to create a new QApplication since anki creates it. Nor is a while True necessary since it will block the eventloop and even less use time.sleep since it also blocks the eventloop.
import time
from aqt import gui_hooks
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QWidget
window = QWidget()
window.setWindowFlags(window.windowFlags() | Qt.WindowStaysOnTopHint)
gui_hooks.main_window_did_init.append(window.show)

Related

Loading bar job doesn't render till job is complete PyQt5

I have created a UI using PyQt5 that contains a progress bar. I have coded this progress bar in python so that it increases to 99% in real time. The problem I am facing is that the window does not load until the job is complete. I have read upon similar posts which talk about threading and I cannot possibly grasp anything from it. I would appreciate if someone could explain to me a solution and if it does require threading, an aspect I am yet to learn, please explain it to me in laymans term.
import sys, os, sqlite3
import random, datetime
from PyQt5 import QtCore, QtGui, uic
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton,
QMessageBox, QProgressBar, QSplashScreen
import sqlite3
import time
window2 = uic.loadUiType("login_loadingbar.ui")[0]
class LoadingBar(QtWidgets.QMainWindow, window2):
def __init__(self, parent=None):
QtWidgets.QMainWindow.__init__(self, parent)
self.setupUi(self)
#title
self.setWindowTitle('Loading')
#makes progress bar go from 0-100, time scaled
def progress(self):
for i in range(100):
time.sleep(0.1)
self.login_progressBar.setValue(i)
app = QApplication(sys.argv)
w2 = LoadingBar(None)
w2.show()
w2.progress()
app.exec_()
When you create gui application you have one thread, in this thread there's thing called event loop - its essentially while true loop that handles events - redraws widgets when necessary, dispatches keyboard and mouse input to related widgets.
Since you have only one thread you can only do one thing at a time, so when you do long operation like time.sleep(), event loop is frozen - no widgets gets redrawn, no inputs handled until it completes and next iteration of event loop reached.
You can create second thread and move operation there or in simple cases when operation can be split into pieces - process events in event loop in between pieces:
for i in range(100):
time.sleep(0.1)
self.login_progressBar.setValue(i)
QtWidgets.QApplication.processEvents()
This way event loop kinda works, but application is not 100% responsive, because every time.sleep(0.1) freezes it, so mouse clicks and other events may not be handled.

PyQt5 resizes tkinter window automatically

I am trying to open a web page using PyQt5 after a button press in tkinter window.
As soon as the new window opens, it resizes (downsizes in this case) the tkinter window permanently.
Minimal code required to reproduce this
import sys
from PyQt5.QtCore import *
from PyQt5.QtWebEngineWidgets import *
from PyQt5.QtWidgets import QApplication
from tkinter import *
from threading import Thread
class Web:
def __init__(self,url,title='',size=False):
self.title=title
self.url=url
self.size=size
self.app=QApplication(sys.argv)
self.web=QWebEngineView()
self.web.setWindowTitle(self.title)
self.web.load(QUrl(self.url))
if size:
self.web.resize(self.size[0],self.size[1])
def open(self):
self.web.show()
sys.exit(self.app.exec_())
def launch():
web=Web('https://www.example.com')
web.open()
root=Tk()
button=Button(root,text='open',command=lambda:Thread(target=launch).start())
button.pack(padx=100,pady=100)
root.mainloop()
Images for reference
Both the images have the same height.
I would like to know the reason and a way to prevent this.
I have figured it out myself. PyQt changes the dpi awareness which does not happen by default with tkinter. Due to which the tkinter window resized itself as soon as PyQt was launched in the same main loop.
Since I am on a windows machine, using this solved the problem.
ctypes.windll.shcore.SetProcessDpiAwareness(1)

How to stop qt app from freezing the main program?

For example:
#!/usr/bin/env python3
import sys
from PySide import QtCore, QtGui
class Dialog(QtGui.QDialog):
def __init__(self):
QtGui.QDialog.__init__(self)
button = QtGui.QPushButton("test")
layout = QtGui.QVBoxLayout()
layout.addWidget(button)
self.setLayout(layout)
app = QtGui.QApplication(sys.argv)
toast = Dialog()
toast.show()
app.exec_()
print("App freezes the main process!")
The last print() function will not be executed until you close the dialog.
I am working on a script that only uses qt for displaying some content that does not require user interaction, so I would prefer the gui code runs in background.
This is not possible. Qt documentation states:
Although QObject is reentrant, the GUI classes, notably QWidget and all its subclasses, are not reentrant. They can only be used from the main thread. As noted earlier, QCoreApplication::exec() must also be called from that thread.
(emphasis mine)
This answer suggests on the other hand that in reality this is not true :) However it seems that PySide sticks to the official version:
This can be verified by the following code sample:
import sys
import threading
from PySide import QtCore, QtGui
class Dialog(QtGui.QDialog):
def __init__(self):
QtGui.QDialog.__init__(self)
button = QtGui.QPushButton("test")
layout = QtGui.QVBoxLayout()
layout.addWidget(button)
self.setLayout(layout)
app = QtGui.QApplication(sys.argv)
toast = Dialog()
toast.show()
t = threading.Thread(target = lambda: app.exec_())
t.daemon = True
t.start()
print("App freezes the main process!")
input()
which produces the following output:
App freezes the main process!
QApplication::exec: Must be called from the main thread
(and a crash, on my machine). I have also verified the option with creating the app within the other thread - it works, but crashes on exit.
So the solution seems to let Qt have the main thread, and organize your processing in a separate thread. This shouldn't really be a problem: if you'll separate your concerns well it won't make a difference for your console-app part on which thread it's running.
I'm not sure if the PySide imposes any restrictions, but here's how it's done in C++:
Instantiate QApplication in a secondary thread.
Create your dialog in that same thread.
Call either QDialog::exec() OR {QApplication::exec() plus QDialog::show()} in that same thread.
Make sure that your secondary thread has fully shut down before you quit your app.
Yes, the Qt documentation currently says that only the main thread is allowed. However, there is nothing in the Qt source code that forbids creating QApplication in a secondary thread and then using GUI classes in that thread (for Windows and Linux). The documentation should be changed.
Mac OS X is different though -- the Cocoa framework only allows GUI operations in the main thread.

PyQt: Qt.Popup widget sometimes loses focus without closing, becomes unclosable

I'm writing a very small application with PyQt. All of my testing has been on Ubuntu/gnome so far.
I want a single "Popup" style window, with no taskbar/panel entry, that will close itself (and the application) the moment it loses focus.
The Qt.Popup flag seems to fit the bill, but I'm having a weird problem. I've noticed that it's possible (pretty easy, in fact) to take focus away from the application as it's starting, leaving a Popup window with no focus -- and it is now impossible to close it, because it cannot lose focus.
Here's a simplified example:
#!/usr/bin/python
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
if __name__ == '__main__':
app = QApplication(sys.argv)
w = QDialog()
w.setWindowFlags(Qt.Popup)
w.exec_()
If you click around a bit within the same moment the program is starting, the QDialog will appear without focus, and will not close itself under any circumstance. Clicking on the popup does not restore focus or allow it to be closed.
I could add a close button to the popup (and I intend to!) but that doesn't fix the broken close-on-lost-focus behavior. Is there something else I should be doing with Qt.Popup windows to prevent this, or is there some way I can work around it?
Using QWidget::raise() seems to help here.
(Also took the liberty and fixed your app event loop)
#!/usr/bin/python
import sys
#import time
from PyQt4.QtCore import *
from PyQt4.QtGui import *
if __name__ == '__main__':
#time.sleep(2)
app = QApplication(sys.argv)
w = QDialog()
w.setWindowFlags(Qt.Popup)
w.setAttribute(Qt.WA_QuitOnClose)
w.show()
w.raise_()
sys.exit(app.exec_())

PyQt4: Stop Window from taking Focus

What I am trying to do is make an on screen keyboard.
To do this I need to stop the Program from taking focus away from other windows.
Here is the code I have that keeps the window on top.
import sys
from PyQt4 import QtGui, QtCore, Qt
class mymainwindow(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self, None, QtCore.Qt.WindowStaysOnTopHint)
app = QtGui.QApplication(sys.argv)
mywindow.show()
app.exec_()
(Note: Example from Keep Window on Top)
So what I want to do is add code to stop the window taking focus.
Thanks
Change focus policy of window and all of its contents QWidget::setFocusPolicy

Categories