
<B1-Motion> Mouse Left button Press and movemy_w.bind('<B1-Motion>',my_callback)Full code is here. import tkinter as tk my_w = tk.Tk()my_w.geometry("615x400") # width and height def my_callback(event): l1.config(text='Position x : '+ str(event.x) +", y : "+ str(event.y))l1=tk.Label(my_w,text='to Display',bg='yellow',font=30)l1.pack(padx=10,pady=10)my_w.bind('<B1-Motion>',my_callback) # Mouse left button pressed movemy_w.mainloop()f_img = tk.PhotoImage(file = "D:\\top2.png") # path of the imagemy_c = tk.Canvas(my_w,width=600,height=400) # canvas size my_c.pack() # place on pack my_img = my_c.create_image(180, 50, image=f_img) # add image to canvasimport tkinter as tk
my_w = tk.Tk()
my_w.geometry("615x400") # width and height def my_callback(event):
global f_img
l1.config(text='Position x : '+ str(event.x) +", y : "+ str(event.y))
f_img = tk.PhotoImage(file = "D:\\top2.png")
my_img = my_c.create_image(event.x, event.y, image=f_img)
l1=tk.Label(my_w,text='to Display',bg='yellow',font=30)l1.pack(padx=10,pady=5)
f_img = tk.PhotoImage(file = "D:\\top2.png") # path of the image
my_c = tk.Canvas(my_w,width=600,height=400) # canvas size
my_c.pack() # place on pack
my_img = my_c.create_image(180, 50, image=f_img) # add image to canvas
my_w.bind('<B1-Motion>',my_callback) # Mouse left button pressed movemy_w.mainloop()
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("615x400") # width and height
def my_callback(event):
l1.config(text='Position x : '+ str(event.x) +", y : "+ str(event.y))
bt1.pack_forget() # remove the button
bt1.place(x=event.x,y=event.y) # create at new coordinate
l1=tk.Label(my_w,text='to Display',bg='yellow',font=30)
l1.place(x=0,y=0)
bt1=tk.Button(my_w,text='Button',bg='lightgreen',font=12)
bt1.place(x=150,y=180,anchor='se')
my_w.bind('<B1-Motion>',my_callback) # Mouse left button pressed move
my_w.mainloop() ⚡ Master Tkinter Drag and DropAuthor & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.