[英]Linux Terminal Display and Python
I am writing a Python script to print out displayable user interface. 我正在编写一个Python脚本来打印出可显示的用户界面。 The problem is every Linux user would have their own unique terminal size.
问题是每个Linux用户都有自己独特的终端大小。 This will cause the hard-coded user interface to go out of format.
这将导致硬编码的用户界面超出格式。
(If there is a lot of example below, the terminal looks Crazy!!!). (如果下面有很多例子,终端看起来很疯狂!!!)。
Example, in the script. 例如,在脚本中。 I have print out:
我打印出来了:
print "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
Format should goes well in my terminal: +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 我的终端格式应该很好:+++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++++
When the terminal is smaller, the print out format will run out. 当终端较小时,打印输出格式将用完。 Format become: ++++++++++++++++++++++++++++++++++++++++++++
格式变为:+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++
So I am thinking: 所以我在想:
I would strongly prefer recommendation in Python 我非常希望在Python中推荐
I'd highly suggest using something like the Python Standard Library's curses
module to do this. 我强烈建议使用类似Python标准库的
curses
模块来执行此操作。
Don't reinvent the wheel - using an existing library will both help you avoid corner cases and also save you time. 不要重新发明轮子 - 使用现有的库既可以帮助您避免角落情况,也可以节省您的时间。 Plus, the curses interface is a familiar one to *nix users, which will make them like you more.
另外,对于* nix用户来说,curses界面是一个熟悉的界面,这将使他们更喜欢你。
Based on the comments in Amber's solution , there is some desire to see a solution that works on Windows too. 根据Amber解决方案中的评论,我们希望看到一个适用于Windows的解决方案。 A simple cross platform solution is to use asciimatics .
一个简单的跨平台解决方案是使用asciimatics 。 For example:
例如:
from asciimatics.screen import Screen
def demo(screen):
screen.print_at('+' * screen.width, 0, 0)
screen.refresh()
sleep(10)
Screen.wrapper(demo)
This package also provides a whole load of higher level widgets to make full screen text UIs easier. 此软件包还提供了一整套更高级别的小部件,使整个屏幕文本UI更容易。 See the contact list demo for an example.
有关示例 ,请参阅联系人列表演示 。
Full disclosure: yes - I am the author of that package and so might be a little biased. 完全披露:是的 - 我是该套餐的作者,因此可能有点偏颇。 :-)
:-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.