2017-05-03 2 views
0

내가 무엇입니까 다음과 같은 오류 :_tkinter.TclError : 잘못된 명령 이름은 "0.14574424"

_tkinter.TclError: invalid command name “.14574424”

나는 오류를 이해할 수 없습니다입니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?

# Import tkinter 
from tkinter import * 

class AnimationDemo: 
    def __init__(self): 
     window = Tk()  # Create a window 
     window.title("Animation Demo")  # Set a title 

     width = 250   # Width of the canvas 
     canvas = Canvas(window, bg = "white", width = 250, height = 50) 
     canvas.pack() 

     x = 0   # Starting x position 
     canvas.create_text(x, 30, text = "Message moving?", tags = "text") 

     dx = 3 
     while True: 
      canvas.move("text", dx, 0)  # Move text dx unit 
      canvas.after(100)   # Sleep for 100 milliseconds 
      canvas.update()   # Update canvas 
      if x < width: 
       x += dx   # Get the current position for string 
      else: 
       x = 0  # Reset string position to the beginning 
       canvas.delete("text") 
       # Redraw text at the beginning 
       canvas.create_text(x, 30, text = "Message moving?", tags = "text") 
     window.mainloop()  # Create an event loop 

AnimationDemo()  # Create GUI 
+0

오류 : 역 추적 (가장 최근 통화 최종) : 파일 "D를 : /sem4/t/lesson4/task1/task.py ", 줄 29, AnimationDemo() # GUI 작성 파일"D : /sem4/t/lesson4/task1/task.py "17 번째 줄 __init__ canvas.move ("text", dx, 0) # 텍스트 DX 단위 이동 F ile "C : \ Users \ sreeyu \ AppData \ Local \ Programs \ Python \ Python35 \ lib \ tkinter \ __init__.py"(이동 2431) self.tk.call ((self._w, 'move') + args) _tkinter.TclError : 잘못된 명령 이름 ".14440096" – hss

+0

그 원인은 무엇입니까? 그런데 Tkinter로 애니메이션을 만드는 것은 잘못된 방법입니다. http://stackoverflow.com/a/11505034/7432 –

+0

을 참조하십시오 감사합니다 @ BryanOakley하지만 내가 pls 우리가 루프를 사용하는 경우 오류를 보여주는 이유를 알 수 있습니다 – hss

답변

0

대신 while 루프를 사용하는 브라이언에 의해 언급 한 바와 같이, .after() 방법과 직접 텍스트 애니메이션을보십시오

import tkinter as tk 

class AnimationDemo: 
    def __init__(self): 
     self.window = tk.Tk() 
     self.window.title("Animation Demo") 

     # Create a canvas 
     self.width = 250 
     self.canvas = tk.Canvas(self.window, bg="white", width=self.width, 
          height=50) 
     self.canvas.pack() 

     # Create a text on the canvas 
     self.x = 0 
     self.canvas.create_text(self.x, 30, text = "Message moving?", tags="text") 
     self.dx = 3 

     # Start animation & launch GUI 
     self.animate_text() 
     self.window.mainloop() 

    def animate_text(self): 
     # Move text dx unit 
     self.canvas.move("text", self.dx, 0) 
     if self.x < self.width: 
      # Get the current position for string 
      self.x += self.dx 
     else: 
      # Reset string position to the beginning 
      self.x = 0 
      self.canvas.delete("text") 
      self.canvas.create_text(self.x, 30, text = "Message moving?", tags="text") 
     self.window.after(100, self.animate_text) 

# Create GUI 
AnimationDemo() 
+1

감사합니다 @ Josselin – hss