将多行粘贴到 IDLE 中

发布于 2024-08-09 14:24:06 字数 419 浏览 2 评论 0原文

有没有办法将代码块粘贴到 IDLE 中?逐行粘贴是可行的,但有时我想一次粘贴多行。当我尝试时,IDLE 会读取第一行并忽略其余行。

>>> a = 1
b = 2
c = 3

>>>
>>> a
1
>>> b

Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    b
NameError: name 'b' is not defined

Is there a way to paste a block of code into IDLE? Pasting line by line works, but sometimes I'd like to paste many lines at once. When I try, IDLE reads the first line and ignores the rest.

>>> a = 1
b = 2
c = 3

>>>
>>> a
1
>>> b

Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    b
NameError: name 'b' is not defined

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

若能看破又如何 2024-08-16 14:24:06

可能不是最漂亮的过程,但这是可行的:

cmds = '''

粘贴您的命令,然后添加 '''

a = 1
b = 2
c = 3
'''

然后 exec(cmds) 将执行它们。

或者更直接,

exec('''

然后粘贴你的命令,后面加上 ''')

a = 1
b = 2
c = 3
''')

这只是一个技巧,也许有更官方、更优雅的方式。

Probably not the most beautiful procedure, but this works:

cmds = '''

paste your commands, followed by ''':

a = 1
b = 2
c = 3
'''

Then exec(cmds) will execute them.

Or more directly,

exec('''

then paste your commands, followed by '''):

a = 1
b = 2
c = 3
''')

It's just a trick, maybe there's a more official, elegant way.

远山浅 2024-08-16 14:24:06

IdleX 为 IDLE 提供 PastePyShell.py 扩展,允许将多行粘贴到 shell 中执行。

IdleX provides the PastePyShell.py extension for IDLE which allows pasting of multiple lines into the shell for execution.

初心未许 2024-08-16 14:24:06

请参阅另一篇文章:Python,在 IDLE 中编写多行代码
您可以使用编辑器(文件 > 新文件),在那里编写代码行并使用 F5

See this other post: Python, writing multi line code in IDLE
You can use an editor (File > New File), write your lines of code there and use F5

柠檬 2024-08-16 14:24:06

基于 RedGlyph 的答案,但使用 AutoHotkey (AHK) 进行一些自动化:

; Python IDLE shell
#IfWinActive ahk_exe pythonw.exe

^+x::  ; IDLE - multiple commands paste
SoundBeep,1700, 150
send, ^{end}
var1 = cmds = '''
var2 := clipboard
var3 = %var1%%var2%
var4 = %var3%'''
msgbox,,, %var4%,2
clipboard = %var4%
send, ^{vk0x56}   ;send, ^v
send, {enter}
sleep, 1000
send, exec(cmds)
return

您需要安装 AutoHotkey 即可实现此功能。
安装 AutoHotkey 后,上述代码示例必须保存在扩展名为 AHK 的文件中(例如,Idle.ahk),并且应始终运行以启用快捷方式:
Ctrl + Shift + X 为您进行字符串操作。

Based on the answer of RedGlyph, but with some automation using AutoHotkey (AHK):

; Python IDLE shell
#IfWinActive ahk_exe pythonw.exe

^+x::  ; IDLE - multiple commands paste
SoundBeep,1700, 150
send, ^{end}
var1 = cmds = '''
var2 := clipboard
var3 = %var1%%var2%
var4 = %var3%'''
msgbox,,, %var4%,2
clipboard = %var4%
send, ^{vk0x56}   ;send, ^v
send, {enter}
sleep, 1000
send, exec(cmds)
return

You need to install AutoHotkey to make this work.
After installing AutoHotkey, the above code sample must be saved in a file with extension AHK (e.g., Idle.ahk), and should be running always to enable the shortcut:
Ctrl + Shift + X to make the string manipulation for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文