@Jeronymo32

Можно сделать так, чтобы кнопки из одной функции перемещались отдельно в Tkinter Python 3.8.3?

Мне нужно, чтобы кнопки, которые создаются одной функцией перемещались отдельно. Я начинающий программист. В коде есть пример перемещения объектов и сама кнопка, которая создает новые кнопки. Вот код:
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()
  • Вопрос задан
  • 216 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы