Tkinter: Mouse drag a window without borders, eg. overridedirect(1)

后端 未结 5 1523
说谎
说谎 2020-11-27 20:18

Any suggestions on how one might create event bindings that would allow a user to mouse drag a window without borders, eg. a window created with overridedirect(1)

5条回答
  •  北海茫月
    2020-11-27 21:18

    Here is my solution:

    from tkinter import *
    from webbrowser import *
    
    
    lastClickX = 0
    lastClickY = 0
    
    
    def SaveLastClickPos(event):
        global lastClickX, lastClickY
        lastClickX = event.x
        lastClickY = event.y
    
    
    def Dragging(event):
        x, y = event.x - lastClickX + window.winfo_x(), event.y - lastClickY + window.winfo_y()
        window.geometry("+%s+%s" % (x , y))
    
    
    window = Tk()
    window.overrideredirect(True)
    window.attributes('-topmost', True)
    window.geometry("400x400+500+300")
    window.bind('', SaveLastClickPos)
    window.bind('', Dragging)
    window.mainloop()
    

提交回复
热议问题