user3432182
user3432182

Reputation: 1

Curses library in python

I have C code which draws a vertical & a horizontal line in the center of screen as below:

#include<stdio.h>
#define HLINE for(i=0;i<79;i++)\
                  printf("%c",196);
#define VLINE(X,Y) {\
                     gotoxy(X,Y);\
                     printf("%c",179);\
                   }
int main()
{
  int i,j;
  clrscr();
  gotoxy(1,12);
  HLINE
  for(y=1;y<25;y++)
      VLINE(39,y)
  return 0;
}

I am trying to convert it literally in python version 2.7.6:

import curses
def HLINE():
    for i in range(0,79):
        print "%c" % 45
def VLINE(X,Y):
    curses.setsyx(Y,X)
    print "%c" % 124
curses.setsyx(12,1)
HLINE()
for y in range(1,25):
    VLINE(39,y)

My questions:

1.Do we have to change the position of x and y in setsyx function i.e, gotoxy(1,12) is setsyx(12,1) ?

2.Curses module is only available for unix not for windows?If yes, then what about windows(python 2.7.6)?

3.Why character value of 179 and 196 are � in python but in C, it is | and - respectively?

4.Above code in python is literally right or it needs some improvement?

Upvotes: 0

Views: 1304

Answers (2)

Carnifex
Carnifex

Reputation: 11

  1. Yes, you will have to change the argument positions. setsyx(y, x) and gotoxy(x, y)

  2. There are Windows libraries made available. I find most useful binaries here: link

  3. This most likely has to do with unicode formatting. What you could try to do is add the following line to the top of your python file (after the #!/usr/bin/python line) as this forces python to work with utf-8 encoding in String objects:
    # -*- coding: utf-8 -*-
  4. Your Python code to me looks acceptable enough, I wouldn't worry about it.

Upvotes: 1

Helmut Grohne
Helmut Grohne

Reputation: 6788

  1. Yes.
  2. Duplicate of Curses alternative for windows
  3. Presumably you are using Python 2.x, thus your characters are bytes and therefore encoding-dependent. The meaning of a particular numeric value is determined by the encoding used. Most likely you are using utf8 on Linux and something non-utf8 in your Windows program, so you cannot compare the values. In curses you should use curses.ACS_HLINE and curses.ACS_VLINE.
  4. You cannot mix print and curses functions, it will mess up the display. Use curses.addch or variants instead.

Upvotes: 0

Related Questions