10

I need to be able to check if a tkinter widget is visible (if its pack or grid method has been called).

I'm currently doing the following:

def is_visible(widget):
    try:
        widget.pack_info()
    except tk.TclError:
        # pack_info raises if pack hasn't been
        # called yet.
        return bool(widget.grid_info())
        # grid_info returns {} if grid hasn't been
        # called yet.
    else:
        return True

Is there any way in which I could improve this code, by using a standard widget attribute, or through some other solution that's less 'hacky'?

1 Answer 1

14

You can try widget.winfo_ismapped() instead :

Check if the window has been created. This method checks if Tkinter has created a window corresponding to the widget in the underlying window system (an X window, a Windows HWND, etc).

Returns: A true value if a window has been created.

1

Not the answer you're looking for? Browse other questions tagged or ask your own question.