user506710
user506710

Reputation:

Combined Event Handling of widgets in TKinter

I am making a GUI Program in Tkinter and am running into problems.What I want to do is draw 2 checkboxes and a button. According to the user input next steps should take place. A part of my code has been shown below :-

CheckVar1 = IntVar()
CheckVar2 = IntVar()
self.C1 = Checkbutton(root, text = "C Classifier", variable = CheckVar1, onvalue = 1, offvalue = 0, height=5,width = 20).grid(row=4)

self.C2 = Checkbutton(root, text = "GClassifier", variable = CheckVar2, onvalue = 1,    offvalue = 0, height=5, width = 20).grid(row=5)

self.proceed1 = Button(root,text = "\n Proceed",command =       self.proceed(CheckVar1.get(),CheckVar2.get())).grid(row=6)

# where proceed prints the combined values of 2 checkboxes

The error that I am getting is typical ie a default value of both the selected checkboxes gets printed up and then there is no further input. The error that I get is NullType Object is not callable.

I searched on the net and I think the answer is related to lambda events or curry.

Please help ..

Upvotes: 0

Views: 602

Answers (2)

Bryan Oakley
Bryan Oakley

Reputation: 385820

Like Peter Milley said, the command option needs a reference to a function (ie: give it a function name (ie: no parenthesis). Don't try to "inline" something, create a special function. Your code will be easier to understand and to maintain.

Upvotes: 0

Peter Milley
Peter Milley

Reputation: 2808

You're passing the value of self.proceed(CheckVar1.get(),CheckVar2.get()) to the Button constructor, but presumably what you want is for command to be set to a function which will call self.proceed(CheckVar1.get(),CheckVar2.get()) and return a new, possibly different value every time the button is pressed. You can fix that with a lambda, or by wrapping the call in a short callback function. For example, replace the last line with:

def callback():
    return self.proceed(CheckVar1.get(), CheckVar2.get())
self.proceed1 = Button(root, text="\n Proceed", command=callback).grid(row=6)

This is pretty typical Tkinter. Remember: when you see a variable called command in Tkinter, it's looking for a function, not a value.

EDIT: to be clear: you're getting 'NullType Object is not callable' because you've set command to equal the return value of a single call to self.proceed (that's the NullType Object). self.proceed is a function, but its return value is not. What you need is to set command to be a function which calls self.proceed.

Upvotes: 1

Related Questions