J
J
Jeronymo322020-06-01 19:58:38
Python
Jeronymo32, 2020-06-01 19:58:38

Is it possible to make buttons from the same function move separately in Tkinter Python 3.8.3?

I need buttons that are created by one function to move separately. I am a beginner programmer. The code contains an example of moving objects and the button itself, which creates new buttons. Here is the code:

import tkinter as tk
  
root = tk.Tk(className='ghtvtotybt')
root.geometry("600x600+0+0")
root.state('zoomed')
root.resizable(0,0)
  
def on_mouse_down(event):
    global dif_x, dif_y
    win_position = [int(coord) for coord in root.wm_geometry().split('+')[1:]]
    dif_x, dif_y = win_position[0] - event.x, win_position[1] - event.y
  
  
def update_position(event):
    button.place(x=(event.x_root + dif_x), y=(event.y_root + dif_y-26))
    print((event.x_root + dif_x, event.y_root + dif_y))
  
def on_mouse_down1(event):
    global dif_x, dif_y
    win_position = [int(coord) for coord in root.wm_geometry().split('+')[1:]]
    dif_x, dif_y = win_position[0] - event.x, win_position[1] - event.y
  
  
def update_position1(event):
    scrollbar.place(x=(event.x_root + dif_x), y=(event.y_root + dif_y-26))
    print((event.x_root + dif_x, event.y_root + dif_y))
xl=[]

def click():
    global l
    l=tk.Button(root)
    l.pack()
    l.bind('<ButtonPress-1>', on_mouse_down)
    l.bind('<B1-Motion>', update_position2)
    xl.append(l)
    return xl
   
def update_position2(event):
    widget=event.widget
    l.place(x=(event.x_root + dif_x), y=(event.y_root + dif_y-26))
    

    
    
buttons = tk.Button(root, command=click)
buttons.pack()


button=tk.Button(root)
scrollbar=tk.Scrollbar(root)
button.pack()
scrollbar.pack()
button.bind('<ButtonPress-1>', on_mouse_down)
button.bind('<B1-Motion>', update_position)
  
scrollbar.bind('<ButtonPress-1>', on_mouse_down1)
scrollbar.bind('<B1-Motion>', update_position1)
  
  
root.mainloop()

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question