2

如何从 Python 程序中获取终端的状态?我希望程序在屏幕已满时停止在屏幕上打印行并等待用户输入。

4

2 回答 2

5

最简单(无代码)的方法是通过分页程序(如lessmore(假设为 *nix))通过管道传输程序的输出,例如:

无穷大.py

import random

while 1:
   print random.randint(0, 0xffffffff)

命令行

python infinty.py | less

...给出如下输出:

848605718
899092472
2576425641
3098821373
259379057
164782822
416064876
2488878735
1216764788
2682214542
531576871
2175787865
869960770
:

...并等待用户输入。

于 2012-10-26T13:47:04.507 回答
2

如果您需要对终端窗口进行更多控制,并且假设您使用的是 Linux/*BSD/MacOSX,那么您将需要为此使用curses。这只是一个简单的例子:

import curses
stdscr = curses.initscr()
stdscr.getmaxyx() #returns the width and height of the terminal screen
stdscr.getyx()    #returns the current x,y position.

您应该查看库参考以获取更多功能。

于 2012-10-26T13:39:19.233 回答