How to clear the screen of ALL text in IDLE shell?

百般思念 提交于 2021-01-27 18:10:51

问题


I want to create a text based GUI for a python game. I intend to move a character around between multiple lines of text. after hours of searching online, the solutions I found don't work for this. I want something that will erase all text on screen, so I can re-print the same line, but with the character moved one space. To be clear, the screen I want to erase is the one that appears when you hit "run module" in IDLE. Also, I cannot import any external libraries, because for some reason, that is broken on my computer.

I've tried ctrl+l. It did absolutely nothing. I've tried to use os.system('cls') (I'm on windows) It opens command prompt, clears it, then closes it. not what I need. print ("\n" * 50) works, but doesn't clear the screen. The screen MUST be clear, I don't want to print a million new lines.

Here is my code:

import sys
valone = 5
valtwo = 5
# create line of text
sys.stdout.write(" a " * valone + " t " + " a " * valtwo)
# move t character 1 space
valone = valone -1
valtwo = valtwo + 1
# clear screen and redraw line with t moved 1 space.

回答1:


That is not possible with IDLE's Python shell.

From the documentation:

Shell never throws away output.

and

Tab characters cause the following text to begin after the next tab stop. (They occur every 8 ‘characters’). Newline characters cause following text to appear on a new line. Other control characters are ignored or displayed as a space, box, or something else, depending on the operating system and font.

Meaning it's a very basic shell that outputs all text in a linear fashion. You need to run Python in a different shell to accomplish what you want.




回答2:


A text-based graphical user interface (GUI) should mean "based on a text widget of a GUI framework", such as the cross-platform tkinter module that comes with Python, and which is used by IDLE. The IDLE shell is not, currently, designed to facilitate what you want to do. Instead, you could write your own tkinter program using either a Label or Text widget. The following could give you a start.

import tkinter as tk
r = tk.Tk()
f = 'TkFixedFont'
s = 'X         '
sv = tk.StringVar(r)
l = tk.Label(r, font=f, textvariable=sv)
sv.set(s)
t = tk.Text(r, font=f, height=1, width=10)
t.insert('1.0', 'X')

n = 0  # index of marker
i = 200  # milliseconds between moves

def moveright():
    global n, s
    if n < 9:
        s = ' ' + s[:-1]
        sv.set(s)
        t.insert('1.0', ' ')
        n += 1
        r.after(i, moveright)
    else:
        moveleft()

def moveleft():
    global n, s
    if n:
        s = s[1:] + ' '
        sv.set(s)
        t.delete('1.0', '1.1')
        n -= 1
        r.after(i, moveleft)
    else:
        moveright()

l.pack()
t.pack()
r.after(i, moveright)
r.mainloop()



回答3:


You might be able to do this with curses. It's built-in on Python for Linux, etl. al, but for Windows, you'll need to install the windows-curses library.

Once you've done that, you can can use the .clear() method:

from curses import initscr

stdscr = initscr()
stdscr.clear()
stdscr.refresh()

Of course, this depends on solving your problem with installing external modules. Without doing that, or using something other than Windows, I'm afraid you're stuck.

Also, I'm not sure if this works when run under IDLE, but if not, then simply run your program from the cmd prompt.

Edit: I originally mentioned the UniCurses library, but it doesn't seem well supported, so I've changed this to windows-curses, which seems to be a patch of the built-in curses made to work on Windows.



来源:https://stackoverflow.com/questions/56819599/how-to-clear-the-screen-of-all-text-in-idle-shell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!