我已经定义了一个带有'bpm'属性的类:
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
然后我调用该属性将其用作spinbox上的textvariable参数,一旦我在字典中创建了我的Section类的实例:
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)
我能够将[new_id] .bpm的值打印为120,所以我知道这有效。
但即使我在spinbox上设置textvariable,我只是在旋转盒上获得值为1,当我希望它是来自新实例的120时。即使我只是将textvariable设置为120,我仍然只有1。
答案 0 :(得分:1)
您需要将其设置为IntVar()
才能使用
self.bpm = IntVar()
self.bpm.set(bpm) # set value passed in
从旋转框中获取值时,您可以使用Spinbox.get()
,它将返回当前值的字符串或self.bpm.get()
,这将是一个int值