jonnyg
jonnyg

Reputation: 157

Tkinter Spinbox textvariable argument has no effect. Is there something I missed?

I've defined a class with a 'bpm' attribute:

class Section:
    def __init__(self, id, bpm, bars, reps, num_tracks):
        self.id = id
        self.bpm = bpm
        self.bars = bars
        self.reps = reps
        self.num_tracks = num_tracks

then I call the attribute to use it as the textvariable argument on a spinbox, once I've created an instance of my Section class, inside a dictionary:

def add_section():
    new_id = next(itertools.count(1))
    sections[new_id] = Section(new_id, 120, 1, 2, 1)
    print((str(sections[new_id].bpm)))

    sections[new_id].label_section_title = Label(root, text="Section {}".format(sections[new_id].id, relief = GROOVE))
    sections[new_id].label_section_title.grid(row = 1, column = 4, columnspan = 5)

    sections[new_id].label_bpm = Label(root, text="BPM: ")
    sections[new_id].label_bpm.grid(row = 2, column = 4)

    sections[new_id].bpm_control = Spinbox(root, from_ = 1, to = 999, textvariable = sections[new_id].bpm, command = lambda: sections[new_id].bpm_change(sections[new_id].bpm_control.get()))
    sections[new_id].bpm_control.grid(row = 2, column = 5)

I'm able to print the value of sections[new_id].bpm as 120, so I know that works.

But even though I am setting textvariable on the spinbox, I'm just getting a value of 1 on the spinbox when I'd like it to be the 120 from the new instance. Even if I just set textvariable to straight up 120 I still just get a 1 there.

Upvotes: 0

Views: 448

Answers (1)

Steven Summers
Steven Summers

Reputation: 5384

You need to set it as a IntVar() to use

self.bpm = IntVar()
self.bpm.set(bpm) # set value passed in

When getting the value from your spinbox you get either use Spinbox.get() which will return a string of the current value or self.bpm.get() which will be an int value

Upvotes: 1

Related Questions