Home > Blockchain >  Tkinter - How do I populate a combo box with a list?
Tkinter - How do I populate a combo box with a list?

Time:12-16

I'm actually using Custom Tkinter but I think it should be the same.

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.

If I print templates_list I get: ['Template1', 'Template2'].

My issue is that when I click on the button, nothing changes inside of the ComboBox.

CodePudding user response:

You have to explicitly configure the combobox. It won't happen automatically.

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