1

Please help! As it says in the title - I can't get checkbox' variable value.

   def init_widgets(self):
   ttk.Button(self.root, command=self.insert_txt, text='Button', width='10').place(x=10, y=10)
   ...
   cbcc = ttk.Checkbutton(root, text="Damage", onvalue="on", offvalue='off').place(x=235, y=12) 
    ...

    def insert_txt(self):
    ...
    cbcd = StringVar()
    cbcd.get()
    print(cbcd)            

    if cbcd == "on":
       self.damage
    else:

Print delivers "PY_VAR2" and counting up from there with every time I click (PY_VAR3, etc.)

1 Answer 1

1

It seems in your code, the argument 'variable' in your Checkbutton is not bounded, furthermore to get the value from IntVar or StringVar you would use (IntVar.get(), StringVar().get()), you could use the next code as example to use Checkbutton widget.

'''
CheckBox Test
References:
 http://effbot.org/tkinterbook/checkbutton.htm
'''

from tkinter import *

class App:

    def __init__(self, master):
        self.var = IntVar() #This is the variable bounded to checkbutton to 
                            #get the checkbutton state value
        #
        frame = Frame(master)        
        frame.pack()

        self.checkbutton = Checkbutton(frame, text="Hello Checkbutton",
                                       command=self.say_hello, variable=self.var)
        self.checkbutton.pack(side=LEFT)
        #
        self.button = Button(frame, text="QUIT", fg="red", command=frame.quit)
        self.button.pack(side=LEFT)       

    def say_hello(self):
        '''
         Function Bounded to Checkbutton in command parameter, every click
         either check or un-check print the current state of the checkbutton
        '''        
        print("State Changed:", self.var.get())


if __name__ == '__main__':
    root = Tk()
    app = App(root)
    #
    root.mainloop()
    root.destroy() # Quit the App when you click "Quit"

Every click on the check button, you will see printed in the console the value of the current state for the checkbutton. You can check the next reference to get an general overview tkinter widgets: http://effbot.org/tkinterbook/checkbutton.htm

I hope this snippet helps you. Very Best Regards.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.