Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python Tkinter Getting value of CheckButton from children list

I got widget list of frame with frame.winfo_children(). Now i need to get that checkbutton’s value.Children List:

[<tkinter.Label object .!toplevel2.!labelframe.!label>, <tkinter.Entry object .!toplevel2.!labelframe.!entry>, <tkinter.Entry object .!toplevel2.!labelframe.!entry2>, <tkinter.Checkbutton object .!toplevel2.!labelframe.!checkbutton>]

What i tried :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

checkbuttonwidget.get() , checkbuttonwidget.cget('variable')

How can i get its value? And is there any option to use if command for split these children?
For example:

for wid i frame.winfo_children():
    if wid == LABEL(option?):
        print('yes its a label')
    elif wid == CheckButton(option?):
        print('Its a CheckButton')

Thanks..

>Solution :

The simplest way to check if the widget is a Checkbutton is to use isinstance, then to get the value, you can use .getvar and the widget['variable'] (or widget.cget('variable') depending on what you prefer) to get the value of the corresponding variable:

for widget in frame.winfo_children():
    if isinstance(widget, tk.Checkbutton):
        value = frame.getvar(widget['variable'])
        print(value)

Also depending on how you imported you may need to use Checkbutton instead of tk.Checkbutton

Useful:

  • isinstance docs

  • getvar(self, name='PY_VAR')
        Return value of Tcl variable NAME.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading