Can anyone help me figure out how to move the window and it not snapping to the corner while using a custom title bar...
from tkinter import *
root = Tk()
root.geometry('571x819')
root.overrideredirect(1)
root.wm_attributes("-transparentcolor", "grey")
def move_app(e):
root.geometry(f' {e.x_root} {e.y_root}')
frame_photo = PhotoImage(file = 'F:\\gui\\Frame 1.png')
frame_label = Label(root, borderwidth = 0, bg = 'grey', image = frame_photo)
frame_label.pack(fill=BOTH, expand = True)
title_bar = Frame(root, bg='#FFF6C9', relief='raised', bd=0,highlightthickness=0)
title_bar_title = Label(title_bar, bg='#FFF6C9',bd=0,fg='white',font=("helvetica",
10),highlightthickness=0)
frame_label.bind("<B1-Motion>", move_app)
root.mainloop()
CodePudding user response:
You can save the offset from the top-left corner of window when start moving by binding the <Button-1>
event, then calculate the position the top-left corner of the window based on this offset inside move_app()
:
def start_drag(e):
# save the offset from the top-left corner of window
e.widget.offset = (e.x, e.y)
def move_app(e):
# calculate the top-left corner of window based on the saved offset
root.geometry(f' {e.x_root-e.widget.offset[0]} {e.y_root-e.widget.offset[1]}')
...
frame_label.bind("<Button-1>", start_drag)
frame_label.bind("<B1-Motion>", move_app)
...