微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。