Home > OS >  How to move image down each time button is pressed Tkinter
How to move image down each time button is pressed Tkinter

Time:10-16

I am trying to create a tkinter application where every time you press a button an image moves down a certain number of pixels. For example, the first time it is placed at y=30 and then the next time the button is pressed it is placed at y=60 etc. Is there any way to do this? I do not want to use the pack() method as I need to place the image in a specific location on the screen using x and y coordinates.

import calendar
import tkinter as tk
from tkinter import ttk

root = tk.Tk()
root.geometry('800x800')

def display():
        box_image = tk.PhotoImage(file='apple.png')
        panel2 = tk.Label(root, image=box_image, bg='#f7f6f6')
        panel2.image = box_image
        panel2.place(x=30, y=30   30) #i was thinking about doing something like adding 30 each time but this didn't work


button = tk.Button(root, text="click me", command=display)
button.place(x=0, y=0)



root.mainloop()

CodePudding user response:

do the following :

import calendar
import tkinter as tk
from tkinter import ttk

root = tk.Tk()
root.geometry('800x800')


x = 30
y = 30
box_image = tk.PhotoImage(file=r'apple.png')

def display():
    global x , y
    panel2 = tk.Label(root, image=box_image, bg='#f7f6f6')
    panel2.place(x=x 30, y=y 30) 
    x = x 30
    y = y 30


button = tk.Button(root, text="click me", command=display)
button.place(x=0, y=0)

root.mainloop()
  • Related