Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading