0

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!

  • Does this answer your question? [Tkinter Checkbuttons not changing the variable value](https://stackoverflow.com/questions/58012487/tkinter-checkbuttons-not-changing-the-variable-value) – acw1668 Oct 21 '20 at 13:29
  • No, becasue that window this is a main window. Thank you! – Alexandru Manole Oct 21 '20 at 13:45
  • The code you posted works as expected for me on python 3.6.8 Win10. I think more code or information is needed to see where your problem is since it doesn't seem to be in this isolated piece of code. – fhdrsdg Oct 21 '20 at 13:57
  • Does this code _really_ reproduce the problem? I don't see where you're calling `mainloop`, so it would be impossible for the user to even see the window much less interact with it. There also appears to be an indentation error with `def upload_selected_data` – Bryan Oakley Oct 21 '20 at 14:18
  • If `select_window` is passed to all `IntVar()` like `IntVar(select_window)`, do you get what you want? If yes, then it is definitely `select_window` is not the root window. – acw1668 Oct 21 '20 at 14:37
  • If you are using `Tk()` more than once in your entire app, that is your problem. You need to change all `Tk()` (except the very first one) to `Toplevel()` – OneMadGypsy Oct 21 '20 at 14:44
  • Michael Guidry, thank you very much! It Worked! Make it as a post, to mark as answer, please! – Alexandru Manole Oct 21 '20 at 15:26

1 Answers1

1

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()

milanbalazs
  • 4,811
  • 4
  • 23
  • 45