2013-03-23 55 views
2

我有一個繪製koch雪花的python腳本。一切工作正常,除非如果我在繪製完成前關閉tutle圖形窗口,我得到一個很長的錯誤,作爲最後一行:tkinter.tclerror:無效的命令名稱「.38006576」。 我認爲這就像在java.swing:exit_on_close。這不是默認設置。不過,我還沒有發現這樣的蟒龜。 任何建議如何避免這種情況?當它完成它的繪製之前關閉python龜時獲取tkinter.tclerror

  import turtle 
      import sys 
      import easygui as eg 

      def isNum(s): 
       try: 
        int(s) 
        return True 
       except ValueError: 
        return False 

      run = True 
      while run: 

       msg   = "Input" 
       title  = "Koch snowflake" 
       fieldNames = ["Length","Depth"] 
       fieldValues = [] # we start with blanks for the values 
       fieldValues = eg.multenterbox(msg,title, fieldNames) 

       a=0 
       iterations=0 
      # make sure that none of the fields was left blank 
       while 1: # do forever, until we find acceptable values and break out 
        if fieldValues == None: 
         break 
        errmsg = "" 

        # look for errors in the returned values 
        for i in range(len(fieldNames)): 
         if fieldValues[i].strip() == "": 
          errmsg = errmsg + ('"%s" must be filled.\n\n' % fieldNames[i]) 
         elif not (isNum(fieldValues[i].strip())): 
          errmsg = errmsg + ('"%s" requires a number.\n\n' % fieldNames[i]) 

        if (isNum(fieldValues[1])): 
         if float(fieldValues[1].strip()) > 13: 
          errmsg = errmsg + ('Depth cannot be more than 13!') 

        if errmsg == "": 
         break # no problems found 
        else: 
         # show the box again, with the errmsg as the message  
         fieldValues = eg.multenterbox(errmsg, title, fieldNames, fieldValues) 

       #a=float(input("Length: ")) 
       if not fieldValues == None: 
        if isNum(fieldValues[0]) and isNum(fieldValues[1]): 
         a=float(fieldValues[0]) 
         iterations=int(fieldValues[1]) 
       else: 
        sys.exit(0) 

      #turtle settings 
       turtle.bgcolor("black") 
       turtle.reset() 
       turtle.up() 
       turtle.setpos(-a*1.5 ,a) 
       turtle.down() 
       turtle.delay(0) 
       turtle.speed(0) 
       turtle.pencolor("white") 

       koch_flake = "EJEJE" 

       for i in range(iterations): 
        koch_flake = koch_flake.replace("E","EBEJEBE\n") 

       turtle.down() 

       for move in koch_flake: 
        if move == "E": 
         turtle.forward(a/(3 ** (iterations - 1))) 
        elif move == "B": 
         turtle.left(60) 
        elif move == "J": 
         turtle.right(120) 
       turtle.hideturtle() 
       image = "KochSnowflake.gif" 
       msg = "Run it again?" 
       title = "Please Confirm" 
       if eg.ynbox(msg, title,('Yes', 'No'),image):  # show a Continue/Cancel dialog 
        run = True 
       else: # user chose Cancel 
        sys.exit(0) 

回答

1

將龜塗在Canvas對象上,您可以從中訪問Tkinter根。 捕獲根的WM_DELETE_WINDOW事件。當事件被捕獲時,del烏龜對象,以便它不能繼續試圖繪製。控制烏龜事件的線程將拋出一個NameError,因爲烏龜已被刪除。捕獲NameError異常並忽略它。

這是要做到這一點的代碼。我測試了它;它的工作原理&不會引發任何錯誤:

import turtle 
import Tkinter 
import sys 
import easygui as eg 

def isNum(s): 
    try: 
     int(s) 
     return True 
    except ValueError: 
     return False 

run = True 
while run: 

    msg   = "Input" 
    title  = "Koch snowflake" 
    fieldNames = ["Length","Depth"] 
    fieldValues = [] # we start with blanks for the values 
    fieldValues = eg.multenterbox(msg,title, fieldNames) 

    a=0 
    iterations=0 
# make sure that none of the fields was left blank 
    while 1: # do forever, until we find acceptable values and break out 
     if fieldValues == None: 
      break 
     errmsg = "" 

     # look for errors in the returned values 
     for i in range(len(fieldNames)): 
      if fieldValues[i].strip() == "": 
       errmsg = errmsg + ('"%s" must be filled.\n\n' % fieldNames[i]) 
      elif not (isNum(fieldValues[i].strip())): 
       errmsg = errmsg + ('"%s" requires a number.\n\n' % fieldNames[i]) 

     if (isNum(fieldValues[1])): 
      if float(fieldValues[1].strip()) > 13: 
       errmsg = errmsg + ('Depth cannot be more than 13!') 

     if errmsg == "": 
      break # no problems found 
     else: 
      # show the box again, with the errmsg as the message  
      fieldValues = eg.multenterbox(errmsg, title, fieldNames, fieldValues) 

    #a=float(input("Length: ")) 
    if not fieldValues == None: 
     if isNum(fieldValues[0]) and isNum(fieldValues[1]): 
      a=float(fieldValues[0]) 
      iterations=int(fieldValues[1]) 
    else: 
     sys.exit(0) 

    root = Tkinter.Tk() 
    cv = turtle.Canvas(root, width=200, height=200) 
    cv.pack() 
    screen = turtle.TurtleScreen(cv) 
    screen.bgcolor("black") 
    myturtle = turtle.RawTurtle(screen) 

    def lastAction(): 
     global myturtle 
     del myturtle 
     root.destroy() 
    root.protocol('WM_DELETE_WINDOW', lastAction) 

    try: 
#turtle settings 
     screen.bgcolor("black") 
     myturtle.reset() 
     myturtle.up() 
     myturtle.setpos(-a*1.5 ,a) 
     myturtle.down() 
     turtle.delay(0) 
     myturtle.speed(0) 
     myturtle.pencolor("white") 

     koch_flake = "EJEJE" 

     for i in range(iterations): 
      koch_flake = koch_flake.replace("E","EBEJEBE\n") 

     myturtle.down() 

     for move in koch_flake: 
      if move == "E": 
       myturtle.forward(a/(3 ** (iterations - 1))) 
      elif move == "B": 
       myturtle.left(60) 
      elif move == "J": 
       myturtle.right(120) 
     myturtle.hideturtle() 
    except NameError: 
     pass 

    image = "KochSnowflake.gif" 
    msg = "Run it again?" 
    title = "Please Confirm" 
    if eg.ynbox(msg, title,('Yes', 'No'),image):  # show a Continue/Cancel dialog 
     run = True 
    else: # user chose Cancel 
     sys.exit(0) 
相關問題