7

I have a Tkinter window that I need to stay on top of everything else - including fullscreen windows.

Things I have tried:

root.attributes('-topmost', True) before mainloop - doesn't work

self.root.wm_attributes("-topmost", True) inside the class - doesn't work

root.overrideredirect(True) before mainloop - works on Linux only

root.lift() before mainloop - doesn't work

And finally, I set root.lift() to be called repetitively:

class TestingGUI:
    def __init__(self, root):
        self.root = root
        self.EnsureTop()

    def EnsureTop(self):
        root.lift()
        root.after(5000, self.EnsureTop)`

This works in the sense that it successfully raises the window over any other windows I've manually toggled to be 'always on top', using e.g. Dexpot, but crucially it doesn't work for fullscreen applications.

Specifically, I need this to be shown over games, as it's an in-game music player. I'm fine with solutions that require some command to be used repetitively to ensure the window is called to the top again.

heidi
  • 655
  • 6
  • 20
  • Sorry if this comment is a bit off-topic - the question itself is very good - but I can't understand people using Tkinter in 2016. Why don't you use wxWidgets, PySide, PyQt or PyGtk? – linusg Jun 21 '16 at 16:56
  • 2
    https://blogs.msdn.microsoft.com/oldnewthing/20110310-00/?p=11253 – TessellatingHeckler Jun 21 '16 at 16:57
  • @linusg It's part of Python, and it works just fine on Linux and for other small projects I've done. While you've made the comment, what would you suggest? – heidi Jun 21 '16 at 16:57
  • See the updated comment – linusg Jun 21 '16 at 16:57
  • @TessellatingHeckler I don't need my application to be on top in every single possible case, just for a narrow set of applications (Steam games). I'm pretty sure that they wouldn't engage in a cat and mouse game of wanting to be on top. – heidi Jun 21 '16 at 16:59
  • @linusg You haven't explained what's not ideal about Tkinter though. – heidi Jun 21 '16 at 17:00
  • Long ago I played around with the idea of drawing custom HUDs on top of fullscreen applications. I didn't get very far, but during my research I found this article: [Intercept Calls to DirectX with a Proxy DLL](http://www.codeguru.com/cpp/g-m/directx/directx8/article.php/c11453/Intercept-Calls-to-DirectX-with-a-Proxy-DLL.htm). Maybe it will be useful to you. – Kevin Jun 21 '16 at 17:07
  • @heidi They wouldn't aggressively, and they wouldn't fight back. But they already do usurp the existing window manager, take over the entire display, draw their own UI in order to 'be on top'. If you could raise your window in front it would be like alt-tab switching from the game - a clunky resolution change, taking the keyboard/mouse input away from the game. Steam itself has a normal friend chat interface for Windows, and a completely different in-game-overlay one because it can't just put the normal one there. – TessellatingHeckler Jun 21 '16 at 17:13
  • http://stackoverflow.com/questions/925981/overlaying-on-a-3d-fullscreen-application – TessellatingHeckler Jun 21 '16 at 17:15
  • @Kevin Thank you for your suggestion, but ideally this would be portable between various platforms... – heidi Jun 21 '16 at 17:16
  • @TessellatingHeckler I guess it's not possible without some dll hackery, then. I'll just recommend my users play in a borderless window. If you want to post an answer to that effect, I'll accept it if noone else answers soon. – heidi Jun 21 '16 at 17:19
  • Tkinter looks *awful*. I recommend either using the native OS look (wxWdgets) or a styleable GUI (PySide/PyQt). – linusg Jun 21 '16 at 17:53
  • @linusg Ah well if it's just for looks, the minimalist approach is just fine by me. I don't need it to look pretty in this case. – heidi Jun 21 '16 at 18:20
  • @linusg: _" but I can't understand people using Tkinter in 2016"_ : it's because tkinter is remarkably simple and powerful, and does probably 90% of what most people need in a desktop GUI. It's easy to learn, lightweight, and you likely already have it installed. – Bryan Oakley Jun 21 '16 at 19:00
  • @linusg: _"Tkinter looks awful."_: while it _can_ look awful, it can also look very good. If you make an awful looking tkinter application, you simply aren't trying hard enough. See http://www.tkdocs.com/tutorial/idle.html, for example. Besides, for the types of tools tkinter was designed for, functionality trumps being visually stunning. It's a very practical, pragmatic choice for a wide range of problems. – Bryan Oakley Jun 21 '16 at 19:03
  • @BryanOakley Thanks for that page! I didn't knew that you can evena apply themes to Tkinter! I see, with some effort, you can make a Tkinter application well looking. But IMO, using a modern Framework such as Qt is one is more preferrable. And if I want to make an application optimized for functionality, I'll use terminal outputs :P – linusg Jun 21 '16 at 19:23
  • @linusg: in my experience, most people who say "tkinter is ugly" are operating on several year old assumptions. Themed tk has been available in one way or another since around 2007, and was usable as far back as python 2.5 as an installable package. – Bryan Oakley Jun 21 '16 at 19:43
  • @BryanOakley - Anyway, that's my opinion. When I made my first tk app, I decided to switch to a more modern (yes, we can have different definitions here) GUI toolkit, and I don't think I'll ever switch back. – linusg Jun 22 '16 at 17:53

1 Answers1

2

As TessellatingHeckler pointed out in the comments, it's not possible to ensure a window stays on top of another fullscreen one. I solved this by combining a looping root.lift() and asking my users to run their applications in a borderless window.

heidi
  • 655
  • 6
  • 20