Determine which Tkinter widget is on top (or visible)

python, tkinter

Solution

When you click button you can set some variable to keep information what label is visible.

You can add that variable to label:

frame1 = Frame(...)
frame1.visible = False

Or you can keep access to visible frame

visible = frame1

If all frames were classes with function `update` it could update values only in that frame

visible.update( all_data )

Problem

I would like to determine which widget (frame in this case) is on top (or visible anyway). ``` from Tkinter import * spam1=0 spam2=1000 def busywork(): global spam1 global spam2 #if frame1 is on top / visible spam1=spam1+1 label1.config(text=str(spam1)) #if frame2 is on top / visable spam2=spam2+1 label2.config(text=str(spam2)) root.after(10,busywork) root=Tk() frame1=Frame(root) frame2=Frame(root) button1=Button(frame1,text="Bring Frame 2 to top",command=frame2.lift) label1=Label(frame1,text=str(spam1)) button1.pack() label1.pack() frame1.place(relx=0,rely=0) button2=Button(frame2,text="Bring Frame 1 to top",command=frame1.lift) label2=Label(frame2,text=str(spam2)) button2.pack() label2.pack() frame2.place(relx=0,rely=0) root.after(10,busywork) root.mainloop() ``` [This is close]but it works for windows not frames (or widgets in general) In my application, I have several frames. Each frame has many labels on it. The busywork function reads all of this information from a machine tool. Fetching all of the information all of the time makes the over all machine sluggish. I would like to only fetch and update the information that the user can see. I can see a way to do it by manually keeping a global that tells me the last .lift()ed window. But this does not seem clean. ( In total there are about 400 labels that get updated. However at most there are only about 60 that are visible at any given time. And some of the 'Labels' are actually graphical meter faces, so a lot of horse power is wasted updating these when they are not visible)

Original source