112madgamer
112madgamer

Reputation: 195

Python curses getting x and y

Okay I am writing a python application using curses and I am trying to center my text in a terminal window from the documentation you can get the x and y using this

curses.LINES and curses.COLS

I got this from here

I guess they return the x and y as a integer

Here is how I am doing this

screen.addstr(curses.LINES, curses.COLS, 'Please enter a number...', curses.color_pair(1))

but when I run the program i get this

Traceback (most recent call last):
                                    File "main.py", line 102, in <module>
                                                                             main()
     File "main.py", line 47, in main
                                         screen.addstr(curses.LINES/2, curses.COLS/2, 'Please enter a number...', curses.color_pair(1))
                                                       TypeError: integer argument expected, got float

I am dividing by 2 to get the center but it keeps throwing the error

Upvotes: 0

Views: 3115

Answers (1)

BlackJack
BlackJack

Reputation: 4679

You not only have to calculate the center of the screen but also move the starting point half the length of the text to the left. So the center of the text and the center of the screen fall onto the same coordinate.

text = 'Please enter a number...'
screen.addstr(
    curses.LINES // 2,
    curses.COLS // 2 - len(text) // 2,
    text,
    curses.color_pair(1)
)

Upvotes: 4

Related Questions