我正在用Python3.5创建一个Hangman游戏。我试图创建一个函数来清除控制台窗口,这可以通过在Windows上使用os.system("cls")或为macOS、Linux等使用os.system(“清除”)来实现。但是,当在空闲Shell中运行脚本时,这些命令不起作用,所以我尝试打印一系列新行以隐藏所有以前的内容。
我正在努力寻找闲置的壳牌的高度线。我已经尝试过os.get_terminal_size()1,但是这给出了错误"ValueError:坏文件描述符“。
以下是我的当前代码:
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")怎样才能找到行中的空闲Shell大小呢?提前谢谢。
发布于 2019-11-26 21:34:29
你的问题是双重的。1.空闲是20年前为开发程序而设计的,而不是为运行这些程序的用户设计的。2.在重要的情况下,os和shutil被设计成与实际的文本终端一起工作,有固定数量的行和列,或者与程序一起工作,而不是模仿它们。它们不是为与GUI框架交互而设计的。这方面有单独的模块。
你能做的。
"\n**clear screen**\n"的东西,向开发人员发出信号,显示屏幕通常是被清除的。print('\n'*N),其中N是,比方说,75,这通常是足够的。或使用较小的号码,并通知用户,您的程序假设窗口最多为N行。text.delete('1.0', 'end-1c')。发布于 2019-11-26 15:31:59
发布于 2022-01-12 20:13:13
在python 3.3+中(仅限于文本模式):
>>> import os
>>> os.get_terminal_size()
os.terminal_size(columns=124, lines=52)此外:
>>> os.get_terminal_size().lines
52
>>> os.get_terminal_size()[1]
52好的
https://stackoverflow.com/questions/59053782
复制相似问题