Python Forum
[Tkinter] How to determine the stack order or topmost top-level window
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] How to determine the stack order or topmost top-level window
#8
Thanks again Larz, Sebastian, and Gribouillis for your help. All of the links were useful and educational.

I have a solution to the problem of finding which of the matplotlib windows is frontmost which avoids the issue that they have no common root. I'm posting it because somebody may stumble over this thread and just possibly it will be useful. However, it's not really ideal for a number of reasons, such as dependency on backend. I think that somewhere there's some simple call that would let me skip all of this.

Anyhow, here's my modified version: the idea is to put a callback on the FocusIn event so that the program keeps itself keeps track of where the focus last was.

This part would be stored in a file vers2_mw.py ('vers2', because it's version 2):

front_win = None
def make_wins( cnt = 3 ):
    def make_func( w ):
        def replace_front( event ):
            global front_win
            front_win = w
        return replace_front
    figs=[None]*cnt
    axs =[None]*cnt
    for i in range(cnt):
        figs[i],axs[i] = plt.subplots()
    plt.show(block=False)
    wins=[None]*cnt
    for i in range(cnt):
        wins[i] = figs[i].canvas.manager.window
        wins[i].bind( '<FocusIn>', make_func( wins[i] ) )
    global front_win
    front_win = wins[cnt-1]
    return wins
Then you can demonstrate it like this:
import vers2_mw
from vers2_mw import *
w = make_wins()
vers2_mw.front_win == w[2]
# This last line should print True
# Now click around and select window 0:
vers2_mw.front_win == w[0]
# The previous line should also print True
vers2_mw.front_win == w[1]
# The previous line here will print False unless window 1 was chosen.
So this works, and is good enough for the program that i'm developing, but a more universal solution would be useful (and thanks for any suggestions, btw).

Thanks again everybody! :)
Reply


Messages In This Thread
RE: How to determine the stack order or topmost top-level window - by dan - Jan-24-2018, 05:18 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Interaction between Matplotlib window, Python prompt and TKinter window NorbertMoussy 3 611 Mar-17-2024, 09:37 AM
Last Post: deanhystad
  tkinter window and turtle window error 1885 3 6,795 Nov-02-2019, 12:18 PM
Last Post: 1885
  [Tkinter] Top Level Window - from tkinter import * francisco_neves2020 6 4,223 Apr-23-2019, 09:27 PM
Last Post: francisco_neves2020
  top level window juliolop 3 3,933 Nov-07-2018, 02:52 PM
Last Post: juliolop
  update a variable in parent window after closing its toplevel window gray 5 9,175 Mar-20-2017, 10:35 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020