Python-如何在切换开关时清除屏幕



现在,当我使用tab键切换新算法时,我正试图使海龟图形屏幕清晰并重置所有内容。切换部分可以工作,但是当我切换到新算法时,我无法清除整个屏幕。

我的代码如下所示:

from pynput import keyboard
from pynput.keyboard import Listener, Key, Controller
import keyboard
from turtle import Turtle, Screen, clearscreen, clear
import sys

def lefthand_Algo():
execfile('...')
def bfs_Algo():
execfile('...')
# Creates the title
def title():                                              
t = Turtle()
t.color('white')
t.write('Hello, hit tab to start the algorithm!', font=('lemon',20,'normal'), align='center')
t.hideturtle()
screen = Screen()
clearscreen = clearscreen
clear = clear
screen.bgcolor("black")                                        # Set the background colour
screen.setup(width=0.9, height=0.9)                            # Setup the dimensions of the working window  
title = title()
current_state = bfs_Algo
next_state = lefthand_Algo
switch = False
def toggle():
global switch
switch = not switch
if switch:
next_state()
else:
current_state()
screen.onkeypress(toggle, "Tab")
screen.listen()
screen.mainloop()

注意:算法在单独的文件中,此文件仅用于在两个文件之间切换。

如何在每次切换时清除屏幕?我们将非常感谢您的帮助!:(

来自@RhinoRunner的评论:

使用turtle.reset()

最新更新