Question

I have a small GUI application that listens for network messages so a user can update some info and accept it. This is in a production factory environment and used for interacting with a specific piece of physical hardware (over serial in some cases). The workflow looks like this:

  • User is interacting with another program (5250 Green Screen)
  • They enter a certain keybinding that sends a UDP message to a Tkinter GUI
  • The Tkinter GUI does a deiconify()
  • User edits data, accepts (Enter) and it does an iconify()

My issue is that on windows XP, the GUI does not become active when I do the deiconify and conversely does not fall back to the prior window on iconify. I have tried some things I found in other questions such as:

  • Setting the Tk GUI as top.. self.wm_attributes("-topmost", 1)
  • Trying to set/force focus... self.focus_set() and self.focus_force()

Although the window is visible with the first, I can not seem to get it to be the active window so that the user can type in it without "clicking" on it to activate. The same is true for releasing the "focus" so that the active window becomes the one they were previously on (5250).

It seems like an issue that others also have had but I have not been able to find anything that works. Is there a programmatic way to get the window activated and release it when done?

Was it helpful?

Solution

Unfortunately, after a week there have been no answers and I was not able to find a direct way to do this with Tkinter. I did find a way to solve the problem though and it appears to work consistently. Here are the steps I took to make the screens activate:

  • Install pywin32.
  • Create a function that activates the tk app.
  • Create a function that activates the 5250.

Then each time I do a iconify/deiconify I also run the function to activate the appropriate screen. The code that activates the tk window looks like this:

def activate_self(self):
    """ Activate this window. """
    shell = win32com.client.Dispatch('WScript.Shell')
    shell.AppActivate(str(self.title))
    shell = None

The code that activates the caller is a little ugly since it has to guess the title but is the same basic concept.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top