简体   繁体   English

Tkinter - 如何用列表填充组合框?

[英]Tkinter - How do I populate a combo box with a list?

I'm actually using Custom Tkinter but I think it should be the same.我实际上正在使用 Custom Tkinter,但我认为它应该是一样的。

I would like to populate a ComboBox after clicking on a button.我想在单击按钮后填充一个组合框。 My button calls a function, which returns a list.我的按钮调用一个函数,该函数返回一个列表。 I would like to populate the Combobox with each item in that list but I can't seem to get the hang of it.我想用该列表中的每个项目填充组合框,但我似乎无法掌握它。

Here is a snippet of the app, you can actually copy paste it and it will run:这是应用程序的一个片段,您实际上可以复制粘贴它并运行它:

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()

As I understand it: My button load_template_button executes: command=self.get_templates , which inside of it sets templates_list_cb['values'] to the list object which is templates_list .据我了解:我的按钮load_template_button执行: command=self.get_templates ,其中将templates_list_cb['values']设置为列表对象templates_list

If I print templates_list I get: ['Template1', 'Template2'] .如果我打印templates_list我得到: ['Template1', 'Template2']

My issue is that when I click on the button, nothing changes inside of the ComboBox.我的问题是,当我单击按钮时,ComboBox 内部没有任何变化。

You have to explicitly configure the combobox.您必须明确配置组合框。 It won't happen automatically.它不会自动发生。

def get_templates(self):
    ...
    self.templates_list_cb.configure(values=templates_list)
    ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM