5

I would like some help with something simple: A tkinter checkbox that does have a command attached <--this simple example is always mentioned but never shown in tutorials on the web.

I have:

from tkinter import *

def activateMotors(active):
    scale.config(state=active)


root = Tk()
root.wm_title('Servo Control')
motorsOn= IntVar()
motorsCheck=Checkbutton(root,text="Motors ON(checked)/OFF(unchecked)", variable=motorsOn, command=activateMotors)
motorsCheck.pack()
scale = Scale(root, from_=0, to=180, 
              orient=HORIZONTAL,label="Motor #",state=DISABLED)
scale.pack()
root.mainloop()

This does not work. Sure the window comes up but when I click on the checkbox I get "TypeError activateMotors() missing 1 required positional argument 'active' "

Can anybody correct this so that we can have one operational checkbox example with commands?

1
  • Have you tried removing the argument? Commented Mar 29, 2017 at 1:00

1 Answer 1

10

The callback must not have arguments, we must use the get() function of IntVar

from tkinter import *

def activateMotors():
    if motorsOn.get() == 1:
        scale.config(state=ACTIVE)
    elif motorsOn.get() == 0:
        scale.config(state=DISABLED)


root = Tk()
root.wm_title('Servo Control')
motorsOn= IntVar()
motorsCheck=Checkbutton(root, 
    text="Motors ON(checked)/OFF(unchecked)", 
    variable=motorsOn, 
    command=activateMotors)
motorsCheck.pack()

scale = Scale(root, from_=0, to=180, 
              orient=HORIZONTAL,label="Motor #",state=DISABLED)
scale.pack()
root.mainloop()
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, it seems so. some minutes later I found that similar to yours, except that I did not use ACTIVE but NORMAL. I suppose they are synonymous
@KansaiRobot according to this answer, "NORMAL enables a button while ACTIVE can change the appearance settings on mouse over etc."

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.