473,856 Members | 1,762 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(Tkint er.Frame):
def __init__(self, master = None):
Tkinter.Frame._ _init__(self, master)
self.ticker = Tkinter.Label(s elf)
self.pack()
self.ticker.pac k()
self.ticker["text"] = "foo"
self.master.bin d("<FocusIn>" , self.__focus_in )
self.master.bin d("<FocusOut> ", self.__focus_ou t)
self.master.wm_ attributes("-topmost", 1)

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

def __focus_out(sel f, detail):
self.master.wm_ attributes("-alpha", 0.8)
if ("__main__" == __name__):
root = Tkinter.Tk()
app = countdown(maste r = 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.unb ind(), 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 5136
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(Tkint er.Frame):
def __init__(self, master = None):
Tkinter.Frame._ _init__(self, master)
self.ticker = Tkinter.Label(s elf)
self.pack()
self.ticker.pac k()
self.ticker["text"] = "foo"
self.master.bin d("<FocusIn>" , self.__focus_in )
self.master.bin d("<FocusOut> ", self.__focus_ou t)
self.master.wm_ attributes("-topmost", 1)

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

def __focus_out(sel f, detail):
self.master.wm_ attributes("-alpha", 0.8)

if ("__main__" == __name__):
root = Tkinter.Tk()
app = countdown(maste r = 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.unb ind(), 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
1895
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 keyboard on a touchscreen pc). the troubles i see is: on pressing the button focus changes to it if you set focus back, some inputfields will act specially (eg. on focus out/focus in, keypress) i know i have to use the "event" function in tkinter to send the keystroke - but i still have the above...
0
1371
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 plotting to a Tk window. One annoyance we've noticed is that when we update the plot window, it steals the focus from the shell window, at least on a Win32 platform. I am very new to Tkinter so I am looking for insight into how the focus works on win32 and how to prevent the Tkinter window from...
2
4169
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 to tkFileDialog.askopenfilename() seems to screw things up. After the file dialog, I can no longer use the Text widget (typing, selecting, it doesn't work anymore!) What am I doing wrong? TIA, --Irmen de Jong
9
3717
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 characters have been keyed in to a textarea. Obviously, must return focus and select to the textarea if too many have been keyed. When the function is called with onblur, it works in IE but not in Safari or NN. The function is called but the focus() and select() methods fail.
17
3873
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 form has code in the AfterUpdate event which adds a record to the subform (based on the value of the combo box) and requeries the subform control. I want the focus to return to the combo box on the main form when it's done, but I can't get it to do so if the user enters a value and presses Enter...
1
1972
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 AutoScroll = False. pnlParent contains a panel called pnlScroller. pnlScroller.AutoScroll = True. Finally, pnlScroller contains a custom control I wrote (in which I override OnPaint & paint entirely custom, if that matters). So in summary there are 4 controls with the following...
5
7903
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 application into focus, but it seems I need to have some part of my application already in focus on a system level to make this work. If for example I am entering text at the command line and call a force_focus on my root window the call will not return till I click on a window in my application. The...
10
7384
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 had a post about a few days ago). Anyway, all is well, except one thing. When I am not in the program, and the program receives a draw command (from a FIFO pipe), the canvas does not refresh until I click into the program. How do I force it to refresh, or force the window to gain focus? It...
0
1061
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 and Mac OS X (and the PyGtk equivalent works on all platforms), but in Windows XP, the second window comes up without
0
9904
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
11051
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
10693
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...
1
10773
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9527
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7929
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7087
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
5956
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4170
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.