简体   繁体   English

python中的curses库

[英]Curses library in python

I have C code which draws a vertical & a horizontal line in the center of screen as below:我有 C 代码,它在屏幕中央绘制一条垂直线和一条水平线,如下所示:

#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:我试图在 python 版本 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 ie, gotoxy(1,12) is setsyx(12,1) ? 1.我们是否必须改变setsyx函数中x和y的位置,即gotoxy(1,12)是setsyx(12,1)?

2.Curses module is only available for unix not for windows?If yes, then what about windows(python 2.7.6)? 2.Curses 模块只适用于 unix,不适用于 windows?如果是,那么 windows(python 2.7.6) 呢?

3.Why character value of 179 and 196 are in python but in C, it is | 3.为什么179和196的字符值在python中是 而在C中却是| and - respectively?和 - 分别?

4.Above code in python is literally right or it needs some improvement? 4.python 中的以上代码字面上是正确的还是需要一些改进?

  1. Yes, you will have to change the argument positions.是的,您将不得不更改参数位置。 setsyx(y, x) and gotoxy(x, y) setsyx(y, x)gotoxy(x, y)

  2. There are Windows libraries made available.有可用的 Windows 库。 I find most useful binaries here: link我在这里找到最有用的二进制文件:链接

  3. This most likely has to do with unicode formatting.这很可能与 unicode 格式有关。 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:您可以尝试将以下行添加到 python 文件的顶部(在#!/usr/bin/python行之后),因为这会强制 python 在 String 对象中使用 utf-8 编码:
     # -*- coding: utf-8 -*-
  4. Your Python code to me looks acceptable enough, I wouldn't worry about it.你的 Python 代码对我来说看起来已经足够了,我不会担心。
  1. Yes.是的。
  2. Duplicate of Curses alternative for windows Windows 的重复诅咒替代方案
  3. Presumably you are using Python 2.x, thus your characters are bytes and therefore encoding-dependent.大概您使用的是 Python 2.x,因此您的字符是字节,因此依赖于编码。 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.很可能您在 Linux 上使用 utf8,而在 Windows 程序中使用非 utf8,因此您无法比较这些值。 In curses you should use curses.ACS_HLINE and curses.ACS_VLINE .在curses 中,您应该使用curses.ACS_HLINEcurses.ACS_VLINE
  4. You cannot mix print and curses functions, it will mess up the display.您不能混合使用printcurses功能,它会弄乱显示。 Use curses.addch or variants instead.改用curses.addch或变体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM