how can tkinter bind the mouse when it's on the edge of the window use tkinter

Viewed 15

platfrom: windows python3.7

i used self.overrideredirect(True), to hide the title bar but i aslo can't zoom the window at the same time. i only want to hide the title bar.

At first i tried to detect when the mouse is hover on the edge of the window and then use self.geometry() to change the size of the window.

but i failed when i began to write to code, i don't know how to detect when the mouse is hover on the edge of the window.

Does Tkinter expose enough functionality to allow me to implement the task at hand? Or are there easier/higher-level ways to achieve what I want to do?

1 Answers

Check out the code of class Example for a way of making a window without menu bar movable by using the right mouse button and re-sizable by grabbing a grip placed in the right bottom corner:

import tkinter as tk
import tkinter.ttk as ttk

class Example(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.floater = FloatingWindow(self)

class FloatingWindow(tk.Toplevel):
    def __init__(self, *args, **kwargs):
        tk.Toplevel.__init__(self, *args, **kwargs)
        self.overrideredirect(True)
        self.wm_geometry("400x400+100+100")

        self.label = tk.Label(self, text="Grab the lower-right corner to resize\nMove window with right mouse button")
        self.label.pack(side="top", fill="both", expand=True)

        self.grip1 = ttk.Sizegrip(self)
        self.grip1.place(relx=1.0, rely=1.0, anchor="se")
        self.grip1.lift(self.label)

        self.bind("<B3-Motion>", self.OnMotion)

    def OnMotion(self, event):
        x1 = self.winfo_pointerx()
        y1 = self.winfo_pointery()
        #x0 = self.winfo_rootx()
        #y0 = self.winfo_rooty()
        #w  = self.winfo_width()
        #h  = self.winfo_height()
        self.wm_geometry("+%s+%s" % (x1,y1))
        return

app=Example()
app.mainloop()
Related