Search code examples
pythontext-alignmenttext-processing

How to detect window resize in Python


How do you see if the window has been resized? I start in full screen, but if I change so it fits half of my screen, all my text gets mixed up.

Here is my code:

# Import os
import os

# Create function called title that prints the title screen
def title():
    print('@------------------------------------------------------------------------------------@'.center(os.get_terminal_size().columns), end='')
    print('|                                                                                    |'.center(os.get_terminal_size().columns), end='')
    print('|                                      |PyTerm v0.1.7|                               |'.center(os.get_terminal_size().columns), end='')
    print('|                                                                                    |'.center(os.get_terminal_size().columns), end='')
    print('@------------------------------------------------------------------------------------@'.center(os.get_terminal_size().columns), end='')

# Call the function
title()

Once I run it, the text gets aligned, but when I go into fullscreen, they get all over the place. Is there a way to fix this? Can I somehow sense when the user resizes the window? I'm on Windows 10, using Python 3.1.9. Running it with py ( C:\Windows\py.exe )

This is what it looks like when I run it: Before picture

Here is it when I go into fullscreen: After picture

Thanks


Solution

  • I don't have a Windows machine to test on, but one possibility would be to redraw the console in a loop:

    1. Clear the current screen (see: Clear terminal in Python)
    2. Run the title function
    3. Wait for a little while

    If the user resizes the screen, the resized value of the offset variable should be set to the new size.

    import os
    import time
    
    
    def title(offset_value):
        print('@------------------------------------------------------------------------------------@'.center(offset_value), end='')
        print('|                                                                                    |'.center(offset_value), end='')
        print('|                                      |PyTerm v0.1.7|                               |'.center(offset_value), end='')
        print('|                                                                                    |'.center(offset_value), end='')
        print('@------------------------------------------------------------------------------------@'.center(offset_value))
    
    
    if __name__ == "__main__":
    
        while True:
    
            # Clear the terminal
            # See: https://stackoverflow.com/questions/2084508/clear-terminal-in-python
            os.system('cls' if os.name == 'nt' else 'clear')
    
            # Redraw the screen
            offset = os.get_terminal_size().columns
            title(offset)
    
            # Wait for a bit
            time.sleep(1)