1

I have made a simple pygame window as shown below. But when I click somewhere on the screen (not the pygame window itself), the window goes to the back. Therefore, how do I make sure that my pygame window always or conditionally(when click a particular place on screen) stays on top?

This project of mine is needed to be run on Ubuntu platform

This is my simple program

# Simple pygame program


# Import and initialize the pygame library
import pygame

pygame.init()

# Set up the drawing window
# Set the size of the pygame window
window_width = 500
window_height = 500
window_size = (window_width, window_height)
surface = pygame.display.set_mode(window_size, pygame.RESIZABLE)


# Run until the user asks to quit

running = True

while running:


    # Fill the background with white

    surface.fill((255, 255, 255))


    # Draw a solid blue circle in the center

    pygame.draw.circle(surface, (0, 0, 255), (int(surface.get_width()/2),
      int(surface.get_height()/2)), 75)


    # Flip the display
    # magic
    pygame.display.flip()

    pygame.display.update()

    # gives window size
    # window_size = [pygame.display.Info().current_w, pygame.display.Info().current_h]
    # print(window_size)

    # Did the user click the window close button?

    for event in pygame.event.get():

        if event.type == pygame.QUIT:

            running = False

        if event.type == pygame.VIDEORESIZE:
            # There's some code to add back window content here.
            surface = pygame.display.set_mode((event.w, event.h),
                                              pygame.RESIZABLE)



# Done! Time to quit.

pygame.quit()
  • This problem has nothing to do with your code. It is not even related to pygame. – Rabbid76 Aug 30 '22 at 11:24
  • pygame is based on SDL, which offers a function [`SDL_SetWindowAlwaysOnTop`](https://wiki.libsdl.org/SDL_SetWindowAlwaysOnTop). It seems you can use [`pg_GetDefaultWindow`](https://www.pygame.org/docs/c_api/base.html?highlight=window#c.pg_GetDefaultWindow) to get the `SDL_Window*` required when calling this function. – Thomas Aug 30 '22 at 11:31
  • 1
    Does this answer your question? [Pygame set window on top without changing it's position](https://stackoverflow.com/questions/25381589/pygame-set-window-on-top-without-changing-its-position) – import random Aug 31 '22 at 03:55
  • @Thomas, Can you please show me an example coding for this SetWindowAlwaysOnTop function based on my project. It would help me alot. Thank in advance – Zubair Adil Aug 31 '22 at 04:48
  • @importrandom, Actually I have already gone through the link previously, that you have provided. But I could not resolve the issue, as I was getting some kind of error in my terminal. This might be due to the fact that the coding in that link is focused only for the Microsoft windows platform. However, my project needs to be run in Ubuntu – Zubair Adil Aug 31 '22 at 04:51
  • Have you tried something like `os.system("wmctrl -r :ACTIVE: -b toggle,above")`? On Ubuntu it should set the active window ([doc](https://manpages.ubuntu.com/manpages/jammy/en/man1/wmctrl.1.html)). – import random Aug 31 '22 at 05:17
  • 1
    @importrandom, Yes, I have tried it now and it did help me. Thanks a lot I also have tried with other code which gives much better result than the `xdo.activate_window()` because when every time it is run, it has some kind of 'glitch' (disappear and appear again within milliseconds) – Zubair Adil Aug 31 '22 at 06:55
  • Glad to hear that you have a solution, it's perfectly acceptable to [answer your own question](https://stackoverflow.com/help/self-answer). It might be helpful for future folks who find this question. – import random Aug 31 '22 at 07:05

0 Answers0