我使用单选按钮选择来更改窗口中的标签小部件,我刚刚编写了这段代码,以避免自己编写本质上相同的东西四次,但是当它运行时,它会创建默认选中的单选按钮。

下面是所讨论的代码:
# At the top of the method
radio_text = ['The Tivoli Theatre', 'Brisbane City', 'Suncorp Stadium']
radio_buttons_list = []# Radio buttons are stored in a frame, according to a grid layout. When clicked they call the Generate_Event_Info method and
# the value passed along corrosponds to a case in the method. Production version will use database input to generate responses.
self.v = IntVar()
for i in range(len(radio_text)):
radio_buttons_list.append(Radiobutton(self.radio_options_frame, text=radio_text[i], font=self.style, bg=self.bg_colour, variable=self.v, value=0, command=self.Generate_Event_Info).grid(row=i, column=0, pady=5, sticky='w'))很抱歉,在那里的最后一行非常长,你可以看到为什么我想要处理它的循环。当我在没有循环的情况下一个一个地创建它们时,没有问题,但是代码是一团糟的(不是世界末日,但我仍然好奇是什么导致了这个问题)。任何帮助都将不胜感激!
发布于 2022-05-13 11:32:13
我修好了。我不想问这个问题,但我在这里发了个答案。结果,我忘记给循环索引分配值了。我更改了它,在小部件自动的情况下仍然有一个问题--选择第一个选项,因为v的默认值为0。我使用set()函数将v变量设置为-1,并修复了它!下面是修改后的代码,在修改后的部分旁边有星号:
self.v = IntVar()
self.v.set(-1)**
for i in range(len(radio_text)):
radio_buttons_list.append(Radiobutton(self.radio_options_frame, text=radio_text[i], font=self.style, bg=self.bg_colour, variable=self.v, value=i**, command=self.Generate_Event_Info).grid(row=i, column=0, pady=5, sticky='w'))https://stackoverflow.com/questions/72228790
复制相似问题