0

Program Info & Problem

I have created a Python Program Using Pygame Module which displays the Ads on the monitor. It shows The Ad on screen But as soon as I launch different applications like kodi or vlc or chrome, etc. It goes behind those applications.

The Problem is: The program runs but behind those applications if these applications are launched after my Ad Program.

Ideal Working

  1. Program Laucnhed
  2. Ad Displayed on screen
  3. Launched Other Application
  4. The Program still displayes the ad on top of screen.

System Info

OS: Linux - Ubuntu 20

Language: Python

Module: Pygame, Pymovie, GTK3+

Architecture: amd64

Desktop Enviroment: OpenBOX

Code Launch: CLI using a bash script which launches the python program of advertisment.

Sample Screenshot of Advertiesment

Advertiesment Sample

Please Help! Thank you.

2 Answers2

0

Looks like the best answer I can find is from this outdated website (https://www.mail-archive.com/pygtk@daa.com.au/msg01370.html) they say to use code below, it should work on all OSs...things are never that easy

transient.set_transient_for(main_window)

Alternatively I have four other answers lol

Taken from (How to keep a python window on top of all others (python 3.1))

stackoverflow user pyfunc says for windows you can just do

import win32gui
import win32con
win32gui.SetWindowPos(hWnd, win32con.HWND_TOPMOST, 0,0,0,0,
win32con.SWP_NOMOVE | win32con.SWP_NOSIZE)

Since this only works for windows I think you should try python-tinker, I believe it works on linux

root = Tk()
root.wm_attributes("-topmost", 1)

Also whatnick says you can use PyGTK

gtk.Window.set_keep_above

reference: How to make python window run as "Always On Top"?

Let me know if any of these work for you, I will keep looking for a better answer.

Jacob Glik
  • 223
  • 3
  • 10
  • Hi @jacob-glik Thanks for providing me the Answer but seems like the above solution are for windows operating system. Where win32gui, win32con and tinker works like charm. But my problem is for Linux operating system. I am unable to use ```gtk.Window.set_keep_above``` and ```transient.set_transient_for(main_window)``` on my code. I mean I don't know where I have to put this line in my code neither I can find proper documentation on sites. – Amogh Saxena - REXTER Feb 01 '22 at 07:04
0

I think PyWinCtl can make the trick in most cases. You can invoke alwaysOnTop() once for your window, or you can call it inside your loop to assure it stays on top after other apps open. Check this:

import tkinter as tk
import pywinctl as pwc


class Window(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        self.overrideredirect(True)
        self.geometry('300x200')
        self.config(background='black')
        self.attributes('-alpha', 0.5)

        self.label = tk.Label(text="Hello World!")
        self.label.pack(fill=tk.BOTH, expand=1)

        self.wait_visibility()
        self.window = pwc.Window(int(self.frame(), base=16))
        # Call it once at the beginning of your program...
        try:
            self.window.alwaysOnTop()
        except:
            pass

        self.counter = 0
        self.display()

    def display(self):

        if self.state() == "normal":
            try:
                # ... or call it repeatedly to assure it stays on top
                self.window.alwaysOnTop()
            except:
                # On Linux, sometimes it takes more time to recognize the new window
                self.window = pwc.Window(int(self.frame(), base=16))

        self.label.config(text=str(self.counter))
        self.counter += 1
        self.after(1000, self.display)

root = Window()
root.mainloop()
Kalma
  • 111
  • 2
  • 15
  • Hi!, Actually I have been through a lot of RND and got the conclusion that on Linux OS Always on top Down not depend on the Program. It depends on the Window Manager and DEs. – Amogh Saxena - REXTER Dec 06 '22 at 07:16
  • Hi! Agree. Your conclusion is totally right. Furthermore, the combinations of distributions, WMs and Desktops in Linux is a nightmare! Anyway, I got it working fine on Ubuntu, Mint and Raspbian... In most cases (let's say that the "fight" for being the top-most window is not always easy to be won!) – Kalma Dec 07 '22 at 08:45