cannot change CheckButton variable value

48 views Asked by At

I have a little problem with the checkbtn value.

Here is my code:

def select_data():
        select_window = Tk()

        var_autotrader = IntVar()
        check_sites_autotrader = Checkbutton(select_window, text = 'autotrader', variable = var_autotrader)
        var_bestcarfinder = IntVar()
        var_cardady = IntVar()
        var_cars = IntVar()
        var_car_gurus = IntVar()
        var_iseecars = IntVar()
        var_truecar = IntVar()

        check_sites_bestcarfinder = Checkbutton(select_window, text = 'bestcarfinder', variable = var_bestcarfinder)
        check_sites_cardaddy = Checkbutton(select_window, text = 'cardady', variable = var_cardady)
        check_sites_cars = Checkbutton(select_window, text = 'cars.com', variable = var_cars)
        check_sites_car_gurus = Checkbutton(select_window, text = 'car gurus', variable = var_car_gurus)
        check_sites_iseecars = Checkbutton(select_window, text = 'iseecars', variable = var_iseecars)
        check_sites_truecar = Checkbutton(select_window, text = 'truecar', variable = var_truecar)

        check_sites_autotrader.grid(row = 0, column = 0, sticky = W)
        check_sites_bestcarfinder.grid(row = 1, column = 0, sticky = W)
        check_sites_cardaddy.grid(row = 2, column = 0, sticky = W)
        check_sites_cars.grid(row = 3, column = 0, sticky = W)
        check_sites_car_gurus.grid(row = 4, column = 0, sticky = W)
        check_sites_iseecars.grid(row = 5, column = 0, sticky = W)
        check_sites_truecar.grid(row = 6, column = 0, sticky = W)
         def upload_selected_data():
            print(var_autotrader.get())// This one does not give me 1 when check is on
            

        btn = Button(select_window, text = 'go!',  command = upload_selected_data)
        btn.grid(row = 7, column = 0, sticky = W+E)

The trouble is that my variable is not changing when I check the box

Thank you very much!

1

There are 1 answers

0
milanbalazs On BEST ANSWER

Probably you created your "main window" with Tk(). In this case you should create another window with the Toplevel() (Not with Tk()).

It means in your case you should change the select_window = Tk() line to select_window = Toplevel()