Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7736573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:46:12+00:00 2026-06-01T07:46:12+00:00

Using following code I try to get updated list of checkbuttons’ corresponding text values,

  • 0

Using following code I try to get updated list of checkbuttons’ corresponding text values, everytime checkbutton is checked or unchecked:

import Tkinter as tk

opt = []
def chkbox_checked():
    for ix, item in enumerate(cb):
        opt.append(cb_v[ix].get())
    print opt
root = tk.Tk()
mylist = [
'NR',
'ECEF X',
'ECEF Y',
'ECEF Z',
'height'
]
cb = []
cb_v = []
for ix, text in enumerate(mylist):
    cb_v.append(tk.StringVar())
    cb.append(tk.Checkbutton(root, text=text, onvalue=text, variable=cb_v[ix],   comand=chkbox_checked))
    cb[ix].grid(row=ix, column=0, sticky='w')   
label = tk.Label(root, width=20)
label.grid(row=ix+1, column=0, sticky='w')
root.mainloop()

If for example all buttons are checked from the first to the last, my desired output would be:

['NR']
['NR','ECEF X]
['NR','ECEF X','ECEF Y']
['NR','ECEF X','ECEF Y','ECEF Z]
['NR','ECEF X','ECEF Y','ECEF Z','height',]

but with above code I get multiplied output and also there’s something wrong with checkbuttons themselves, their state is checked from the beginning.
Any help would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T07:46:13+00:00Added an answer on June 1, 2026 at 7:46 am

    One problem with the above is the opt.append in chkbox_checked … Since this function gets called everytime a button is checked/unchecked, the length of the opt list will increase by the number of checkbuttons you have everytime one of the buttons is clicked. The solution (posted below) is to initialize opt when you create the buttons and then just update it’s elements in chkbox_checked. As far as the state of the buttons on creation, I’m not sure why they’re initially checked, but you can easily deselect the buttons at initialization as well using the deselect method.

    import Tkinter as tk
    
    opt = []
    def chkbox_checked():
        for ix, item in enumerate(cb):
            opt[ix]=(cb_v[ix].get())
        print opt
    root = tk.Tk()  
    mylist = [
    'NR',
    'ECEF X',
    'ECEF Y',
    'ECEF Z',
    'height' 
    ]
    cb = []
    cb_v = []
    for ix, text in enumerate(mylist):
        cb_v.append(tk.StringVar())
        off_value=0  #whatever you want it to be when the checkbutton is off
        cb.append(tk.Checkbutton(root, text=text, onvalue=text,offvalue=off_value,
                                 variable=cb_v[ix],
                                 command=chkbox_checked))
        cb[ix].grid(row=ix, column=0, sticky='w')
        opt.append(off_value)
        cb[-1].deselect() #uncheck the boxes initially.
    label = tk.Label(root, width=20)
    label.grid(row=ix+1, column=0, sticky='w')
    root.mainloop()
    

    Another trick that may be useful is instead of keeping 2 lists (cb and cb_v), you could just add the StringVars as attributes to your checkbuttons. e.g.:

    v=tk.StringVar()
    cb.append(tk.CheckButton(... , variable=v, ...)
    cb[-1].v=v
    

    Then you just have one list with all the data. The corresponding chkbox_checked would look like:

    def chkbox_checked():
       opt=[chkbox.v.get() for chkbox in cb]
       print opt
    

    (Note this also eliminates the need for a global opt list … although there are probably a whole bunch of other ways to get rid of that list)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently downloading an HTML page, using the following code: Try Dim req As
I'm using the following code to try to read the results of a df
I am using the following code to try and display in a time in
The following code is a try to encrypt data using AES with asymmetric key:
We try to convert from string to Byte[] using the following Java code: String
I am trying to send email using c# following is my code. try {
I am using the following code snippet to copy/backup database Public Sub backupData() Try
I have the following code: using System; using System.Collections.Generic; using System.Linq; using System.Text; using
I am using the following code do get all data records from a MS
I using following code: var search = 'test'; if ($('#sku').find(search) ){ //alert(search); $(document).find(search).css('color','red'); <TABLE>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.