473,657 Members | 2,505 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Moving widgets in Tkinter

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.updat e_idletasks()
self.f.place_co nfigure(x=event .x_root, y=event.y_root-20)

def __init__(self):
self.root = Tk()
self.root.title ("...")
self.root.resiz able(0,0)
self.root.geome try("%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(fil l=X, padx=1, pady=1)

self.l.bind('<B 1-Motion>', self.MoveWindow )
self.f.bind('<B 1-Motion>', self.MoveWindow )

self.root.mainl oop()

x = blah()
Jul 18 '05 #1
3 12537
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.updat e_idletasks()
self.f.place_co nfigure(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.resiz able(0,0)
self.root.geome try("%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(fil l=X, padx=1, pady=1)

self.l.bind('<B 1-Motion>', self.MoveWindow )
self.f.bind('<B 1-Motion>', self.MoveWindow )

self.root.mainl oop()

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.updat e_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_co nfigure(x=self. __winX, y=self.__winY)

def __init__(self):
self.root = Tk()
self.root.title ("...")
self.root.resiz able(0,0)
self.root.geome try("%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(fil l=X, padx=1, pady=1)

## When the button is pressed, make sure we get the first coordinates
self.l.bind('<B uttonPress-1>', self.startMoveW indow)
self.l.bind('<B 1-Motion>', self.MoveWindow )
self.f.bind('<B uttonPress-1>', self.startMoveW indow)
self.f.bind('<B 1-Motion>', self.MoveWindow )

self.root.mainl oop()

x = blah()
-----------------------------------------

HTH
--
- Eric Brunel <eric dot brunel at pragmadev dot com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com

Jul 18 '05 #2
Thanks a million, works like a charm!

Just another problem arose *grumble*, trying to get it to work with multiple
frames, almost there but no cigar.

Adonis

Jul 18 '05 #3
I've embellished your program a little bit ...

from Tkinter import *

def clamp(lo, hi, x):
return min(max(x, lo), hi)

class blah:
all = []
def MoveWindowStart (self, event):
self.move_lastx = event.x_root
self.move_lasty = event.y_root
self.focus()
def MoveWindow(self , event):
self.root.updat e_idletasks()

dx = event.x_root - self.move_lastx
dy = event.y_root - self.move_lasty
self.move_lastx = event.x_root
self.move_lasty = event.y_root
self.x = clamp(0, 640-200, self.x + dx) # should depend on
self.y = clamp(0, 480-200, self.y + dy) # actual size here
self.f.place_co nfigure(x=self. x, y=self.y)

def __init__(self, root, title, x, y):
self.root = root

self.x = x; self.y = y
self.f = Frame(self.root , bd=1, relief=RAISED)
self.f.place(x= x, y=y, width=200, height=200)

self.l = Label(self.f, bd=1, bg="#08246b", fg="white",text =title)
self.l.pack(fil l=X)

self.l.bind('<1 >', self.MoveWindow Start)
self.f.bind('<1 >', self.focus)
self.l.bind('<B 1-Motion>', self.MoveWindow )
# self.f.bind('<B 1-Motion>', self.MoveWindow )
self.all.append (self)
self.focus()

def focus(self, event=None):
self.f.tkraise( )
for w in self.all:
if w is self:
w.l.configure(b g="#08246b", fg="white")
else:
w.l.configure(b g="#d9d9d9", fg="black")

root = Tk()
root.title("... ")
root.resizable( 0,0)
root.geometry(" %dx%d%+d%+d"%(6 40, 480, 0, 0))
x = blah(root, "Window 1", 10, 10)
y = blah(root, "Window 2", 220, 10)
y = blah(root, "Window 3", 10, 220)
root.mainloop()

Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
7022
by: srijit | last post by:
Hello, Any idea - why the following code crashes on my Win 98 machine with Python 2.3? Everytime I run this code, I have to reboot my machine. I also have Win32all-157 installed. from Tkinter import * class App:
2
2325
by: Adonis | last post by:
I am creating some widgets by inheriting from Tkinter.Frame and populating the frame with whatever the widget will be then creating certain attributes/methods to be accessed later. My question is, is this a poper way to create widgets or should I take a different approach? Any help is greatly appreciated. Adonis
25
3345
by: BJörn Lindqvist | last post by:
See: http://www.wxpython.org/quotes.php. especially: "wxPython is the best and most mature cross-platform GUI toolkit, given a number of constraints. The only reason wxPython isn't the standard Python GUI toolkit is that Tkinter was there first." - Guido van Rossum Guess, that answers my question, but isn't "Tkinter was there first" a very bad answer? :) It is kinda ugly too, so I wonder why it can't be replaced? Or maybe another GUI...
3
2145
by: Matt Hammond | last post by:
Here's a strange one in Tkinter that has me stumped: (I'm running python 2.4 on Suse Linux 9.3 64bit) I'm trying to make a set of Entry widgets with Label widgets to the left of each one, using the grid layout. If I make and grid the Label *before* the Entry then the Entry widget doesn't seem to work - it lets me put the cursor in it, but I can't type! See example code below. Is this just me doing something really really silly, or is...
2
1731
by: William Gill | last post by:
I need to display a couple of labels and a checkbox from each entry in my database. Simple enough, but there are several hundred records, and I only want to display 5 or 10 at a time. Can this be accomplished by putting everything in a Frame(), using width, height, grid_propagate(0) , and a scrollbar? or do I have to grid 5 rows at a time? If the latter, can I just grid over the previous 5 or do they have to be explicitly removed...
2
2827
by: nholtz | last post by:
Is there any way to delete a widget (window) from a Text widget, and then add it back to the Text, without re-creating the original widget. For example, I think I would like to do something like the following: ########################################################## from Tkinter import * root = Tk()
32
7464
by: Kevin Walzer | last post by:
I'm a Tcl/Tk developer who has been working, slowly, at learning Python, in part because Python has better support for certain kinds of applications that I want to develop than Tcl/Tk does. Naturally, I thought that I would use Tkinter as the GUI for these programs. However, in doing research into GUI development techniques, sample code, and showcase applications, what has struck me is how little sophisticated GUI development seems to be...
2
5373
by: Ben Finney | last post by:
Howdy all, Python programmers looking for a built-in GUI toolkit are told two things: one, Python already comes with a GUI toolkit, and two, it looks equally ugly on all platforms. This is because the Tk widget library, that Tkinter uses, defaults to looking like Motif, which hasn't been the default widget set of *anything* for a long time. The Tk folks are apparently getting their act together. Tile is a "theming engine" for Tk with...
0
1800
by: Guilherme Polo | last post by:
On 10/29/08, Olrik Lenstra <o.lenstra@gmail.comwrote: It will be a combination of commands, not a single one. Initially I considered this as "probably without solution", since tcl acquired a yield command just in the 8.6a3 release, but then I looked at wx.SafeYield code and apparently it is possible to replicate it. Here is an initial cut, it is very possible to contain something not equivalent to wx.SafeYield (besides it could be...
0
8420
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8842
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8740
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8617
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2743
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1970
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.