J
J
Jeronymo322020-06-18 23:09:18
Python
Jeronymo32, 2020-06-18 23:09:18

How to make the buttons created by the function perform one function, but each separately?

I need the buttons created by the functions to move, but it turns out that the new one moves instead of the old one, it is no longer used. Here is the code:

import tkinter as tk 
root = tk.Tk()
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))
B1=tk.Button(root)
button_identities = [B1]
bn=[]
def search():
    global bname, bnr
    
    if button_identities.index(xl):
        
        p=button_identities.index(xl)
        bname = (button_identities[p])
        print(p)
        bname.bind('<ButtonPress-1>', on_mouse_down)
        bname.bind('<B1-Motion>', update_position2)
        
    
    else:
        p=0
    
    bn.append(p)
    print(bn)
def click():
    global xl, bname
    
    xl=tk.Button(root, command=search)
    xl.pack()
    
    button_identities.append(xl)
    
    print(button_identities)

       
        
def update_position2(event):
    bname.place(x=(event.x_root + dif_x), y=(event.y_root + dif_y-26))
    bnr.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