1

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!

Thalis
  • 188
  • 2
  • 12
  • 1
    for this question , here is a good solution from TheLizzard https://stackoverflow.com/questions/66968676/how-to-get-a-better-window-postion-if-i-click-on-an-overrideredirectted-window – bangKok May 21 '21 at 12:36
  • 1
    if you want to move the title bar include with the label, `bind` the label – bangKok May 21 '21 at 12:45
  • I don't know what the link you provided has to do with my question, but I anyways solved my problem with binding – Thalis May 21 '21 at 13:00
  • @Thalis If you are trying to create your own title bar, look at [this](https://stackoverflow.com/a/66194808/11106801). It is quite a lot of code, but you can remove all of the unnecessary parts if you want to. – TheLizzard May 21 '21 at 13:15
  • @TheLizzard I've made one which is looking pretty good, thanks for your suggestion – Thalis May 21 '21 at 13:17
  • @Thalis If you have suggestion how to improve mine, I will be happy to implement them. It should work on both Linux and Windows (maybe MacOS - I have no way of testing it). – TheLizzard May 21 '21 at 13:20
  • I was unsured what exactly you mean. But you can see in this example how you can use it. If you want to move your statusbar (if you make one) you need to bind it aswell. If you want a sizegrip make `winfo_pointerx/y() - winfo_x/y()` and `bind` a widget. So always you need to bind the widgets – bangKok May 21 '21 at 13:20
  • I've solved my problem completely, thanks again! – Thalis May 21 '21 at 13:23

1 Answers1

2

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))
Thalis
  • 188
  • 2
  • 12
  • 1
    You know that instead of `.bind(..., lambda events: start_move(events))` you can just use `.bind(..., start_move)`. Same goes for the other bindings. – TheLizzard May 21 '21 at 12:54