我知道这是一个奇怪而愚蠢的问题,但我很好奇,因为我对 python 及其工作原理不太了解。
从终端或使用 IDLE 时,有没有办法在特定屏幕位置打印字符串?
我会尝试更好地解释这一点:您还记得以前用 Basic 编写小程序的日子吗?也许是在 Commodore 64、Apple II 或 ZX Spectrum 上?
在那些日子里,如果你想在某个位置打印一个字符串,你通常会写这样的东西:
10 LOCATE 30, 40 : PRINT "hello world"
我只是想知道是否有任何方法告诉 python 打印某个位置的字符串,以及是否有办法知道 IDLE 窗口中实际可以显示多少列和多少行。
我还制作了一个模型图来解释这个概念。
It's a strange and silly question I know, but I'm curious because I don't know that much about python and how it works.
From the terminal or when you use IDLE, is there any way to print a string at a certain screen position?
I'll try to explain this better: Do you remember the old days when you used to make small programs in Basic, maybe on a Commodore 64, Apple II or ZX Spectrum?
During that days if you wanted to print a string at a certain position you used to write something like this:
10 LOCATE 30, 40 : PRINT "hello world"
I'm just curious to know if there's any way to tell python to print a string at a certain position, and if there's a way to know how many columns and how many rows can be actually displayed inside the IDLE window.
I've also made a mockup draw, to explain this concept.
发布评论
评论(4)
我不知道这是否适用于 IDLE,但它适用于任何普通终端:
这使用 Ansi-转义序列
I don't know if this works on IDLE, but it does in any normal terminal:
This uses Ansi-Escape Sequences
为了避免 @user3431399 提出的问题,您首先需要 制作 win32控制台识别 ANSI/VT100 转义序列。我在 Windows 10 终端上遇到了与 @user3431399 相同的问题,我按照 href="https://stackoverflow.com/users/980442/daniel-de-le%c3%b3n">@Daniel De Léon。也就是说,我在windows提示符下(cmd命令)以管理员身份登录。然后我复制、粘贴并运行该命令。
To avoid the issue raised by @user3431399, you first need to make win32 console recognize ANSI/VT100 escape sequences. I got the same problem as @user3431399 on my Windows 10 terminal and I solved it by following the solution recommended by @Daniel De Léon. That is, I logged in as administrator at the windows prompt (cmd command). Then I copied, pasted, and ran the command.
使用光标位置
ANSI 转义码。
或者
Use the cursor position
ANSI escape code.
or
使用 Tkinter 通过在框架下选择并提供行号和列号来显示您的输出来使其完美
Use Tkinter to make it perfect by select under frame and provide row and column number to display your output