0

I am having trouble getting the On/OFF state of a tkinter checkbox button from its grid position. In this basic example, I want to print the text if the checkbox is on, but I keep getting the error that the checkbutton object has no attribute get, although when I click on one of the checkboxes, the test function prints "on" or "off" just fine.

import tkinter as tk

def gui(root):
    root.geometry('150x150')
    root.config(background='snow3')

    for row in range(5):
        checkboxVar = tk.IntVar()
        checkbox = tk.Checkbutton(root, text='', variable=checkboxVar, command= lambda status=checkboxVar: test(status=status))
        checkbox.select()
        checkbox.grid(row=row, column=1)
        textbox = tk.Text(root, height=1, width=10)
        textbox.grid(row=row, column=2)
    saveBtn = tk.Button(root, text='Save', command=save)
    saveBtn.grid(row=6, column=1)


def save():
    for row in range(5):
        print(root.grid_slaves(row=row, column=2)[0].get('1.0', 'end-1c'))
        if root.grid_slaves(row=row, column=1)[0].get() == 1:
            print(root.grid_slaves(row=row, column=2)[0].get('1.0', 'end-1c'))


def test(status):
    if status.get() == 0:
        print('OFF')
    if status.get() == 1:
        print('ON')

if __name__ == '__main__':
    root = tk.Tk()
    gui(root)
    tk.mainloop()
AttributeError: 'Checkbutton' object has no attribute 'get'
Lzypenguin
  • 945
  • 1
  • 7
  • 18

1 Answers1

2

Your code is doing everything right even the error is correct, Checkbutton doesn't have any get() attribute. I think you are trying to get the value of checkboxVar instead. But as there is no connection between the function and I think it is not possible to get the instance of Variable in your code.

So to fix the problem you can save all the checkboxVar values to a list or dictionary for later use or save them to their respective Checkbuttons.

...
    for row in range(5):
        checkboxVar = tk.IntVar()
        checkbox = tk.Checkbutton(root, text='', variable=checkboxVar, command=lambda status=checkboxVar: test(status=status))
        checkbox.select()
        checkbox.var = checkboxVar  # SAVE VARIABLE
        checkbox.grid(row=row, column=1)
        textbox = tk.Text(root, height=1, width=10)
        textbox.grid(row=row, column=2)
    saveBtn = tk.Button(root, text='Save', command=save)
    saveBtn.grid(row=6, column=1)
...

Which later can be called from the checkbutton's instance like checkbutton.var.get(). So here is a small change in your save() function

def save():
    for row in range(5):
        print(root.grid_slaves(row=row, column=2)[0].get('1.0', 'end-1c'))
        if root.grid_slaves(row=row, column=1)[0].var.get():
            print(root.grid_slaves(row=row, column=2)[0].get('1.0', 'end-1c'))
Saad
  • 3,340
  • 2
  • 10
  • 32
  • Thank you so much! This fixed my issue. How did you know (or I will know in the future) to save the checkboxVar to checkbox.var, and then reference that later? Why is it needed when calling the grid position, but not in the test function? Thanks!!! – Lzypenguin Jan 30 '21 at 22:32
  • Well `test(status)` has a different story, you are passing the variable as an argument to the function whereas in your save function you are referencing to the check-buttons. Either way it can be done, like you can also pass the `checkboxVar` as an argument to save function as well. – Saad Jan 30 '21 at 22:37