簡體   English   中英

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

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM