The CTkComboBox is a widget that allows users to select an option from a dropdown menu. It is customizable and supports the use of variables to control or retrieve the selected value.
Example without a Variable
import customtkinter
def combobox_callback(choice):
print("Combobox dropdown clicked:", choice)
# Initial setup
app = customtkinter.CTk()
app.geometry("400x300")
# Creating a ComboBox
combobox = customtkinter.CTkComboBox(app, values=["option 1", "option 2"],
command=combobox_callback)
combobox.pack(pady=20)
combobox.set("option 2") # Sets the initial value
app.mainloop()
Example with a Variable
import customtkinter
def combobox_callback(choice):
print("Combobox dropdown clicked:", choice)
# Initial setup
app = customtkinter.CTk()
app.geometry("400x300")
# Creating a StringVar variable
combobox_var = customtkinter.StringVar(value="option 2")
# Creating a ComboBox linked to the variable
combobox = customtkinter.CTkComboBox(app, values=["option 1", "option 2"],
command=combobox_callback, variable=combobox_var)
combobox.pack(pady=20)
# Changing the variable value
combobox_var.set("option 1")
app.mainloop()
CTkComboBox Arguments
Argument | Description |
---|---|
master |
Required parameter: root, frame, or TopLevel. |
width |
Width of the ComboBox in pixels. |
height |
Height of the ComboBox in pixels. |
corner_radius |
Corner radius in pixels. |
border_width |
Border width in pixels. |
fg_color |
Main background color (single color or tuple: (light_color, dark_color)). |
border_color |
Border color (single color or tuple: (light_color, dark_color)). |
button_color |
Right-side button color (single color or tuple: (light_color, dark_color)). |
button_hover_color |
Button hover color (single color or tuple: (light_color, dark_color)). |
dropdown_fg_color |
Dropdown menu background color (single color or tuple: (light_color, dark_color)). |
dropdown_hover_color |
Hover color for dropdown menu options. |
dropdown_text_color |
Text color in the dropdown menu (single color or tuple: (light_color, dark_color)). |
text_color |
Text color in the ComboBox (single color or tuple: (light_color, dark_color)). |
text_color_disabled |
Text color when disabled. |
font |
Font of the text (e.g., (font_name, size)). |
dropdown_font |
Font for the text in the dropdown menu. |
values |
List of strings displayed in the dropdown menu. |
hover |
Enables/disables hover effect. Default: True . |
state |
Sets the state: "normal", "disabled", or "readonly". |
command |
Function called when an option is selected manually. |
variable |
StringVar object to control or retrieve the current value. |
justify |
Text alignment: "right", "left", "center". Default: "left". |
CTkComboBox Methods
Method | Description |
.configure() |
Updates the ComboBox properties (e.g., combobox.configure(values=["new value 1", "new value 2"]) ). |
.cget(attribute) |
Retrieves the current value of an attribute (e.g., state = combobox.cget("state") ). |
.set(value) |
Sets the current value of the ComboBox. The value does not need to be in the values list. |
.get() |
Retrieves the current value of the ComboBox. |
With these examples and information, you can create highly customized dropdown menus using CTkComboBox in CustomTkinter!
Copyright Statement: Unless stated otherwise, all articles are original to this site, please credit the source when sharing.
Article link:http://pybeginners.com/customtkinter/ctkcombobox-in-customtkinter/
License Agreement:Attribution-NonCommercial 4.0 International License