The best way I can think of to accomplish this is in the following way.
Just think about it: we don't really care about the DPI or the resolution; we just want to adjust our output to our terminal window size. Precisely, the columns or width.
#!/usr/bin/python
import os
size = int(os.popen('tput cols').read().strip())
print('#' * size)
print('Welcome to my App').center(size))
print('#' * size,'\n\n')
i = input('What's your name? ')
We are using popen()
to run a shell command 'tput'
to get the columns('cols'
) of our window, getting the answer with read(
), striping the trailing new line with strip()
, converting it to an integer and then saving it to the variable size
.
Next we are printing pound signs(#) based on the number of columns (size
)
Next we are using center()
to align out text to the middle of the window(size
).
The rest I think is self explanatory we just added 2 new lines to the end of our bottom pound signs(#)
I hope this clears some things up for anyone that hasn't figured out how to ask the right question.
Links:
popen(), read(), strip(), center(), tput