2

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

The Duck
  • 25
  • 7

1 Answers1

2

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)
Alexander L. Hayes
  • 1,657
  • 2
  • 11
  • 23
  • I have another while loop at the end with user inputs. Where do I put the if statements? Do I put the if inside the other while loop? – The Duck Dec 15 '20 at 17:39
  • If statements/another while loop could probably be handled in a second function; then that second function could be after the `title(offset)` call. There is a side effect though: if you're waiting for user input with something like `var = input()`, then the terminal is only redrawn **after** the user provides an input. – Alexander L. Hayes Dec 15 '20 at 17:43
  • How to you sense when the user changes the window size? I can now fix the text, but I found some problems with my other scripts, I can create a if statement that checks if the user changes the window size. – The Duck Dec 15 '20 at 17:55
  • "*Checking and responding to events*" is a little more complicated (and might have to be a separate question), it may require a thread checking the state and then responding to events. – Alexander L. Hayes Dec 15 '20 at 18:07
  • Another option could be something like "Python Prompt Toolkit" https://python-prompt-toolkit.readthedocs.io/en/master/index.html# – Alexander L. Hayes Dec 15 '20 at 18:07