469,576 Members | 1,785 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,576 developers. It's quick & easy.

focus trap in tkinter

Hi all,

I'm trying to write a simple tkinter program, then this problem popped
up. The followin code will describe the problem.

------------------------------------------

import Tkinter

class countdown(Tkinter.Frame):
def __init__(self, master = None):
Tkinter.Frame.__init__(self, master)
self.ticker = Tkinter.Label(self)
self.pack()
self.ticker.pack()
self.ticker["text"] = "foo"
self.master.bind("<FocusIn>", self.__focus_in)
self.master.bind("<FocusOut>", self.__focus_out)
self.master.wm_attributes("-topmost", 1)

def __focus_in(self, detail):
self.master.wm_attributes("-alpha", 1)

def __focus_out(self, detail):
self.master.wm_attributes("-alpha", 0.8)
if ("__main__" == __name__):
root = Tkinter.Tk()
app = countdown(master = root)
app.mainloop()

------------------------------------------

My aim is to set the alpha property of the toplevel window to 0.8 when
it lose focus, and set it back to 1 when it regain focus, pretty
simple. But the problem is, whenever wm_attributes() is called, a new
focus event is generated, and then wm_attributes() will be called
again, then the program runs into endless loop of receiving focus
events. Simply run above code will see what's going on.

I tried to disable FocusIn event in the event handlers, by calling
self.master.unbind(), but it doesn't work.

I also tried to bind FocusIn and FocusOut event to the frame, the
label widgets, instead of the toplevel window, but both frame and
label widgets doesn't receive focus event, even after I set their
takefocus properties to 1.

There should be a way to make it work, just I didn't find it. Any
input?

Best regards!

Apr 9 '07 #1
1 4559
On Apr 9, 8:27 am, "vigac...@gmail.com" <vigac...@gmail.comwrote:
Hi all,

I'm trying to write a simple tkinter program, then this problem popped
up. The followin code will describe the problem.

------------------------------------------

import Tkinter

class countdown(Tkinter.Frame):
def __init__(self, master = None):
Tkinter.Frame.__init__(self, master)
self.ticker = Tkinter.Label(self)
self.pack()
self.ticker.pack()
self.ticker["text"] = "foo"
self.master.bind("<FocusIn>", self.__focus_in)
self.master.bind("<FocusOut>", self.__focus_out)
self.master.wm_attributes("-topmost", 1)

def __focus_in(self, detail):
self.master.wm_attributes("-alpha", 1)

def __focus_out(self, detail):
self.master.wm_attributes("-alpha", 0.8)

if ("__main__" == __name__):
root = Tkinter.Tk()
app = countdown(master = root)
app.mainloop()

------------------------------------------

My aim is to set the alpha property of the toplevel window to 0.8 when
it lose focus, and set it back to 1 when it regain focus, pretty
simple. But the problem is, whenever wm_attributes() is called, a new
focus event is generated, and then wm_attributes() will be called
again, then the program runs into endless loop of receiving focus
events. Simply run above code will see what's going on.

I tried to disable FocusIn event in the event handlers, by calling
self.master.unbind(), but it doesn't work.

I also tried to bind FocusIn and FocusOut event to the frame, the
label widgets, instead of the toplevel window, but both frame and
label widgets doesn't receive focus event, even after I set their
takefocus properties to 1.

There should be a way to make it work, just I didn't find it. Any
input?

Best regards!
I found some info on focus events in a previous post:
http://mail.python.org/pipermail/pyt...ry/021226.html

Maybe it will give you an idea.

Lundh's site has a lot of event information on it:
http://effbot.org/tkinterbook/tkinte...d-bindings.htm

If you were using wxPython, you'd do an event.Skip(), but I can't seem
to find that functionality in Tkinter. Maybe you'll have better luck.
Hopefully a Tkinter guru will come to the rescue.

Sorry I wasn't more help.

Mike

Apr 9 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Kim Petersen | last post: by
reply views Thread by Todd Miller | last post: by
2 posts views Thread by Irmen de Jong | last post: by
9 posts views Thread by Roger Withnell | last post: by
17 posts views Thread by Neil Ginsberg | last post: by
5 posts views Thread by annagel | last post: by
reply views Thread by William McBrine | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.