【问题标题】:Error while calling turtle window again after closing it through a button in tkinter通过 tkinter 中的按钮关闭乌龟窗口后再次调用乌龟窗口时出错
【发布时间】:2021-06-10 19:03:20
【问题描述】:

以下代码是 tkinter 窗口,其中包含一个调用海龟窗口的按钮

import tkinter
from tkinter import *
from turtlefunc import *
root = Tk()

#creating a button
myButton = Button(root, text="call turtle",command =call)
myButton.pack()

root.mainloop()

以下是海龟窗口的代码

import turtle
def call():
    t = turtle.Turtle()
    screen = turtle.Screen()
    screen.bgcolor('white')
    t.width(3)
    for i in range(1,5):
        t.forward(20)
        t.left(90)
    t.hideturtle()
    turtle.exitonclick()

按钮第一次运行正常,但第二次调用海龟窗口或函数时返回错误

错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\sharmila\AppData\Local\Programs\Python\Python38\lib\tkinter\__init__.py", line 1883, in __call__
    return self.func(*args)
  File "C:\Users\sharmila\PycharmProjects\GAME ARCADE GUI\turtlefunc.py", line 3, in call
    t = turtle.Turtle()
  File "C:\Users\sharmila\AppData\Local\Programs\Python\Python38\lib\turtle.py", line 3813, in __init__
    RawTurtle.__init__(self, Turtle._screen,
  File "C:\Users\sharmila\AppData\Local\Programs\Python\Python38\lib\turtle.py", line 2557, in __init__
    self._update()
  File "C:\Users\sharmila\AppData\Local\Programs\Python\Python38\lib\turtle.py", line 2660, in _update
    self._update_data()
  File "C:\Users\sharmila\AppData\Local\Programs\Python\Python38\lib\turtle.py", line 2646, in _update_data
    self.screen._incrementudc()
  File "C:\Users\sharmila\AppData\Local\Programs\Python\Python38\lib\turtle.py", line 1292, in _incrementudc
    raise Terminator
turtle.Terminator

【问题讨论】:

  • 尝试销毁 Turtle 实例,但我认为之前有人问过这个问题
  • @Matiiss 如果您也可以帮助我,那将非常有帮助。我做不到

标签: tkinter python-turtle


【解决方案1】:

你必须.clear()你的画布在你销毁它之前,所以所有的海龟都不在等式中。这样,当它不再看到 TurtleScreen._RUNNING 时,它不会引发 Terminator

#! /usr/bin/python3

import tkinter as tk
import turtle as trt

title = 'Python Turtle'
root  = tk .Tk() ; root .title( title )

Width, Height  = 500, 500
myButton = None  ##  empty, forward declaration

def call():
    myButton .pack_forget()  ##  hide button
    canvas  = tk .Canvas( root, width=Width, height=Height )
    canvas .pack()  ##  show canvas
    turtle  = trt .RawTurtle( canvas=canvas )
    screen  = turtle .getscreen()
    screen .bgcolor('white')
    turtle .width( 3 )
    for i in range( 1, 5 ):
        turtle .forward( 20 )
        turtle .left( 90 )
    turtle .hideturtle()
    def dest( x, y ):
        screen .clear()  ##  destroy turtles
        canvas .destroy()
        myButton .pack()  ##  show button again
    screen .onclick( dest )

##  creating a button
myButton = tk .Button( root,  text='call turtle',  width=len(title) +10,  command=call )
myButton .pack()

root .mainloop()

不过,您实际上并不需要每次都“销毁”画布并重新构建它。您可以在不使用时“隐藏”它。

#! /usr/bin/python3

import tkinter as tk
import turtle as trt

title = 'Python Turtle'
root  = tk .Tk() ; root .title( title )

Width, Height  = 500, 500
canvas  = tk .Canvas( root, width=Width, height=Height )
myButton = None  ##  empty, forward declaration

def call():
    myButton .pack_forget()  ##  hide button
    canvas .pack()  ##  show canvas
    turtle  = trt .RawTurtle( canvas=canvas )
    screen  = turtle .getscreen()
    screen .bgcolor('white')
    turtle .width( 3 )
    for i in range( 1, 5 ):
        turtle .forward( 20 )
        turtle .left( 90 )
    turtle .hideturtle()
    def dest( x, y ):
        screen .clear()  ##  destroy turtles
        canvas .pack_forget()  ##  hide canvas, so you can reuse it later
        myButton .pack()  ##  show button again
    screen .onclick( dest )

##  creating a button
myButton = tk .Button( root,  text='call turtle',  width=len(title) +10,  command=call )
myButton .pack()

root .mainloop()

【讨论】:

    【解决方案2】:

    该模块使用一个类变量 _RUNNING,当像这样运行时,它在执行之间保持为真。我已请求通过拉取请求更新模块。

    同时,解决/工作示例

    1)

    import importlib
    import turtle
    def call():
        importlib.reload(turtle)
        screen = turtle.Screen()
        screen.bgcolor('white')
        t = turtle.Turtle()
        t.width(3)
        for i in range(1,5):
            t.forward(20)
            t.left(90)
        t.hideturtle()
        turtle.exitonclick()
    
    
    
    
    import turtle
    def call():
        screen = turtle.Screen()
        turtle.TurtleScreen._RUNNING=True
        screen.bgcolor('white')
        t = turtle.Turtle()
        t.width(3)
        for i in range(1,5):
            t.forward(20)
            t.left(90)
        t.hideturtle()
        turtle.exitonclick()
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-12
      • 1970-01-01
      相关资源
      最近更新 更多