简体   繁体   English

Linux终端显示和Python

[英]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: 所以我在想:

  1. When the user run the script, can I auto change the Linux terminal size to my declare size 当用户运行脚本时,我可以自动将Linux终端大小更改为我的声明大小
  2. Can I get the Width and Length of the user terminal size using Python, so the terminal display can be flexible 我可以使用Python获取用户终端大小的宽度和长度,因此终端显示可以灵活
  3. I would like to hear any better solution around the world to solve the terminal display problem! 我想听听全世界解决终端显示问题的更好解决方案!

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界面是一个熟悉的界面,这将使他们更喜欢你。

As Amber suggested, you should use a library like curses . 正如Amber建议的那样,你应该使用像curses这样的库。

Still, you could get the width of the terminal using something like this: 不过,您可以使用以下内容获取终端的宽度:

import subprocess
int(subprocess.Popen(['tput', 'cols'], stdout=subprocess.PIPE).stdout.read())

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.

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