Why can't I drag a custom made title bar from a place where there is a label? And how to do it?

64 Views Asked by At

I've created an overrideredirected root window in tkinter but there seems to be a problem when I'm trying to drag the title_bar (that i created) from a specific place where I've put a label

from tkinter import Tk, Label, Button, Frame

basically these functions are for the window movement, nothing else

x_cor, y_cor = 0, 0


def start_move(event):
    global x_cor, y_cor
    x_cor = event.x
    y_cor = event.y


def stop_move(_):
    global x_cor, y_cor
    x_cor = None
    y_cor = None


def do_move(event):
    global x_cor, y_cor
    deltax = event.x - x_cor
    deltay = event.y - y_cor
    x = root.winfo_x() + deltax
    y = root.winfo_y() + deltay
    root.geometry(f"+{x}+{y}")

And here is the main program

root = Tk()
root.overrideredirect(1)
root.geometry('500x500')
title = Frame(root, bg='pink')
title.pack(fill='x')

label = Label(title, text='Title of my program',bg='black', fg='white', anchor='c')
label.place(x=200, y=0)

close = Button(title, text='X', fg='white', bg='red', command=root.destroy)
close.pack(side='right')

title.bind("<ButtonPress-1>", lambda events: start_move(events))
title.bind("<ButtonRelease-1>", lambda events: stop_move(events))
title.bind("<B1-Motion>", lambda events: do_move(events))

root.mainloop()

Title bar moves just fine when I'm holding the mouse button in any other place except the space where label is located. Is there a way to fix this?

In other words can I somehow 'overcome' the label and drag the title bar from wherever I want?

Thanks in advance!

1

There are 1 best solutions below

1
On

Solution is to bind whatever widget there is inside the title_bar with the move functions. In this case, like this:

label.bind("<ButtonPress-1>", lambda events: start_move(events))
label.bind("<ButtonRelease-1>", lambda events: stop_move(events))
label.bind("<B1-Motion>", lambda events: do_move(events))