You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Custom Tkinter点击按钮后无法为ComboBox填充列表问题求助

Custom Tkinter ComboBox 点击按钮后不更新选项的解决办法

问题出在直接通过字典索引赋值values后,CTkComboBox没有自动刷新。解决这个问题只需要改用组件的configure方法来更新选项即可,这是Custom Tkinter官方推荐的组件属性修改方式。

修改你的get_templates方法,把:

self.templates_list_cb['values'] = templates_list

替换成:

self.templates_list_cb.configure(values=templates_list)

或者也可以在原赋值语句后加上强制刷新的代码:

self.templates_list_cb['values'] = templates_list
self.templates_list_cb.update()

修改后的完整代码如下:

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)
        # 改用configure方法更新选项
        self.templates_list_cb.configure(values=templates_list)
        return templates_list

if __name__ == "__main__":
    app = App()
    app.mainloop()

这样修改后,点击按钮就能看到ComboBox里正确加载模板列表了。

内容的提问来源于stack exchange,提问作者Daniel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 06:25:06