Is there a way to deiconify() or maximize a pyGame window?

Viewed 374

I'm currently making a graph plotter and I'm in the early stages so Im just using the shell to get inputs from the user at the moment. However, due to other parts of my program, I need the pygame window to be open before they begin their inputs (I cannot change the order of this as their inputs are gotten by a function and I don't really want to open pygame in this function). This blocks the shell so I used pygame.display.iconify() which minimized the pygame window doing what I needed.

My problem is that when you have completed the inputs the pygame window is still minimized and I want it to be back as an active window. Is there such a thing that does the opposite of iconify() or should I change my code completely?

Thanks.

2 Answers

There's no way to do this with pygame only, but if you're on Windows, you can use the pywin32 package.

Here's an example that will minify and restore the window every second:

import pygame
import win32gui
import win32con

def main():
    pygame.init()
    clock = pygame.time.Clock()
    screen = pygame.display.set_mode((200, 200))
    hwnd = win32gui.GetForegroundWindow()
    
    EVENT = pygame.USEREVENT + 1
    pygame.time.set_timer(EVENT, 1000)
    while True:

        for event in pygame.event.get():
            pos = pygame.mouse.get_pos()
            if event.type == pygame.QUIT:
                return
            if event.type == EVENT:
                if win32gui.IsIconic(hwnd):
                    win32gui.ShowWindow(hwnd, win32con.SW_SHOWNOACTIVATE)
                    win32gui.BringWindowToTop(hwnd)
                else:
                    pygame.display.iconify()

        screen.fill((30, 30, 30))
        clock.tick(30)
        pygame.display.flip()
main()

Don't use pygame.display.iconify() as there is no pygame.display.uniconify() counterpart.

There is this ugly hack, where you can use pygame.display.set_mode() to hide the window, and then regain focus and size, when needed:

# Minimize window
pygame.display.set_mode((1,1)) 

# Restore window
pygame.display.set_mode((1024, 768)) 

You should write your plot on a surface, independent of the display surface, and then blit and flip it, when you need to display it.

Related