472,342 Members | 1,681 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,342 software developers and data experts.

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 4880
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Kim Petersen | last post by:
Is it possible in Tkinter to simulate a keystroke into the current active (focused widget) by pressing another button (this is for a virtual...
0
by: Todd Miller | last post by:
I'm working on a Tkinter backend for the matplotlib plotting software. matplotlib can be run interactively from some form of Python shell while...
2
by: Irmen de Jong | last post by:
Hi, I'm having trouble with the code below. It's just a regular Tk text widget in which you can type and select text as expected, however the call...
9
by: Roger Withnell | last post by:
Tearing hair out time! Simple attached page shows the problem. http://www.brilley.co.uk/TestFocusSelect.htm Using a function to test if too many...
17
by: Neil Ginsberg | last post by:
OK, this is a stupid thing, but I can't seem to get this to work. I have a form with a subform (in continuous form view). A combo box on the main...
1
by: Workgroups | last post by:
Not having much luck tracking this down... I'm working with a form, Form1, which contains a panel called pnlParent. Both Form1 and pnlParent have...
5
by: annagel | last post by:
I am looking for a way to force a Tkinter window into focus on a system level. I know the force focus method should bring one window of my...
10
by: blaine | last post by:
Hey everyone! I'm not very good with Tk, and I am using a very simple canvas to draw some pictures (this relates to that nokia screen emulator I...
0
by: William McBrine | last post by:
So, basically what I'm doing is this: window1 = Tkinter.Tk() .... window1.destroy() .... window2 = Tkinter.Tk() This works well in Linux...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...

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.