【问题标题】:Set Console Width (Windows, Python)设置控制台宽度(Windows、Python)
【发布时间】:2012-10-21 22:16:40
【问题描述】:

我正在使用 Python 开发一个基于文本的小型控制台游戏。因为我使用了一些 ASCII 艺术,所以我必须确保在我的游戏启动后每个人的控制台宽度都是相同的。谁能告诉我如何设置控制台的宽度和高度? :)

问候

弗洛

【问题讨论】:

标签: python console height width set


【解决方案1】:

a) 检查终端窗口的大小

import os

x = os.get_terminal_size().lines
y = os.get_terminal_size().columns

print(x)
print(y)

b) 改变终端窗口的大小

import os

cmd = 'mode 50,20'
os.system(cmd)

c) 改变终端窗口的颜色

import os

cmd = 'color 5E'     
os.system(cmd)

【讨论】:

  • 这回答了我的下一个问题!后续是针对3.3或更高版本,所以Python 2用户需要导入future。
  • 如何重置尺寸?
  • 这太棒了——但我想作为模式命令功能的一部分,滚动条会消失:-/
【解决方案2】:

最简单的方法是执行mode命令。

例如对于 80x25 窗口:

C:\> mode con: cols=25 lines=80

或者在 Python 中:

subprocess.Popen(["mode", "con:", "cols=25", "lines=80"])

【讨论】:

  • 好的,我导入了子进程,但它仍然无法正常工作:c 我收到“FileNotFoundError: [WinError 2] Das System kann die angegebene Datei nicht finden”错误 -.-
  • 好吧,我自己搞定了 ;) 我刚刚使用了 os.system("mode con: cols=25 lines=80")
  • 大声笑,mode 是 cmd 命令,不是可执行文件,subprocess.Popen(["mode", "con:", "cols=25", "lines=80"]) 肯定不行。
猜你喜欢
  • 2013-02-05
  • 2014-02-09
  • 2010-09-16
  • 2015-11-18
  • 2016-12-13
  • 1970-01-01
  • 2010-11-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多