Created
November 16, 2017 15:31
-
-
Save MitchRatquest/3b12e7bbce4a3e89bb81a368eb7e825f to your computer and use it in GitHub Desktop.
python-curses-example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
from time import sleep | |
import curses, curses.panel | |
COLUMNS=40 | |
ROWS=30 | |
side = 60 # < | |
top = 126 # ~ | |
bottom = 95 # _ | |
def make_panel(h,l, y,x, str): | |
win = curses.newwin(h,l, y,x) | |
win.erase() | |
win.border(side, side, top, bottom, side, side, side, side) | |
# win.box(curses.ACS_RARROW,curses.ACS_BULLET) | |
win.addstr(2, 2, str) | |
panel = curses.panel.new_panel(win) | |
return win, panel | |
def test(stdscr): | |
try: | |
curses.curs_set(0) | |
except: | |
pass | |
#stdscr.border(left, right, top, bottom, ulcorne, urcorner, blcorner, brcorner) | |
# stdscr.border(side, side, top, bottom, side, side, side, side) | |
# stdscr.box(124, 45) | |
curses.start_color() | |
# | |
# curses.init_pair(1, curses.COLOR_RED, curses.COLOR_WHITE) | |
curses.init_pair(1, curses.COLOR_BLUE, curses.COLOR_BLACK) | |
curses.init_pair(2, curses.COLOR_WHITE, curses.COLOR_BLACK) | |
string = "panels forever" | |
stdscr.addstr(0, (COLUMNS/2-(len(string)/2)), string, curses.color_pair(1)|curses.A_BOLD) | |
sleep(.5) | |
stdscr.addstr(0, (COLUMNS/2-(len(string)/2)), string, curses.color_pair(2)) | |
win1, panel1 = make_panel(10,12, 5,5, "PANEL 1") | |
win2, panel2 = make_panel(10,12, 8,8, "PANEL 2") | |
curses.panel.update_panels(); stdscr.refresh() | |
sleep(1) | |
panel1.top(); curses.panel.update_panels(); stdscr.refresh() | |
sleep(1) | |
curses.flash() | |
for i in range(20): | |
panel2.move(8, 8+i) | |
curses.panel.update_panels(); stdscr.refresh() | |
sleep(0.08) | |
sleep(1) | |
if __name__ == '__main__': | |
curses.wrapper(test) | |
Oh, python3 defaults to a float then, interesting. This was done in Python2.7. Thanks for the comment!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the example. I was running this example on python 3.5.2 and got this error:
Traceback (most recent call last):
File "example.py", line 57, in
curses.wrapper(test)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/curses/init.py", line 94, in wrapper
return func(stdscr, *args, **kwds)
File "example.py", line 36, in test
stdscr.addstr(0, (COLUMNS/2-(len(string)/2)), string, curses.color_pair(1)|curses.A_BOLD)
TypeError: integer argument expected, got float
The fix was to add int to lines 36 and 38
stdscr.addstr(0, int(COLUMNS/2-(len(string)/2)), string, curses.color_pair(1)|curses.A_BOLD)
stdscr.addstr(0, int(COLUMNS/2-(len(string)/2)), string, curses.color_pair(2))