有什么方法可以将 Python IDLE 的交互式 shell 缩进宽度设置为 2 个空格而不是默认值?

Is there any way to set Python IDLE's interactive shell indentation width to 2 spaces instead of its default?

我不是在询问 Options > Configure IDLE > Fonts/Tabs > Indentation Width 并将其设置为 2。这只会设置文件内的缩进宽度,而不是交互式 shell.

的缩进宽度

我需要更改什么 Python IDLE 文件才能在交互式 shell 中获得 2-space 间距?

我喜欢用 2 space 编码,而不是 4例如一个文件。

不,目前没有。

Shell目前使用tabs缩进,tabs在tk中固定为8个'spaces'。我相信至少部分原因是 follow-up 行在视觉上是缩进的,尽管有 >>> 提示。

我也不喜欢这个。将来,我想将提示移到边栏中,以便输入的代码开始向左对齐,就像在编辑器中一样,并且可以使用与在编辑器中相同的 user-set 缩进。

有解决办法!!!*1

解决方案

转到idlelib (/lib/idlelib/) *2
打开“pyshell.py”
转到 class PyShell__init__

有3行

self.usetabs = True
# indentwidth must be 8 when using tabs.  See note in EditorWindow:
self.indentwidth = 8

只需将它们更改为

self.usetabs = False
# indentwidth must be 8 when using tabs.  See note in EditorWindow:
self.indentwidth = 4 # you decide, lot of people prefer 2

P.S.

这会导致看起来很奇怪:

>>> def f(x): # calculate x*abs(x)
    if x > 0: # it doesn't look it, but this is indented once
        return x*x # and this twice
    else:
        return -x*x

但我认为它看起来比 8 个空格制表符缩进更好

脚注

*1 我在 windows 10 上使用 python 3.8,但这可能适用于其他地方

*2 取决于 python 的安装位置
第 1 步:找到 python(对我来说是“C:\Programs\Python 3.8”)
第 2 步:从那里转到 /lib/idlelib/