focus trap in tkinter

V

vigacmoe

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!
 
K

kyosohma

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/python-list/2000-January/021226.html

Maybe it will give you an idea.

Lundh's site has a lot of event information on it:
http://effbot.org/tkinterbook/tkinter-events-and-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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,238
Latest member
Top CryptoPodcasts

Latest Threads

Top