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'?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With