0

I'm new to Python and have mostly learnt C# in the past. I am creating a QWidget class:

class Window(QWidget):
    def __init__(self, gif, width, height):
        super().__init__()

        self.setGeometry(400, 200, width, height)
        self.setWindowTitle("Python Run GIF Images")
        self.setWindowIcon(QIcon('icons/qt.png'))

        label = QLabel(self)
        movie = QMovie(gif)
        label.setMovie(movie)
        movie.start()

And then define a function that creates a QApplication and then the Window:

def run_gif(gif, width, height):

    app = QApplication([])
    window = Window(gif, width, height)
    window.show()
    app.exec()
    app.shutdown()

My issue is getting the gif to show topmost when it is launched. There is a Topmost property you can set on a Winform in C# which means no other window or other app you click on will cover the Window. This isn't essential but I want it to at least be shown above the code editor it is launched from so that the user doesn't have to select the Window to see the gif contained in the Window. I've spent hours looking at properties I can set either in the class or the method and not getting the result I need.

However, when I call the run_gif method for the second time, it does show it topmost, but along with the below exception (at least I think it's an exception from what I'm reading, it doesn't affect the running of the program other than printing to the console).

QApplication::regClass: Registering window class 'Qt640ScreenChangeObserverWindow' failed. (Class already exists.)

The only advice I can find on this error is not Python-specific and I don't really understand it. Maybe I'm being a bit ambitious for my early stage learning Python but not really seeing anything much on this error.

This is using PySide6.

  • Your question is unclear. What do you mean by "show topmost"? Also, except for the `run_gif()`, what are you normally doing? – musicamante Nov 21 '22 at 17:21
  • In C# there is a property you can set on a Winform, this.Topmost = true, which means whatever window you click on, the Winform will remain visible on top of the others. So I thought topmost might be a recognisable term for this across languages. My app doesn't do much but run gifs. It's basically just a text interface that asks you what gif you want to run. – ShaunHunter Nov 21 '22 at 19:25
  • So you want to display a *window* that always stays above the other windows of your program? – musicamante Nov 22 '22 at 01:00
  • Yes that's correct. Well, above all other windows or apps – ShaunHunter Nov 22 '22 at 05:29
  • Sorry, but you need to be more aware of the terms you're using. Making a window above all the others of the same **program** is one thing, above *any* other window of the OS is a very different matter. – musicamante Nov 22 '22 at 05:34
  • I want a way to do in Python what this.Topmost does in C# – ShaunHunter Nov 22 '22 at 05:40
  • If the user is running the app from the console there shouldn't be any other apps on top of that anyway, apologies if I'm confusing things, but in C# the Topmost property means no other app or window you click on will cover the Window. – ShaunHunter Nov 22 '22 at 05:46
  • @musicamante I have updated my question. Do you feel I have now explained it satisfactorily? If so perhaps we could delete this exchange. – ShaunHunter Nov 23 '22 at 09:03

1 Answers1

0

Using setWindowFlag(Qt.WindowType.WindowStaysOnTopHint, True) seems to be working for me so far.

Example:

from PySide6.QtWidgets import *
from PySide6.QtCore import *
from PySide6.QtGui import *


class Window(QWidget):

    def __init__(self, parent=None):
        super().__init__(parent=parent)
        self.layout = QVBoxLayout(self)
        self.resize(200,100)
        self.label = QLabel("Always on top...")
        self.layout.addWidget(self.label)
        self.setWindowFlag(Qt.WindowType.WindowStaysOnTopHint, True)  # <--

if __name__ == '__main__':
    app = QApplication([])
    window = Window()
    window.show()
    app.exec()
Alexander
  • 16,091
  • 5
  • 13
  • 29
  • Brilliant. Thanks Alexander. I've just imported QtCore and added that one line and it does exactly what I need. However, I am still getting this error message output to the console when the second Window is launched: "QApplication::regClass: Registering window class 'Qt640ScreenChangeObserverWindow' failed. (Class already exists.)" If I search this I only find 1 answer on Google which is not Python-specific. However, that would be combining two questions into one so I'll accept your answer for the main question I've asked and raise a separate question unless anyone has any insight here. – ShaunHunter Nov 23 '22 at 11:18