1

I've written an app that takes some data from the user, queries the API of a website, and returns and processes the data given by the API. I'm trying to allow the user greater control of what happens next by creating checkboxes for each item retrieved.

Because the number of items retrieved will vary on each use, I'm putting the checkboxes and the IntVars for if they're checked or not into lists. However, when I try to set the IntVars to 1 (so they all start out checked) I get a TypeError telling me it wants the IntVar instance as the first arg, but that doesn't make any sense to me as I'm trying to call a method for the IntVar in the first place.

This is a much simplified version of my code that generates an identical error:

import Tkinter as tk

class Application(tk.Frame):
    def __init__(self, master=None):
        tk.Frame.__init__(self, master)
        self.grid()

        self.test_names = ["Box 1", "Box 2", "Box 3", "Box 4"]
        self.boxes = []
        self.box_vars = []
        self.box_num = 0

        btn_test1 = tk.Button(self, text="Test 1", width = 11, command = self.test1)
        btn_test1.grid()

    def test1(self):
        for name in self.test_names:
            self.box_vars.append(tk.IntVar)
            self.boxes.append(tk.Checkbutton(self, text = name, variable = self.box_vars[self.box_num]))
            self.box_vars[self.box_num].set(1)
            self.boxes[self.box_num].grid(sticky = tk.W)
            self.box_num += 1

root = tk.Tk()
app = Application(master=root)
app.mainloop()

And the error message when pushing the button:

TypeError: unbound method set() must be called with IntVar instance as first
argument (got int instance instead)

What am I doing wrong?

1
  • How do we get the selected value from the checkboxes?, if say we have a submit button. Commented Sep 18, 2020 at 7:26

2 Answers 2

4

Classic problem. You add the type IntVar instead of adding an object of type IntVar. Add parentheses like this:

self.box_vars.append(tk.IntVar())
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I feel a little dumb that it was something so simple. Learn something new every day.
0

Yes you forgot the brackets. The result will be and not a tkinter intvar instance.

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.