[英]Tkinter - How do I populate a combo box with a list?
我实际上正在使用 Custom Tkinter,但我认为它应该是一样的。
我想在单击按钮后填充一个组合框。 我的按钮调用一个函数,该函数返回一个列表。 我想用该列表中的每个项目填充组合框,但我似乎无法掌握它。
这是应用程序的一个片段,您实际上可以复制粘贴它并运行它:
import boto3
import customtkinter
ses = boto3.client('ses')
class App(customtkinter.CTk):
def __init__(self):
super().__init__()
# configure window
self.title("App")
self.geometry(f"{1200}x{700}")
self.load_template_button = customtkinter.CTkButton(master=self, text="Load Template", command=self.get_templates)
self.load_template_button.grid(row=3, column=0, padx=5, pady=5)
self.templates_list_cb = customtkinter.CTkComboBox(master=self)
self.templates_list_cb.grid(row=4, column=0, padx=5, pady=5)
def get_templates(self):
templates_list = []
response = ses.list_templates()
for template in response['TemplatesMetadata']:
templates_list.append(template['Name'])
print(templates_list)
self.templates_list_cb['values'] = templates_list
return templates_list
if __name__ == "__main__":
app = App()
app.mainloop()
据我了解:我的按钮load_template_button
执行: command=self.get_templates
,其中将templates_list_cb['values']
设置为列表对象templates_list
。
如果我打印templates_list
我得到: ['Template1', 'Template2']
。
我的问题是,当我单击按钮时,ComboBox 内部没有任何变化。
您必须明确配置组合框。 它不会自动发生。
def get_templates(self):
...
self.templates_list_cb.configure(values=templates_list)
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.