Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a tkinter widget is visible

Tags:

python

tkinter

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'?

like image 960
Daniel Avatar asked Sep 23 '17 07:09

Daniel


1 Answers

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.

like image 51
PRMoureu Avatar answered Nov 08 '22 04:11

PRMoureu