Drag window handle for Tkinter?
NickName:Martti Laine Ask DateTime:2011-09-17T22:46:17

Drag window handle for Tkinter?

First of all, this is my current code, essential parts of it:

class WindowDraggable():        
    x = 1
    y = 1
    def __init__(self,label):
        label.bind('<ButtonPress-1>',self.StartMove);
        label.bind('<ButtonRelease-1>',self.StopMove);
        label.bind('<B1-Motion>',self.OnMotion);

    def StartMove(self,event):
        self.x = event.x
        self.y = event.y

    def StopMove(self,event):
        self.x = None
        self.y = None

    def OnMotion(self,event):
        deltaX = event.x - self.x
        deltaY = event.y - self.y
        self.x = root.winfo_x() + deltaX
        self.y = root.winfo_y() + deltaY
        root.geometry("+%sx+%s" % (self.x,self.y))

#root is my window:
root = Tk()

#This is how I assign the class to label
WindowDraggable(label)

#All imports
from Tkinter import *
from PIL import Image, ImageTk
import sys
import re

What I am trying to accomplish is; Make the window draggable by a handle, in this case label. I can't really describe how it behaves now, but it does move the window, simply not following the mouse.

Please bear with me as I am a total newcomer to Python. Any help is appreciated :) A rewrite of the class is okay, I know it's really badly written.

Copyright Notice:Content Author:「Martti Laine」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/7455573/drag-window-handle-for-tkinter

More about “Drag window handle for Tkinter?” related questions

Drag window handle for Tkinter?

First of all, this is my current code, essential parts of it: class WindowDraggable(): x = 1 y = 1 def __init__(self,label): label.bind('&lt;ButtonPress-1&gt;',self.Sta...

Show Detail

Drag and drop the object in Tkinter UI

I am working on making an HPE annotation tool using tkinter, and making a drag/drop UI. I am very new to tkinter so I slightly modified the code in other stackoverflow issue and it is as following....

Show Detail

How to turn off drag and drop in Tkinter?

I have some code: from tkinter import * from tkinter.dnd import Tester as DragWindow, Icon as Dragable # Make a root window and hide it, since we don't need it. root = Tk() root.withdraw() # Make ...

Show Detail

How to handle drag event on a maximized window

I have an MFC application running in Win7 with no Titlebar (i.e. My title bar is home-cooked, with custom buttons for restore, maximize and close). In Win7 it responds to the maximize event generat...

Show Detail

Getting tkinter current window handle

how can I get the handle of a tkinter window? I tried ... h = self._get_hwnd() but it doesn't work. I'm using Python IDLE Shell 3.10.1 (OS: Windows 10). Example: import tkinter as tk root=tk.Tk() ...

Show Detail

Drag and Drop in Tkinter?

What is the correct/official/proper/recommended way of accomplishing drag/drop in tkinter? My documentation section 24.1.1 includes: Tkdnd Drag-and-drop support for Tkinter. This is experimental...

Show Detail

tkinter and asyncio, window drag/resize blocks event loop, single thread

Tkinter and asyncio have some issues working together: they both are event loops that want to block indefinitely, and if you try to run them both on the same thread, one will block the other from e...

Show Detail

Bind drag function to object in Tkinter UI

I'm trying to write a solitaire playing program - mainly to explore how to use a mouse to move objects in a GUI using Tkinter. I find the following code allows the user to move a card around a window

Show Detail

How to make "Drag and Drop" and "Select a file" button in one window using tkinter?

I have been trying to make &quot;Drag and drop&quot; and &quot;select a file&quot; butoon in one tkinter window. I have two separate codes for both the functions. I need to combine them in a single

Show Detail

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

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)? Use case: We would like to creat...

Show Detail