Moving widgets in Tkinter
Eric Brunel
eric.brunel at N0SP4M.com
Mon Jan 12 04:26:01 EST 2004
Adonis wrote:
> I wish to manually move widgets in Tkinter, now I have successfully done it,
> but with odd results, I would like to move the widgets with a much smoother
> manner, and better precision.
>
> Any help is greatly appreciated.
>
> --
>
> here is snip of working code:
>
> from Tkinter import *
>
> class blah:
>
> def MoveWindow(self, event):
> self.root.update_idletasks()
> self.f.place_configure(x=event.x_root, y=event.y_root-20)
event.x_root & event.y_root will give you the coordinates of the event in the
*screen*, which is apparently not what you want. The name "root" traditionally
used for Tkinter main windows is somewhat confusing here: the window called
"root" in tk/Tkinter methods is the screen, not your main window.
> def __init__(self):
> self.root = Tk()
> self.root.title("...")
> self.root.resizable(0,0)
> self.root.geometry("%dx%d%+d%+d"%(640, 480, 0, 0))
>
> self.f = Frame(self.root, bd=1, relief=SUNKEN)
> self.f.place(x=0, y=0, width=200, height=200)
>
> self.l = Label(self.f, bd=1, relief=RAISED, text="Test")
> self.l.pack(fill=X, padx=1, pady=1)
>
> self.l.bind('<B1-Motion>', self.MoveWindow)
> self.f.bind('<B1-Motion>', self.MoveWindow)
>
> self.root.mainloop()
>
> x = blah()
Doing what you want is a bit more complicated than what you've already done: the
best way to use event.x_root and event.y_root here is relatively to a former
recorded position. What I'd do would be the following:
-----------------------------------------
from Tkinter import *
class blah:
def startMoveWindow(self, event):
## When the movement starts, record current root coordinates
self.__lastX, self.__lastY = event.x_root, event.y_root
def MoveWindow(self, event):
self.root.update_idletasks()
## Use root coordinates to compute offset for inside window coordinates
self.__winX += event.x_root - self.__lastX
self.__winY += event.y_root - self.__lastY
## Remember last coordinates
self.__lastX, self.__lastY = event.x_root, event.y_root
## Move inside window
self.f.place_configure(x=self.__winX, y=self.__winY)
def __init__(self):
self.root = Tk()
self.root.title("...")
self.root.resizable(0,0)
self.root.geometry("%dx%d%+d%+d"%(640, 480, 0, 0))
## Record coordinates for window to avoid asking them every time
self.__winX, self.__winY = 0, 0
self.f = Frame(self.root, bd=1, relief=SUNKEN)
self.f.place(x=self.__winX, y=self.__winY, width=200, height=200)
self.l = Label(self.f, bd=1, relief=RAISED, text="Test")
self.l.pack(fill=X, padx=1, pady=1)
## When the button is pressed, make sure we get the first coordinates
self.l.bind('<ButtonPress-1>', self.startMoveWindow)
self.l.bind('<B1-Motion>', self.MoveWindow)
self.f.bind('<ButtonPress-1>', self.startMoveWindow)
self.f.bind('<B1-Motion>', self.MoveWindow)
self.root.mainloop()
x = blah()
-----------------------------------------
HTH
--
- Eric Brunel <eric dot brunel at pragmadev dot com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com
More information about the Python-list
mailing list