简体   繁体   English

如何在 Python IDLE Shell 中获取终端高度(以行为单位)?

[英]How do I get the terminal height (in lines) in Python IDLE Shell?

I'm creating a Hangman game in Python 3.5.我正在用 Python 3.5 创建一个 Hangman 游戏。 I'm trying to create a function to clear the console window, which I can do easily by using os.system("cls") on Windows or os.system("clear") for macOS, Linux etc. However, when running the script in the IDLE Shell, these commands do not work, so instead I am trying to print a series of newlines to hide all the previous content.我正在尝试创建一个函数来清除控制台窗口,我可以通过在 Windows 上使用 os.system("cls") 或在 macOS、Linux 等上使用 os.system("clear") 轻松完成。但是,在运行时IDLE Shell 中的脚本,这些命令不起作用,所以我尝试打印一系列换行符来隐藏所有以前的内容。

I am struggling to find the IDLE Shell height in lines.我正在努力寻找 IDLE Shell 的行高。 I've tried os.get_terminal_size()[1] but this gives the error "ValueError: bad file descriptor".我已经尝试过 os.get_terminal_size()[1] 但这给出了错误“ValueError:错误的文件描述符”。

Here is my current code:这是我当前的代码:

def clear():
"""Clears the console window.

Executes the 'cls' command when running on Windows, or 'clear' on
other platforms (e.g. Linux, macOS). IDLE shell cannot clear, so 
prints newlines instead.
"""
if "idlelib.run" in sys.modules:
    # If running in IDLE.
    print("\n" * os.get_terminal_size()[1])
elif platform.system() == "Windows":
    # If running in Windows terminal.
    os.system("cls")
else:
    # If running in other terminals (Linux, macOS)
    os.system("clear")

How would I go about finding the IDLE Shell size in lines?我将如何在行中查找 IDLE Shell 大小? Thanks in advance.提前致谢。

Well I do not think that is that easy... As you can see here for GUI apps the file handle to the output can be None.好吧,我认为这并不容易...正如您在此处看到的 GUI 应用程序,输出的文件句柄可以是 None。 That's the reason you can not get the size of the idle window using os.get_terminal_size() .这就是您无法使用os.get_terminal_size()获取空闲窗口大小的原因。 However when you just use a normal cmd terminal you can just use it.但是,当您只使用普通的 cmd 终端时,您就可以使用它。

Aside from that I would use shutil.get_terminal_size() .除此之外,我会使用shutil.get_terminal_size() This is the high level function as per this and should normally be used.这是根据this的高级功能,通常应该使用。

You problem is twofold.你的问题是双重的。 1. IDLE was designed, 20 years ago, for developing programs, not for users running them. 1. IDLE 是在 20 年前设计的,用于开发程序,而不是用于运行程序的用户。 2. Where it matters, os and shutil were designed to work with actual text terminals, with a fixed number of lines and columns, or with programs than imitate such. 2. 在重要的地方,os 和shutil 被设计为与实际的文本终端一起工作,具有固定数量的行和列,或者与模仿此类的程序一起工作。 They were were not designed to interact with GUI frameworks.它们不是为了与 GUI 框架交互而设计的。 There are separate modules for that.有单独的模块。

What you could do.你能做什么。

  1. Have users run your program on the system terminal (the usual default).让用户在系统终端(通常的默认设置)上运行您的程序。 For development, print something like "\\n**clear screen**\\n" to signal you, the developer, that the screen would be normally be cleared.对于开发,打印诸如"\\n**clear screen**\\n"以向您(开发人员)表示通常会清除屏幕。

  2. In the IDLE branch, print('\\n'*N) , where N is, say, 75, which should usually be enough.在空闲分支中, print('\\n'*N) ,其中 N 是,比如说,75,这通常应该足够了。 Or use a smaller number and inform users that you program assumes the window is at most N lines.或者使用较小的数字并通知用户您的程序假定窗口最多为 N 行。

  3. Learn enough tkinter to run your program.学习足够的 tkinter 来运行你的程序。 Clearing the screen is then text.delete('1.0', 'end-1c') .然后清除屏幕是text.delete('1.0', 'end-1c')

In python 3.3+ (text mode only):在 python 3.3+ 中(仅限文本模式):

>>> import os

>>> os.get_terminal_size()
os.terminal_size(columns=124, lines=52)

also:还:

>>> os.get_terminal_size().lines
52
>>> os.get_terminal_size()[1]
52

ok

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

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