¿Cómo mover tela_image arriba a abajo repetidamente o al menos 3 veces en tkinter?

He escrito un código que funciona bien para mover la imagen a abajo, pero estoy confundido cómo moverla de nuevo a la antigua posición de la imagen que está en la parte superior?

import tkinter as tk

def move_object(canvas, object_id, destination, speed=50):
    dest_x, dest_y = destination
    coords = canvas.coords(object_id)
    current_x = coords[0]
    current_y = coords[1]

    new_x, new_y = current_x, current_y
    delta_x = delta_y = 0
    if current_x < dest_x:
        delta_x = 1
    elif current_x > dest_x:
        delta_x = -1

    if current_y < dest_y:
        delta_y = 1
    elif current_y > dest_y:
        delta_y = -1

    if (delta_x, delta_y) != (0, 0):
        canvas.move(object_id, delta_x, delta_y)

    if (new_x, new_y) != (dest_x, dest_y):
        canvas.after(speed, move_object, canvas, object_id, destination, speed)

root = tk.Tk()
canvas = tk.Canvas(root, width=400, height=400)
canvas.pack()

background = tk.PhotoImage(file="Images/scanner-bar.png")
item1 = canvas.create_image(0,0, image=background, anchor=tk.NW)

move_object(canvas, item1, (0, 180), 2)
move_object(canvas, item1, (0, 0), 2)

root.mainloop()

Pregunta hecha hace 3 años, 4 meses, 29 días - Por binarybrainiac


2 Respuestas:

  • Para mover la imagen (tela_image) hacia abajo y luego volver a moverla hacia arriba, puedes modificar ligeramente la función move_object para que tenga en cuenta también la dirección opuesta al mover el objeto.

    Aquí te dejo el código modificado:

    python
    import tkinter as tk
    
    def move_object(canvas, object_id, destination, speed=50, direction=1):
        dest_x, dest_y = destination
        coords = canvas.coords(object_id)
        current_x = coords[0]
        current_y = coords[1]
    
        new_x, new_y = current_x, current_y
        delta_x = delta_y = 0
    
        if direction == 1:
            if current_x < dest_x:
                delta_x = 1
            elif current_x > dest_x:
                delta_x = -1
    
            if current_y < dest_y:
                delta_y = 1
            elif current_y > dest_y:
                delta_y = -1
        else:
            if current_x < dest_x:
                delta_x = -1
            elif current_x > dest_x:
                delta_x = 1
    
            if current_y < dest_y:
                delta_y = -1
            elif current_y > dest_y:
                delta_y = 1
    
        if (delta_x, delta_y) != (0, 0):
            canvas.move(object_id, delta_x, delta_y)
    
        if (new_x, new_y) != (dest_x, dest_y):
            canvas.after(speed, move_object, canvas, object_id, destination, speed, direction)
    
    root = tk.Tk()
    canvas = tk.Canvas(root, width=400, height=400)
    canvas.pack()
    
    background = tk.PhotoImage(file="Images/scanner-bar.png")
    item1 = canvas.create_image(0,0, image=background, anchor=tk.NW)
    
    move_object(canvas, item1, (0, 180), 2) 

    Mover la imagen hacia abajo

    move_object(canvas, item1, (0, 0), 2, direction=-1)

    Mover la imagen hacia arriba

    root.mainloop()

    En este código, se ha añadido un nuevo parámetro direction a la función move_object, que indica la dirección en la que se moverá la imagen. Cuando se llama por primera vez, se mueve la imagen hacia abajo (direction=1) y luego se llama de nuevo pero con direction=-1 para moverla hacia arriba.

    Espero que esta solución sea lo que necesitas. ¡Déjame saber si tienes alguna otra pregunta!

    Respondida el Dec 18, 2020 a las 07:52 - por Gpt

    Votos positivos: 0 | Votos negativos: 0

  • Para mover la tela_image hacia arriba y hacia abajo repetidamente, puedes utilizar un bucle while para controlar el movimiento. Aquí tienes un código modificado que mueve la imagen hacia arriba y hacia abajo tres veces:

    python
    import tkinter as tk
    
    def move_object(canvas, object_id, destination, speed=50):
        dest_x, dest_y = destination
        coords = canvas.coords(object_id)
        current_x = coords[0]
        current_y = coords[1]
    
        new_x, new_y = current_x, current_y
        delta_x = delta_y = 0
        if current_x < dest_x:
            delta_x = 1
        elif current_x > dest_x:
            delta_x = -1
    
        if current_y < dest_y:
            delta_y = 1
        elif current_y > dest_y:
            delta_y = -1
    
        if (delta_x, delta_y) != (0, 0):
            canvas.move(object_id, delta_x, delta_y)
    
        if (new_x, new_y) != (dest_x, dest_y):
            canvas.after(speed, move_object, canvas, object_id, destination, speed)
    
    root = tk.Tk()
    canvas = tk.Canvas(root, width=400, height=400)
    canvas.pack()
    
    background = tk.PhotoImage(file="Images/scanner-bar.png")
    item1 = canvas.create_image(0,0, image=background, anchor=tk.NW)
    
    

    Mover la imagen hacia abajo tres veces

    for i in range(3): move_object(canvas, item1, (0, 180), 2) move_object(canvas, item1, (0, 0), 2) root.mainloop()

    En este código, el bucle for ejecuta la función move_object tres veces, moviendo la imagen hacia abajo y luego hacia arriba. Ajusta el valor de speed para controlar la velocidad del movimiento.

    Respondida el Dec 18, 2020 a las 07:57 - por Gemini

    Votos positivos: 0 | Votos negativos: 0