-1

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?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • please provide a [mre], not external links. And even using the link code it is not possible to execute the code since elements that are not defined are missing – eyllanesc Jun 19 '21 at 05:33
  • You have published the same post without providing what was previously requested and for which your previous post was closed. Do you think your current post will not be closed? – eyllanesc Jun 19 '21 at 05:35
  • The only way to reproduce this error is to actually download and install the software, are saying I sould add this to the post? I didn't do it because I thought it wasn't a viable option. –  Jun 19 '21 at 05:37
  • And I deleted my own post because I thought it wasn't providing enough information... –  Jun 19 '21 at 05:38
  • 1) You need to provide a script that we can run (copy, paste and run the script) in order to reproduce the problem and thus find the reason for the problem. 2) What you provide does not comply with being an MRE so it does not have enough information. – eyllanesc Jun 19 '21 at 05:39
  • Like I've told you, the only to reproduce the error is by installing the software... there is no way to write a script for that. –  Jun 19 '21 at 05:44
  • But I'll try to write a shorter code that gives me the same error. –  Jun 19 '21 at 05:44
  • The MRE involves a complete script that is minimal and **reproduces** the problem. I do not understand what SW you mean, if to run the script you need to install other libraries or applications then you should point it out as well. Please read the link above in addition to [ask] and pass the [tour] – eyllanesc Jun 19 '21 at 05:50
  • @eyllanesc I just added what you mentioned. Could you take a look? –  Jun 19 '21 at 06:37
  • Okay, now it's better. I want to try to help you because of what I suspect of the error but I want to go further so I want to propose a solution but since I don't know a lot about anki I don't know how to run the script. I already installed anki but how can I run the script? Do you have a tutorial that indicates this? – eyllanesc Jun 19 '21 at 06:49
  • This is actually really nice of you. Is there a way we could chat in private so we could talk better? –  Jun 19 '21 at 06:53
  • @eyllanesc To run an addon go to `AppData\Roaming\Anki2\addons21` on your computer, then put this folder there: https://drive.google.com/drive/folders/1ztRWeTjYHMfD3P2ljW_LJaaF-GaYK_gf (Remember these folders have to be inside a parent folder, like (Anki2\addons21\ankiforprocrastinators). After that go to `C:\Program Files\Anki\anki-console` to run the program with a console. –  Jun 19 '21 at 07:11
  • To be honest there are very few good tutorials on Anki development. This mini tutorial I've sent you is probably the best you'll find. –  Jun 19 '21 at 07:29
  • 1) I could already run the addon but I have a question about your MRE (don't use anything from google drive) why do you use while True? 2) I also see commented code, does that code generate the error? – eyllanesc Jun 19 '21 at 07:31
  • The while True is just to simulate what is happening inside the real addon. It needs to be constantly checking a variable and run this function to decide wether or not if should apply the always on top flag. Is there something wrong with google drive? I was the one who uploaded the files. The commented code actually fixes the code. The uncommented code is what i'm trying to make happen with the anki main window. –  Jun 19 '21 at 07:45
  • The problem is that if testing too long code is complicated, therefore I always test part by part until I find the cause of the error. Checking the drive code I see that you are verifying `for tree in mw.col.sched.deckDueTree():`, do you want to check that data every X seconds and according to that make the window be ahead of the others? – eyllanesc Jun 19 '21 at 07:51
  • Yes. I need this variable to be as updated as possible. This is the number of cards you have to review each day. If the due cards > 0, disable your internet connection and reenable once you're done. –  Jun 19 '21 at 07:54
  • I did not understand the phrase: *disable your internet connection and reenable once you're done*, could you explain better – eyllanesc Jun 19 '21 at 07:55
  • Of course. This addon I'm writing is used to maximise your focus. So it gets the number of due cards and if it's > 0, the code disables your internet connection so you can focus 100% on reviewing your cards. However, I am disabling this feature and replacing by the Always on Top feature. This way, whenever you have cards to review, Anki just gets pinned on your screen until you're done. –  Jun 19 '21 at 07:58
  • This is the function I've found online that does the trick: https://paste.pythondiscord.com/ilomuwuyaz.properties –  Jun 19 '21 at 08:06
  • i'm having exactly the same problem with the Qt c++ bindings (not python), where if i toggle the `WindowStaysOnTopHint` flag, the application immediate exits. did you ever find a solution? – Jason C Mar 30 '23 at 14:27

1 Answers1

0

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)
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • This returns a slightly different error. It doesn't close, but once it runs `window.show()`, anki stops responding. –  Jun 19 '21 at 07:59
  • @Adriano Have you tested my code or have you tested your code? Your code does not respect the Qt rules since you have blocking tasks in the main thread – eyllanesc Jun 19 '21 at 08:14
  • Then how would I implement your solution to my code? This snippet is not intended to be used by itself. This is how I tried to merge both codes: https://paste.pythondiscord.com/sejogepoqe.py –  Jun 19 '21 at 08:20
  • Could you help me understand how to fix this problem of blocking the main thread? –  Jun 19 '21 at 08:21
  • @Adriano I have overextended myself unnecessarily. I just the problem that the OP shows in his MRE, nothing more. If you want more help then improve your MRE. Bye – eyllanesc Jun 19 '21 at 08:33
  • That was unecessarily rude. But thanks for your time. And I am the OP. –  Jun 19 '21 at 08:36
  • @Adriano 1) I have not been rude but clear, in SO we help solve problems based on the information you provide in the post, we do not go or intend to go further. One of the objectives of SO is to provide a collection of quality Q&A and that implies that the posts are self-contained. 2) OP: original poster, I mean the author of the question, I mean you. Please read [ask] and review the [tour] – eyllanesc Jun 19 '21 at 08:41
  • I inform on the post this is just to simulate the main anki addon and your answer does NOT provide a solution. –  Jun 19 '21 at 08:44
  • 1
    @Adriano It is precisely your simulation is not correct, if you want to perform a task every T seconds then use a QTimer. – eyllanesc Jun 19 '21 at 08:52