_tkinter.TclError: неверное имя команды .14574424

Я получаю следующую ошибку:

_tkinter.TclError: неверное имя команды «.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

person hss    schedule 03.05.2017    source источник
comment
ошибка: Traceback (последний последний вызов): Файл D:/sem4/t/lesson4/task1/task.py, строка 29, в ‹module› AnimationDemo() # Создать файл GUI D:/sem4/t/lesson4/task1 /task.py, строка 17, в init canvas.move(text, dx, 0) # Переместить текстовый блок dx Файл C:\Users\sreeyu\AppData\Local\Programs\Python\Python35\ lib\tkinter_init_.py, строка 2431, in move self.tk.call((self._w, 'move') + args) _tkinter.TclError: неверное имя команды .14440096   -  person hss    schedule 03.05.2017
comment
что вы делаете, что вызывает ошибку? Кстати, это неправильный способ делать анимацию с помощью Tkinter. См. stackoverflow.com/a/11505034/7432.   -  person Bryan Oakley    schedule 03.05.2017
comment
Спасибо @BryanOakley, но могу ли я узнать, почему он показывал ошибку, если мы использовали цикл while   -  person hss    schedule 05.05.2017


Ответы (1)


Как упоминал Брайан, вместо использования цикла 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()
person Josselin    schedule 04.05.2017