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

CTkComboBox大列表下拉菜单自动关闭问题求助

CustomTkinter CTkComboBox下拉菜单触底自动关闭的解决方案

问题描述

使用CustomTkinter开发GUI时,CTkComboBox组件在填充大量选项后出现异常:当下拉菜单触碰到屏幕底部时,点击展开后会立即自动收起,仅按住鼠标左键时才能保持菜单打开;若下拉菜单未触碰到屏幕底部则功能正常。

环境信息:

  • Python 3.12.6
  • Ubuntu 22.04.5 LTS

复现代码

import customtkinter as ctk

# List of values for the ComboBox
options = ['alpha_1', 'alpha_2', 'alpha_3', 'A_1', 'A_2', 'A_3', 'y_1', 'y_2', 'y_2p', 'y_3', 
           'v_Phi', 'cutoff', 'M [GeV]', 'm_y', 're_tau_charged_leptons', 'im_tau_charged_leptons',
           're_tau_neutrinos', 'im_tau_neutrinos', 'm_electron [GeV]', 'm_muon [GeV]', 'm_tau [GeV]',
           'chi_electron', 'chi_muon', 'chi_tau', 'chi_charge_lepton_sector', 'm_h [GeV]', 
           'Dm_21 [eV^2]', 'Dm_31 [eV^2]', 'chi_Dm21', 'chi_Dm31', 'mv_1 [eV]', 'mv_2 [eV]', 
           'mv_3 [eV]', 'mN_1 [GeV]', 'mN_2 [GeV]', 'mN_3 [GeV]', 'Sigma [eV]', 'Jarlskog_invariant',
           'PMNSphase', 'PMNSs12_SQ', 'PMNSs23_SQ', 'PMNSs13_SQ', 'chi_PMNSs12SQ', 'chi_PMNSs23SQ', 
           'chi_PMNSs13SQ', 'chi_PMNSphase', '|VPMNS_11|', '|VPMNS_12|', '|VPMNS_13|', '|VPMNS_21|', 
           '|VPMNS_22|', '|VPMNS_23|', '|VPMNS_31|', '|VPMNS_32|', '|VPMNS_33|', 'meff_ve [eV]', 
           'meff_vmu [eV]', 'meff_vtau [eV]', 'm_ee [eV]', 'chi_neutrino_sector', 'BR_muTOegamma']

# Create the application window
class App(ctk.CTk):
    def __init__(self):
        super().__init__()

        self.title("CustomTkinter ComboBox Example")
        self.geometry("400x300")

        # Create a CTkComboBox with the provided list of values
        self.combobox = ctk.CTkComboBox(self, values=options)
        self.combobox.grid(row=0, column=0, padx=20, pady=20)

        # Optionally, set an initial value
        self.combobox.set("Select an option")

# Run the application
if __name__ == "__main__":
    app = App()
    app.mainloop()

问题原因

该异常是由于CTkComboBox默认未限制下拉菜单的高度,当选项过多导致下拉菜单触碰到屏幕底部时,组件的事件处理逻辑出现冲突,触发了菜单自动关闭的行为。

解决方案

方案1:设置下拉菜单最大高度(推荐)

CustomTkinter的CTkComboBox组件支持dropdown_max_height参数,通过设置该参数限制下拉菜单的最大高度,当选项数量超出高度限制时,下拉菜单会自动添加垂直滚动条,避免触碰到屏幕底部,从而解决自动关闭问题。

修改后的初始化代码:

self.combobox = ctk.CTkComboBox(self, values=options, dropdown_max_height=300)
  • 参数值可根据实际需求调整(如250、350),确保下拉菜单不会超出屏幕范围。
  • 设置后,下拉菜单会保持打开状态,可通过滚动条浏览所有选项,正常选择。

方案2:自定义带滚动的下拉框(兼容旧版本)

若使用的CustomTkinter版本不支持dropdown_max_height参数,可通过CTkScrollableFrame自定义下拉菜单:

import customtkinter as ctk

class CustomComboBox(ctk.CTkFrame):
    def __init__(self, master, values, **kwargs):
        super().__init__(master, **kwargs)
        self.values = values
        self.selected_value = ctk.StringVar(value="Select an option")
        
        # 顶部输入框
        self.entry = ctk.CTkEntry(self, textvariable=self.selected_value, state="readonly")
        self.entry.grid(row=0, column=0, sticky="ew")
        self.entry.bind("<Button-1>", self.toggle_dropdown)
        
        # 下拉滚动框(初始隐藏)
        self.dropdown_frame = ctk.CTkScrollableFrame(self, height=300)
        self.dropdown_frame.grid(row=1, column=0, sticky="ew")
        self.dropdown_frame.grid_remove()
        
        # 添加选项按钮
        for value in values:
            btn = ctk.CTkButton(self.dropdown_frame, text=value, command=lambda v=value: self.select_value(v))
            btn.pack(fill="x", padx=2, pady=1)
    
    def toggle_dropdown(self, event):
        if self.dropdown_frame.winfo_ismapped():
            self.dropdown_frame.grid_remove()
        else:
            self.dropdown_frame.grid()
    
    def select_value(self, value):
        self.selected_value.set(value)
        self.dropdown_frame.grid_remove()

# 使用自定义组件
class App(ctk.CTk):
    def __init__(self):
        super().__init__()
        self.title("Custom ComboBox Example")
        self.geometry("400x300")
        
        self.combobox = CustomComboBox(self, values=options)
        self.combobox.grid(row=0, column=0, padx=20, pady=20, sticky="ew")

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

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 04:57:15