### CTk Class Initialization and Basic Usage Source: https://customtkinter.tomschimansky.com/documentation/windows/window The CTk class creates the main application window. It requires a single instance and a call to .mainloop() to start the application. This example demonstrates basic window setup. ```APIDOC ## CTk Class ### Description The CTk class forms the basis of any CustomTkinter program, creating the main app window. During the runtime of a program, there should only be one instance of this class with a single call of the `.mainloop()` method, which starts the app. Additional windows are created using the CTkToplevel class. ### Method Initialization ### Endpoint N/A (Class Initialization) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Arguments - **fg_color** (tuple or str) - Optional - Window background color. Can be a tuple of (light_color, dark_color) or a single color string. ### Request Example ```python import customtkinter app = customtkinter.CTk() app.geometry("600x500") app.title("CTk example") app.mainloop() ``` ### Response #### Success Response (200) N/A (This is a class initialization, not an API endpoint call) #### Response Example N/A ``` -------------------------------- ### Create and Manipulate CTkTextbox Widget Source: https://customtkinter.tomschimansky.com/documentation/widgets/textbox Demonstrates the creation of a CTkTextbox widget and its fundamental operations like inserting, getting, deleting text, and configuring its state. This example is suitable for direct use without class encapsulation. ```python import customtkinter app = customtkinter.CTk() textbox = customtkinter.CTkTextbox(app) textbox.pack(pady=20, padx=20) textbox.insert("0.0", "new text to insert") # insert at line 0 character 0 text = textbox.get("0.0", "end") # get text from line 0 character 0 till the end print(text) textbox.delete("0.0", "end") # delete all text textbox.configure(state="disabled") # configure textbox to be read-only app.mainloop() ``` -------------------------------- ### CTkOptionMenu Widget Source: https://customtkinter.tomschimansky.com/documentation/widgets/optionmenu Documentation for the CTkOptionMenu widget, including example usage, available arguments, and methods for configuration and interaction. ```APIDOC ## CTkOptionMenu Widget ### Description The CTkOptionMenu widget provides a dropdown menu for selecting options. It can be used with or without an associated StringVar to manage the selected value. ### Example Code **Without variable:** ```python def optionmenu_callback(choice): print("optionmenu dropdown clicked:", choice) optionmenu = customtkinter.CTkOptionMenu(app, values=["option 1", "option 2"], command=optionmenu_callback) optionmenu.set("option 2") ``` **With variable:** ```python def optionmenu_callback(choice): print("optionmenu dropdown clicked:", choice) optionmenu_var = customtkinter.StringVar(value="option 2") optionmenu = customtkinter.CTkOptionMenu(app,values=["option 1", "option 2"], command=optionmenu_callback, variable=optionmenu_var) ``` ### Arguments | Argument | Value | |---|---| | master | root, frame, top-level | | width | box width in px | | height | box height in px | | corner_radius | corner radius in px | | fg_color | foreground (inside) color, tuple: (light_color, dark_color) or single color | | button_color | right button color, tuple: (light_color, dark_color) or single color | | button_hover_color | hover color, tuple: (light_color, dark_color) or single color | | dropdown_fg_color | dropdown fg color, tuple: (light_color, dark_color) or single color | | dropdown_hover_color | dropdown button hover color, tuple: (light_color, dark_color) or single color | | dropdown_text_color | dropdown text color, tuple: (light_color, dark_color) or single color | | text_color | text color, tuple: (light_color, dark_color) or single color | | text_color_disabled | text color when disabled, tuple: (light_color, dark_color) or single color | | font | button text font, tuple: (font_name, size) | | dropdown_font | button text font, tuple: (font_name, size) | | hover | enable/disable hover effect: True, False | | state | "normal" (standard) or "disabled" (not clickable, darker color) | | command | function will be called when the dropdown is clicked manually | | variable | StringVar to control or get the current text | | values | list of strings with values that appear in the option menu dropdown | | dynamic_resizing | enable/disable automatic resizing of optionmenu when text is too big to fit: True (standard), False | | anchor | "n", "s", "e", "w", "center", orientation of the text inside the optionmenu, default is "w" | ### Methods * #### `.configure(attribute=value, ...)` All attributes can be configured and updated. ```python optionmenu.configure(values=["new value 1", "new value 2"]) ``` * #### `.cget(attribute_name)` Pass attribute name as string and get current value of attribute, for example. ```python state = optionmenu.cget("state") ``` * #### `.set(value)` Set optionmenu to specific string value. Value don't has to be part of the values list. * #### `.get()` Get current string value of optionmenu. ``` -------------------------------- ### Create and Use CTkTabview (Python) Source: https://customtkinter.tomschimansky.com/documentation/widgets/tabview Demonstrates how to create a CTkTabview widget, add tabs, set the visible tab, and place other widgets within a specific tab. This example shows a basic implementation without using classes. ```Python import customtkinter app = customtkinter.CTk() tabview = customtkinter.CTkTabview(master=app) tabview.pack(padx=20, pady=20) tabview.add("tab 1") # add tab at the end tabview.add("tab 2") # add tab at the end tabview.set("tab 2") # set currently visible tab button = customtkinter.CTkButton(master=tabview.tab("tab 1")) button.pack(padx=20, pady=20) app.mainloop() ``` -------------------------------- ### Create and Get Input from CTkInputDialog Source: https://customtkinter.tomschimansky.com/documentation/windows/inputdialog Demonstrates how to create a CTkInputDialog instance with custom text and title, and then retrieve the user's input. The get_input() method pauses execution until the user interacts with the dialog. ```python import customtkinter dialog = customtkinter.CTkInputDialog(text="Type in a number:", title="Test") text = dialog.get_input() # waits for input ``` -------------------------------- ### Configure CTkProgressBar Mode Source: https://customtkinter.tomschimansky.com/documentation/widgets/progressbar Allows dynamic configuration of CTkProgressBar attributes after initialization. This example demonstrates changing the progress bar's mode to 'indeterminate' for displaying unknown progress. ```python progressbar.configure(mode="indeterminate") ``` -------------------------------- ### Integrate CTkTextbox in a Full-Screen App Class Source: https://customtkinter.tomschimansky.com/documentation/widgets/textbox Shows how to integrate the CTkTextbox widget within a class-based application structure, making it fill the entire window. This example highlights grid configuration for responsive layout. ```python import customtkinter class App(customtkinter.CTk): def __init__(self): super().__init__() self.title("CTkTextbox Example") self.geometry("400x300") self.grid_rowconfigure(0, weight=1) # configure grid system self.grid_columnconfigure(0, weight=1) self.textbox = customtkinter.CTkTextbox(master=self, width=400, corner_radius=0) self.textbox.grid(row=0, column=0, sticky="nsew") self.textbox.insert("0.0", "Some example text!\n" * 50) app = App() app.mainloop() ``` -------------------------------- ### Integrate CTkInputDialog into a CustomTkinter App Source: https://customtkinter.tomschimansky.com/documentation/windows/inputdialog Shows how to embed a CTkInputDialog within a larger CustomTkinter application. A button click triggers the dialog, and the retrieved input is printed to the console. This example requires the CustomTkinter library to be installed. ```python import customtkinter app = customtkinter.CTk() app.geometry("400x300") def button_click_event(): dialog = customtkinter.CTkInputDialog(text="Type in a number:", title="Test") print("Number:", dialog.get_input()) button = customtkinter.CTkButton(app, text="Open Dialog", command=button_click_event) button.pack(padx=20, pady=20) app.mainloop() ``` -------------------------------- ### Create CTkTabview Using Classes (Python) Source: https://customtkinter.tomschimansky.com/documentation/widgets/tabview Provides an example of creating a CTkTabview widget within a class structure. This approach encapsulates the tabview's creation and widget management, promoting modularity and reusability in larger applications. ```Python import customtkinter class MyTabView(customtkinter.CTkTabview): def __init__(self, master, **kwargs): super().__init__(master, **kwargs) # create tabs self.add("tab 1") self.add("tab 2") # add widgets on tabs self.label = customtkinter.CTkLabel(master=self.tab("tab 1")) self.label.grid(row=0, column=0, padx=20, pady=10) class App(customtkinter.CTk): def __init__(self): super().__init__() self.tab_view = MyTabView(master=self) self.tab_view.grid(row=0, column=0, padx=20, pady=20) app = App() app.mainloop() ``` -------------------------------- ### Get CTkOptionMenu State Source: https://customtkinter.tomschimansky.com/documentation/widgets/optionmenu Demonstrates how to retrieve the current configuration value of a specific attribute (e.g., 'state') of a CTkOptionMenu widget using the .cget() method. ```python state = optionmenu.cget("state") ``` -------------------------------- ### Create CTkTextbox with External CTkScrollbar Source: https://customtkinter.tomschimansky.com/documentation/widgets/scrollbar This example demonstrates how to create a CTkTextbox and link it with an external CTkScrollbar. It configures the scrollbar to control the vertical view of the textbox and vice versa, enabling synchronized scrolling. This setup is useful for managing large text content within a GUI. ```python import customtkinter app = customtkinter.CTk() app.grid_rowconfigure(0, weight=1) app.grid_columnconfigure(0, weight=1) # create scrollable textbox tk_textbox = customtkinter.CTkTextbox(app, activate_scrollbars=False) tk_textbox.grid(row=0, column=0, sticky="nsew") # create CTk scrollbar ctk_textbox_scrollbar = customtkinter.CTkScrollbar(app, command=tk_textbox.yview) ctk_textbox_scrollbar.grid(row=0, column=1, sticky="ns") # connect textbox scroll event to CTk scrollbar tk_textbox.configure(yscrollcommand=ctk_textbox_scrollbar.set) app.mainloop() ``` -------------------------------- ### Start CTkProgressBar Automatic Progress Source: https://customtkinter.tomschimansky.com/documentation/widgets/progressbar Starts the automatic progress animation for the CTkProgressBar. The speed of the animation is controlled by `indeterminate_speed` or `determinate_speed` attributes. In indeterminate mode, it oscillates; in determinate mode, it fills and repeats. ```python progressbar.start() ``` -------------------------------- ### Get CTkComboBox State Source: https://customtkinter.tomschimansky.com/documentation/widgets/combobox Demonstrates how to retrieve the current state of a CTkComboBox (e.g., 'normal', 'disabled', 'readonly') using the .cget() method. ```python state = combobox.cget("state") ``` -------------------------------- ### Get Text from CTkEntry Widget Source: https://customtkinter.tomschimansky.com/documentation/widgets/entry Demonstrates how to retrieve the current text content of a CTkEntry widget using the .get() method. This is essential for accessing user input. ```python current_text = entry.get() ``` -------------------------------- ### Configure CTkCheckBox State in Python Source: https://customtkinter.tomschimansky.com/documentation/widgets/checkbox This example shows how to dynamically change the state of a CTkCheckBox widget after it has been created. The `.configure()` method allows modification of various attributes, including the 'state' attribute to disable the checkbox. ```python checkbox.configure(state="disabled") ``` -------------------------------- ### Create Main App Window with CTk Source: https://customtkinter.tomschimansky.com/documentation/windows/window This snippet demonstrates how to create the main application window using the customtkinter.CTk class. It initializes the app, sets its geometry and title, and starts the main event loop. This is the fundamental step for any CustomTkinter application. ```python import customtkinter app = customtkinter.CTk() app.geometry("600x500") app.title("CTk example") app.mainloop() ``` -------------------------------- ### Get CTkProgressBar Mode Source: https://customtkinter.tomschimansky.com/documentation/widgets/progressbar Retrieves the current value of a specified attribute of the CTkProgressBar. This example shows how to get the current 'mode' of the progress bar. ```python mode = progressbar.cget("mode") ``` -------------------------------- ### Get CTkEntry Widget State Source: https://customtkinter.tomschimansky.com/documentation/widgets/entry Illustrates how to retrieve the current state of a CTkEntry widget using the .cget() method. This allows for checking if the widget is enabled or disabled. ```python state = entry.cget("state") ``` -------------------------------- ### Get CTkComboBox Value Source: https://customtkinter.tomschimansky.com/documentation/widgets/combobox Shows how to retrieve the currently selected string value from a CTkComboBox using its .get() method. ```python current_value = combobox.get() ``` -------------------------------- ### Get CTkProgressBar Value Source: https://customtkinter.tomschimansky.com/documentation/widgets/progressbar Retrieves the current value of the CTkProgressBar. The value is a float between 0 and 1, representing the progress made. ```python value = ctk_progressbar.get() ``` -------------------------------- ### Get CTkSlider Value Source: https://customtkinter.tomschimansky.com/documentation/widgets/slider Illustrates how to retrieve the current value of a CTkSlider using the `.get()` method. This is useful for accessing the user's selection from the slider. ```python value = slider.get() ``` -------------------------------- ### Get CTkButton Attribute Value Source: https://customtkinter.tomschimansky.com/documentation/widgets/button Illustrates how to retrieve the current value of a specific attribute of a CTkButton. This is useful for inspecting the button's state or properties. ```python text = button.cget("text") ``` -------------------------------- ### Accessing a Specific Tab in CTkTabview (Python) Source: https://customtkinter.tomschimansky.com/documentation/widgets/tabview Demonstrates how to get a reference to a specific tab within a CTkTabview using the `.tab()` method. This allows you to treat the returned tab object like a CTkFrame and place widgets on it. ```Python import customtkinter app = customtkinter.CTk() tabview = customtkinter.CTkTabview(master=app) tabview.pack(padx=20, pady=20) tabview.add("my tab") button = customtkinter.CTkButton(master=tabview.tab("my tab")) button.pack(padx=20, pady=20) app.mainloop() ``` -------------------------------- ### Get CTkToplevel Window Attribute Value Source: https://customtkinter.tomschimansky.com/documentation/windows/toplevel Illustrates retrieving the current value of a CTkToplevel window's attribute, such as 'fg_color', using the .cget() method. ```python fg_color = toplevel.cget("fg_color") ``` -------------------------------- ### Structure CTkScrollableFrame within a Class Source: https://customtkinter.tomschimansky.com/documentation/widgets/scrollableframe Shows how to encapsulate a CTkScrollableFrame within a custom class, making it reusable and easier to manage within a larger application structure. This example includes adding a label widget to the frame. ```python class MyFrame(customtkinter.CTkScrollableFrame): def __init__(self, master, **kwargs): super().__init__(master, **kwargs) # add widgets onto the frame... self.label = customtkinter.CTkLabel(self) self.label.grid(row=0, column=0, padx=20) class App(customtkinter.CTk): def __init__(self): super().__init__() self.my_frame = MyFrame(master=self, width=300, height=200) self.my_frame.grid(row=0, column=0, padx=20, pady=20) app = App() app.mainloop() ``` -------------------------------- ### Set or Get CTkToplevel Window State Source: https://customtkinter.tomschimansky.com/documentation/windows/toplevel Illustrates how to set the state of a CTkToplevel window to 'normal', 'iconic', 'withdrawn', or 'zoomed' using the .state() method. If no argument is passed, it returns the current state. ```python toplevel.state(new_state) ``` -------------------------------- ### Get CTkCheckBox Text Attribute in Python Source: https://customtkinter.tomschimansky.com/documentation/widgets/checkbox This snippet illustrates how to retrieve the current value of a specific attribute of a CTkCheckBox widget using the `.cget()` method. It demonstrates fetching the 'text' attribute of the checkbox. ```python text = checkbox.cget("text") ``` -------------------------------- ### Get CTkRadioButton Attribute Value Source: https://customtkinter.tomschimansky.com/documentation/widgets/radiobutton Illustrates how to retrieve the current value of a specific attribute of a CTkRadioButton widget using the `.cget()` method. This is useful for inspecting the widget's properties, such as its current state, for debugging or conditional logic. ```python state = radiobutton.cget("state") ``` -------------------------------- ### Get CTk Window Configuration Attribute Source: https://customtkinter.tomschimansky.com/documentation/windows/window This snippet illustrates how to retrieve the current value of a specific attribute of the CTk window using the .cget() method. It's useful for inspecting the current state or configuration of window properties like 'fg_color'. ```python fg_color = app.cget("fg_color") print(f"Current foreground color: {fg_color}") ``` -------------------------------- ### Get CTkLabel Widget Attribute Value Source: https://customtkinter.tomschimansky.com/documentation/widgets/label Illustrates how to retrieve the current value of a specific attribute of a CTkLabel widget. This is useful for inspecting the widget's state or using its current properties in further logic. The .cget() method takes the attribute name as a string. ```python text = label.cget("text") ``` -------------------------------- ### Set or Get CTk Window State Source: https://customtkinter.tomschimansky.com/documentation/windows/window This snippet demonstrates how to set the state of the CTk window to predefined values like 'normal', 'iconic', 'withdrawn', or 'zoomed' using the .state() method. If no argument is passed, it returns the current state. ```python app.state('zoomed') # Maximize the window Current_state = app.state() # Get current state ``` -------------------------------- ### Get Font Metrics with CTkFont (CustomTkinter) Source: https://customtkinter.tomschimansky.com/documentation/utility-classes/font Retrieves font metrics using the .metrics() method of a CTkFont object. Calling .metrics() without arguments returns a dictionary of all available metrics (ascent, descent, fixed, linespace). Specific metrics can be accessed by passing their name as an argument. ```python import customtkinter app = customtkinter.CTk() my_font = customtkinter.CTkFont(family="Times New Roman", size=12) # Get all metrics all_metrics = my_font.metrics() print("All Font Metrics:", all_metrics) # Get a specific metric ascent_height = my_font.metrics("ascent") print(f"Ascent height: {ascent_height}") linespace_height = my_font.metrics("linespace") print(f"Linespace height: {linespace_height}") app.mainloop() ``` -------------------------------- ### Initialize and Use CTkSwitch in Python Source: https://customtkinter.tomschimansky.com/documentation/widgets/switch Demonstrates how to create a CTkSwitch widget, define an event handler for toggling, and associate a variable to track its state. This code requires the 'customtkinter' library. ```python def switch_event(): print("switch toggled, current value:", switch_var.get()) switch_var = customtkinter.StringVar(value="on") switch = customtkinter.CTkSwitch(app, text="CTkSwitch", command=switch_event, variable=switch_var, onvalue="on", offvalue="off") ``` -------------------------------- ### Create CTkLabel Widget Source: https://customtkinter.tomschimansky.com/documentation/widgets/label Demonstrates the basic creation of a CTkLabel widget. It initializes a label with specified text and foreground color, suitable for displaying static text in a GUI. This requires the customtkinter library. ```python label = customtkinter.CTkLabel(app, text="CTkLabel", fg_color="transparent") ``` -------------------------------- ### Create a CTkFrame Instance Source: https://customtkinter.tomschimansky.com/documentation/widgets/frame Demonstrates the basic instantiation of a CTkFrame widget. This requires the 'customtkinter' library to be imported. The frame is created with specified master, width, and height. ```python import customtkinter root_tk = customtkinter.CTk() frame = customtkinter.CTkFrame(master=root_tk, width=200, height=200) frame.pack() ``` -------------------------------- ### CTk Class Configuration and Methods Source: https://customtkinter.tomschimansky.com/documentation/windows/window Demonstrates how to configure and interact with the CTk window object using its various methods for title, geometry, size constraints, and window state management. ```APIDOC ## CTk Class Methods ### Description This section details the methods available for configuring and controlling the main application window created by the CTk class. ### Methods #### `.configure(attribute=value, ...)` Configures and updates attributes of the CTk window. *Example:* ```python app.configure(fg_color="#333333") ``` #### `.cget(attribute_name)` Retrieves the current value of a specified attribute. *Example:* ```python current_fg_color = app.cget("fg_color") ``` #### `.title(string)` Sets the title of the window. *Example:* ```python app.title("My Custom App") ``` #### `.geometry(geometry_string)` Sets the window's size and position. Format: `"x"` or `"x++"`. *Example:* ```python app.geometry("800x600+100+50") ``` #### `.minsize(width, height)` Sets the minimum allowable dimensions for the window. *Example:* ```python app.minsize(400, 300) ``` #### `.maxsize(width, height)` Sets the maximum allowable dimensions for the window. *Example:* ```python app.maxsize(1200, 900) ``` #### `.resizable(width, height)` Determines if the window's width and/or height can be resized by the user. Accepts boolean values. *Example:* ```python app.resizable(True, False) # Width resizable, height not ``` #### `.after(milliseconds, command)` Schedules a command to be executed after a specified delay without blocking the main loop. *Example:* ```python def show_message(): print("Delayed message!") app.after(2000, show_message) # Execute show_message after 2 seconds ``` #### `.withdraw()` Hides the window and its icon. Can be restored using `.deiconify()`. *Example:* ```python app.withdraw() ``` #### `.iconify()` Minimizes the window to the taskbar. Can be restored using `.deiconify()`. *Example:* ```python app.iconify() ``` #### `.deiconify()` Restores a withdrawn or iconified window to its normal state. *Example:* ```python app.deiconify() ``` #### `.state(new_state)` Sets the window's state ('normal', 'iconic', 'withdrawn', 'zoomed'). If no argument is passed, it returns the current state. *Example:* ```python current_state = app.state() app.state("zoomed") ``` ### Parameters Refer to individual method descriptions above for specific parameters. ### Request Example N/A (Class Methods) ### Response #### Success Response (200) N/A (Class Methods) #### Response Example N/A ``` -------------------------------- ### Manage Toplevel Window Creation and Focus Source: https://customtkinter.tomschimansky.com/documentation/windows/toplevel Shows how to create a toplevel window, ensuring it only opens once. If the window already exists, it brings it to the foreground. ```python class ToplevelWindow(customtkinter.CTkToplevel): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.geometry("400x300") self.label = customtkinter.CTkLabel(self, text="ToplevelWindow") self.label.pack(padx=20, pady=20) class App(customtkinter.CTk): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.geometry("500x400") self.button_1 = customtkinter.CTkButton(self, text="open toplevel", command=self.open_toplevel) self.button_1.pack(side="top", padx=20, pady=20) self.toplevel_window = None def open_toplevel(self): if self.toplevel_window is None or not self.toplevel_window.winfo_exists(): self.toplevel_window = ToplevelWindow(self) # create window if its None or destroyed else: self.toplevel_window.focus() # if window exists focus it app = App() app.mainloop() ``` -------------------------------- ### Initialize and Use CTkRadioButton Source: https://customtkinter.tomschimansky.com/documentation/widgets/radiobutton Demonstrates how to create and configure CTkRadioButton widgets. It shows how to set up a Tkinter integer variable to manage the selected value and associate a command function that prints the current radio button state when toggled. This is useful for interactive user interfaces where only one option can be selected at a time. ```python def radiobutton_event(): print("radiobutton toggled, current value:", radio_var.get()) radio_var = tkinter.IntVar(value=0) radiobutton_1 = customtkinter.CTkRadioButton(app, text="CTkRadioButton 1", command=radiobutton_event, variable= radio_var, value=1) radiobutton_2 = customtkinter.CTkRadioButton(app, text="CTkRadioButton 2", command=radiobutton_event, variable= radio_var, value=2) ``` -------------------------------- ### Initialize CTkProgressBar Source: https://customtkinter.tomschimansky.com/documentation/widgets/progressbar Initializes a CTkProgressBar widget. It can be configured with various arguments to control its appearance and behavior, such as orientation, colors, and dimensions. The master argument specifies the parent widget. ```python progressbar = customtkinter.CTkProgressBar(app, orientation="horizontal") ``` -------------------------------- ### Configure CTkOptionMenu Values Source: https://customtkinter.tomschimansky.com/documentation/widgets/optionmenu Shows how to dynamically update the list of available options in a CTkOptionMenu widget after it has been created using the .configure() method. ```python optionmenu.configure(values=["new value 1", "new value 2"]) ``` -------------------------------- ### Create Main App Window using CTk Class Inheritance Source: https://customtkinter.tomschimansky.com/documentation/windows/window This snippet shows an alternative way to create the main application window by inheriting from the customtkinter.CTk class. It defines an App class with an __init__ method to set up the window's geometry and title, and includes a basic button with a click handler. This approach is useful for organizing application logic within a class structure. ```python import customtkinter class App(customtkinter.CTk): def __init__(self): super().__init__() self.geometry("600x500") self.title("CTk example") self.button = customtkinter.CTkButton(self, command=self.button_click) self.button.grid(row=0, column=0, padx=20, pady=10) def button_click(self): print("button click") app = App() app.mainloop() ``` -------------------------------- ### Configure CTkComboBox Values Source: https://customtkinter.tomschimansky.com/documentation/widgets/combobox Shows how to dynamically change the list of available options in a CTkComboBox after it has been created using the .configure() method. ```python combobox.configure(values=["new value 1", "new value 2"]) ``` -------------------------------- ### Create CTkEntry Widget Source: https://customtkinter.tomschimansky.com/documentation/widgets/entry Demonstrates the basic creation of a CTkEntry widget with a placeholder text. This widget is part of the CustomTkinter library for creating modern-looking GUI applications. ```python entry = customtkinter.CTkEntry(app, placeholder_text="CTkEntry") ``` -------------------------------- ### Create CTkButton with Event Source: https://customtkinter.tomschimansky.com/documentation/widgets/button Demonstrates how to create a CTkButton and associate a callback function to its click event. This is a fundamental step for interactive UI elements. ```python def button_event(): print("button pressed") button = customtkinter.CTkButton(app, text="CTkButton", command=button_event) ``` -------------------------------- ### Create and Configure CTkCheckBox in Python Source: https://customtkinter.tomschimansky.com/documentation/widgets/checkbox This code snippet demonstrates how to create a CTkCheckBox widget, define an event handler for when it's toggled, and set its initial state using a StringVar. It utilizes the CustomTkinter library for modern UI elements. ```python def checkbox_event(): print("checkbox toggled, current value:", check_var.get()) check_var = customtkinter.StringVar(value="on") checkbox = customtkinter.CTkCheckBox(app, text="CTkCheckBox", command=checkbox_event, variable=check_var, onvalue="on", offvalue="off") ``` -------------------------------- ### CTkScrollbar Widget Documentation Source: https://customtkinter.tomschimansky.com/documentation/widgets/scrollbar This section provides a comprehensive overview of the CTkScrollbar widget, including its initialization arguments, configuration methods, and state retrieval/setting capabilities. ```APIDOC ## CTkScrollbar Widget ### Description A customizable scrollbar widget for use with scrollable widgets in customtkinter applications. ### Arguments - **master** (customtkinter.CTk or tkinter.Frame or customtkinter.CTkFrame) - The parent widget. - **command** (function) - A function to be called when the scrollbar is moved. Typically used to update the scrollable widget's view. - **width** (int) - The width of the scrollbar in pixels. - **height** (int) - The height of the scrollbar in pixels. - **corner_radius** (int) - The radius of the scrollbar's corners in pixels. - **border_spacing** (int) - The spacing between the scrollbar's border and its content in pixels. - **fg_color** (tuple or str or None) - The foreground color of the scrollbar. Can be a tuple of (light_color, dark_color), a single color string, or None for default. - **button_color** (tuple or str or None) - The color of the scrollbar's thumb (button). Can be a tuple of (light_color, dark_color), a single color string, or None for default. - **button_hover_color** (tuple or str or None) - The hover color of the scrollbar's thumb. Can be a tuple of (light_color, dark_color), a single color string, or None for default. - **minimum_pixel_length** (int) - The minimum pixel length of the scrollbar thumb. - **orientation** (str) - The orientation of the scrollbar, either "vertical" (default) or "horizontal". - **hover** (bool) - Enables or disables the hover effect for the scrollbar thumb. ### Methods - #### .configure(attribute=value, ...) Allows modification of widget attributes after initialization. All arguments listed in the 'Arguments' section can be configured. - #### .cget(attribute_name) Retrieves the current value of a specified attribute. - **attribute_name** (str) - The name of the attribute to retrieve. - #### .get() Retrieves the current start and end values of the scrollbar's range. - **Returns**: A tuple containing the start and end values (e.g., (0.0, 1.0)). - #### .set(start_value, end_value) Sets the start and end values of the scrollbar's range. - **start_value** (float) - The desired start value. - **end_value** (float) - The desired end value. ### Example Usage ```python import customtkinter app = customtkinter.CTk() app.grid_rowconfigure(0, weight=1) app.grid_columnconfigure(0, weight=1) # create scrollable textbox tk_textbox = customtkinter.CTkTextbox(app, activate_scrollbars=False) tk_textbox.grid(row=0, column=0, sticky="nsew") # create CTk scrollbar ctk_textbox_scrollbar = customtkinter.CTkScrollbar(app, command=tk_textbox.yview) ctk_textbox_scrollbar.grid(row=0, column=1, sticky="ns") # connect textbox scroll event to CTk scrollbar tk_textbox.configure(yscrollcommand=ctk_textbox_scrollbar.set) app.mainloop() ``` ``` -------------------------------- ### Create CTkOptionMenu Without Variable Source: https://customtkinter.tomschimansky.com/documentation/widgets/optionmenu Demonstrates creating a CTkOptionMenu widget without an associated StringVar. The 'command' callback function is executed when a new option is selected. ```python def optionmenu_callback(choice): print("optionmenu dropdown clicked:", choice) optionmenu = customtkinter.CTkOptionMenu(app, values=["option 1", "option 2"], command=optionmenu_callback) optionmenu.set("option 2") ``` -------------------------------- ### Create CTkOptionMenu With Variable Source: https://customtkinter.tomschimansky.com/documentation/widgets/optionmenu Illustrates creating a CTkOptionMenu widget linked to a customtkinter.StringVar. This allows for easier management and retrieval of the selected option's value. ```python def optionmenu_callback(choice): print("optionmenu dropdown clicked:", choice) optionmenu_var = customtkinter.StringVar(value="option 2") optionmenu = customtkinter.CTkOptionMenu(app,values=["option 1", "option 2"], command=optionmenu_callback, variable=optionmenu_var) ``` -------------------------------- ### Create CTkComboBox With Variable Source: https://customtkinter.tomschimansky.com/documentation/widgets/combobox Illustrates creating a CTkComboBox using a customtkinter.StringVar to manage its state. This allows for easier retrieval and setting of the combobox's current value. A callback function is also included. ```python def combobox_callback(choice): print("combobox dropdown clicked:", choice) combobox_var = customtkinter.StringVar(value="option 2") combobox = customtkinter.CTkComboBox(app, values=["option 1", "option 2"], command=combobox_callback, variable=combobox_var) combobox_var.set("option 2") ``` -------------------------------- ### Create and Use CTkSlider Source: https://customtkinter.tomschimansky.com/documentation/widgets/slider Demonstrates how to create a CTkSlider widget and define a callback function to handle slider value changes. The `command` argument takes a function that receives the current slider value. ```python def slider_event(value): print(value) slider = customtkinter.CTkSlider(app, from_=0, to=100, command=slider_event) ``` -------------------------------- ### Configure CTkSlider Properties Source: https://customtkinter.tomschimansky.com/documentation/widgets/slider Shows how to dynamically change the properties of an existing CTkSlider instance using the `.configure()` method. This allows for runtime adjustments to widget behavior and appearance. ```python slider.configure(number_of_steps=25) ``` -------------------------------- ### Configure CTkTextbox Widget Attributes Source: https://customtkinter.tomschimansky.com/documentation/widgets/textbox Illustrates how to dynamically change the properties of an existing CTkTextbox widget after its creation using the .configure() method. This allows for runtime adjustments to appearance and behavior. ```python textbox.configure(state="normal", text_color="blue", wrap="word") ``` -------------------------------- ### Set CTkToplevel Window Minimum Size Source: https://customtkinter.tomschimansky.com/documentation/windows/toplevel Demonstrates setting the minimum allowable dimensions for a CTkToplevel window using the .minsize() method. ```python toplevel.minsize(width, height) ``` -------------------------------- ### Create CTkImage Object with Light and Dark Mode Images Source: https://customtkinter.tomschimansky.com/documentation/utility-classes/image This snippet demonstrates how to create a CTkImage object using PIL Image objects for both light and dark modes. It also shows how to display the CTkImage within a CTkLabel. Ensure that the provided PIL Images have a higher resolution than the specified size for optimal rendering on high-resolution displays. ```python from PIL import Image import customtkinter # Assuming 'app' is your main customtkinter window or frame app = customtkinter.CTk() # Create a CTkImage object with separate images for light and dark modes my_image = customtkinter.CTkImage(light_image=Image.open(""), dark_image=Image.open(""), size=(30, 30)) # Display the CTkImage using a CTkLabel image_label = customtkinter.CTkLabel(app, image=my_image, text="") image_label.pack() app.mainloop() ``` -------------------------------- ### Initialize CTkScrollableFrame Source: https://customtkinter.tomschimansky.com/documentation/widgets/scrollableframe Demonstrates the basic initialization of a CTkScrollableFrame widget. This is the fundamental step to create a scrollable area within a CustomTkinter application. ```python scrollable_frame = customtkinter.CTkScrollableFrame(app, width=200, height=200) ``` -------------------------------- ### Configure CTkButton Attributes Source: https://customtkinter.tomschimansky.com/documentation/widgets/button Shows how to dynamically change the properties of an existing CTkButton after it has been created. This allows for updating the button's appearance or behavior. ```python button.configure(text="new text") ``` -------------------------------- ### Load Custom Color Theme in CustomTkinter Source: https://customtkinter.tomschimansky.com/documentation/color Explains how to load a custom color theme by providing the path to a JSON file. This allows for more granular control over widget styling without manually setting each widget's properties. The JSON file should define colors for different widget states and appearance modes. ```python import customtkinter # Load a custom theme from a JSON file customtkinter.set_default_color_theme("path/to/your/custom_theme.json") # The rest of your application code follows... ``` -------------------------------- ### Configure CTk Window Background Color Source: https://customtkinter.tomschimansky.com/documentation/windows/window This snippet demonstrates how to dynamically configure the background color (fg_color) of the main CTk window after it has been created. It shows the usage of the .configure() method to change the window's appearance. The fg_color can be a single color string or a tuple for light/dark mode. ```python new_fg_color = "#FF0000" # Example new color app.configure(fg_color=new_fg_color) ``` -------------------------------- ### Create a CTkToplevel Window Source: https://customtkinter.tomschimansky.com/documentation/windows/toplevel Demonstrates the basic creation of a CTkToplevel window. This class is used for additional windows and does not require a separate mainloop call. ```python toplevel = CTkToplevel(app) # master argument is optional ``` -------------------------------- ### Set CTk Window Geometry Source: https://customtkinter.tomschimansky.com/documentation/windows/window This snippet demonstrates how to set the initial size and position of the CTk window using the .geometry() method. The format string specifies width, height, and optional X/Y coordinates for placement. ```python app.geometry("800x600+100+50") # Width x Height + X_pos + Y_pos ``` -------------------------------- ### Bind Events to CTkLabel Widget Source: https://customtkinter.tomschimansky.com/documentation/widgets/label Explains how to associate specific events (like clicks or key presses) with actions for a CTkLabel widget. This enables interactive behavior for the label. The .bind() method takes the event sequence and a command function as arguments. ```python label.bind(sequence=None, command=None, add=None) ``` -------------------------------- ### Create CTkComboBox Without Variable Source: https://customtkinter.tomschimansky.com/documentation/widgets/combobox Demonstrates how to create a CTkComboBox without using a StringVar. A callback function is provided to handle selection changes. The combobox is initialized with a list of values and a default selection. ```python def combobox_callback(choice): print("combobox dropdown clicked:", choice) combobox = customtkinter.CTkComboBox(app, values=["option 1", "option 2"], command=combobox_callback) combobox.set("option 2") ``` -------------------------------- ### Schedule Command Execution in CTkToplevel Source: https://customtkinter.tomschimansky.com/documentation/windows/toplevel Shows how to execute a command after a specified delay without blocking the main application loop using the .after() method. ```python toplevel.after(milliseconds, command) ``` -------------------------------- ### Create CTkSegmentedButton with a variable (Python) Source: https://customtkinter.tomschimansky.com/documentation/widgets/segmentedbutton Shows how to create a CTkSegmentedButton using a StringVar to manage the selected value. This allows for easier tracking and modification of the button's state. The StringVar is linked to the button, and a callback function is provided. ```python def segmented_button_callback(value): print("segmented button clicked:", value) segemented_button_var = customtkinter.StringVar(value="Value 1") segemented_button = customtkinter.CTkSegmentedButton(app, values=["Value 1", "Value 2", "Value 3"], command=segmented_button_callback, variable=segemented_button_var) ``` -------------------------------- ### Configure CTkEntry Widget State Source: https://customtkinter.tomschimansky.com/documentation/widgets/entry Shows how to change the state of a CTkEntry widget to 'disabled' using the .configure() method. This is useful for temporarily preventing user input. ```python entry.configure(state="disabled") ``` -------------------------------- ### Insert Text into CTkEntry Widget Source: https://customtkinter.tomschimansky.com/documentation/widgets/entry Shows how to insert text into a CTkEntry widget at a specific index using the .insert() method. This can be used for pre-filling fields or programmatically adding text. ```python entry.insert(0, "Initial Text") ``` -------------------------------- ### Configure CTkRadioButton State Source: https://customtkinter.tomschimansky.com/documentation/widgets/radiobutton Shows how to dynamically change the state of a CTkRadioButton widget after it has been created. The `.configure()` method allows modification of various attributes, including setting the state to 'disabled' to make the radio button unclickable and visually distinct. ```python radiobutton.configure(state="disabled") ``` -------------------------------- ### Create and Configure CTkFont Object (CustomTkinter) Source: https://customtkinter.tomschimansky.com/documentation/utility-classes/font Demonstrates creating a CTkFont object for a CustomTkinter CTkButton, allowing for subsequent configuration. This is the recommended approach as CTkFont objects can be modified after creation and reused across multiple widgets. The font can be configured using the .configure() method. ```python import customtkinter app = customtkinter.CTk() # Example: Creating and configuring a CTkFont object my_font = customtkinter.CTkFont(family="Times New Roman", size=14, slant="italic") button = customtkinter.CTkButton(app, font=my_font) button.pack() # Configure font afterwards new_size = 18 button.cget("font").configure(size=new_size) app.mainloop() ``` -------------------------------- ### Set CTkToplevel Window Maximum Size Source: https://customtkinter.tomschimansky.com/documentation/windows/toplevel Illustrates setting the maximum allowable dimensions for a CTkToplevel window using the .maxsize() method. ```python toplevel.maxsize(width, height) ``` -------------------------------- ### Set CTk Window Minimum Size Source: https://customtkinter.tomschimansky.com/documentation/windows/window This snippet shows how to define the minimum allowable dimensions for the CTk window using the .minsize() method. This prevents the window from becoming too small to be usable. ```python app.minsize(width=400, height=300) ``` -------------------------------- ### Configure CTkLabel Widget Attributes Source: https://customtkinter.tomschimansky.com/documentation/widgets/label Shows how to dynamically change the properties of an existing CTkLabel widget after its creation. This method allows for updating attributes like text, color, or font at runtime. It utilizes the .configure() method inherited from Tkinter widgets. ```python label.configure(text="new text") ``` -------------------------------- ### Invoke CTkButton Command Source: https://customtkinter.tomschimansky.com/documentation/widgets/button Explains how to programmatically trigger the command associated with a CTkButton, even if the button's state is currently 'disabled'. This can be used for specific control flows. ```python button.invoke() ``` -------------------------------- ### Save CTkTabview Tabs in Variables (Python) Source: https://customtkinter.tomschimansky.com/documentation/widgets/tabview Illustrates how to save references to individual tabs created within a CTkTabview into separate variables. This allows for easier manipulation and widget placement within those specific tabs. ```Python import customtkinter app = customtkinter.CTk() tabview = customtkinter.CTkTabview(master=app) tabview.pack(padx=20, pady=20) tab_1 = tabview.add("tab 1") tab_2 = tabview.add("tab 2") button = customtkinter.CTkButton(tab_1) button.pack(padx=20, pady=20) app.mainloop() ``` -------------------------------- ### Schedule Command Execution in CTk Source: https://customtkinter.tomschimansky.com/documentation/windows/window This snippet demonstrates the use of the .after() method to schedule a function to be executed after a specified delay in milliseconds, without blocking the main application loop. This is useful for timed events or animations. ```python def my_function(): print("This message appears after 2000ms") app.after(2000, my_function) # Execute my_function after 2 seconds ``` -------------------------------- ### Set CTkToplevel Window Geometry Source: https://customtkinter.tomschimansky.com/documentation/windows/toplevel Shows how to set the size and position of a CTkToplevel window using the .geometry() method with a string format like 'x++'. ```python self.geometry("400x300") ``` -------------------------------- ### Create CTkSegmentedButton without a variable (Python) Source: https://customtkinter.tomschimansky.com/documentation/widgets/segmentedbutton Demonstrates how to create a CTkSegmentedButton without using a StringVar. A callback function is defined to handle button clicks and print the selected value. The button is initialized with a list of string values and a command. ```python def segmented_button_callback(value): print("segmented button clicked:", value) segemented_button = customtkinter.CTkSegmentedButton(app, values=["Value 1", "Value 2", "Value 3"], command=segmented_button_callback) segemented_button.set("Value 1") ``` -------------------------------- ### Control CTkToplevel Window Resizability Source: https://customtkinter.tomschimansky.com/documentation/windows/toplevel Explains how to define whether the width and/or height of a CTkToplevel window can be resized by the user using boolean values in the .resizable() method. ```python toplevel.resizable(width, height) ``` -------------------------------- ### Control CTk Window Resizability Source: https://customtkinter.tomschimansky.com/documentation/windows/window This snippet shows how to control whether the width and/or height of the CTk window can be resized by the user using the .resizable() method. It accepts boolean values for width and height. ```python app.resizable(width=True, height=False) # Only width is resizable ``` -------------------------------- ### Minimize CTk Window Source: https://customtkinter.tomschimansky.com/documentation/windows/window This snippet demonstrates how to minimize the CTk window to the taskbar using the .iconify() method. The window can be restored using .deiconify(). ```python app.iconify() ``` -------------------------------- ### Deiconify CTkToplevel Window Source: https://customtkinter.tomschimansky.com/documentation/windows/toplevel Shows how to restore a hidden or iconified CTkToplevel window to its normal state using the .deiconify() method. ```python toplevel.deiconify() ``` -------------------------------- ### CTkScrollableFrame Filling App Window Source: https://customtkinter.tomschimansky.com/documentation/widgets/scrollableframe Illustrates how to configure a CTkScrollableFrame to completely fill the application window using grid layout options. This is useful for creating responsive UIs where the scrollable area should adapt to window size changes. ```python class MyFrame(customtkinter.CTkScrollableFrame): def __init__(self, master, **kwargs): super().__init__(master, **kwargs) # add widgets onto the frame... self.label = customtkinter.CTkLabel(self) self.label.grid(row=0, column=0, padx=20) class App(customtkinter.CTk): def __init__(self): super().__init__() self.grid_rowconfigure(0, weight=1) self.grid_columnconfigure(0, weight=1) self.my_frame = MyFrame(master=self, width=300, height=200, corner_radius=0, fg_color="transparent") self.my_frame.grid(row=0, column=0, sticky="nsew") app = App() app.mainloop() ``` -------------------------------- ### Configure CTkToplevel Window Background Color Source: https://customtkinter.tomschimansky.com/documentation/windows/toplevel Demonstrates how to change the background color of a CTkToplevel window using the .configure() method. The 'fg_color' attribute accepts a single color or a tuple for light/dark modes. ```python toplevel.configure(fg_color="red") ``` -------------------------------- ### Restore CTk Window Source: https://customtkinter.tomschimansky.com/documentation/windows/window This snippet shows how to restore a withdrawn or iconified CTk window to its normal state using the .deiconify() method. This is the counterpart to .withdraw() and .iconify(). ```python app.deiconify() ``` -------------------------------- ### Iconify CTkToplevel Window Source: https://customtkinter.tomschimansky.com/documentation/windows/toplevel Explains how to minimize a CTkToplevel window to the taskbar using the .iconify() method. It can be restored with .deiconify(). ```python toplevel.iconify() ``` -------------------------------- ### Step CTkProgressBar Manually Source: https://customtkinter.tomschimansky.com/documentation/widgets/progressbar Advances the CTkProgressBar by a single step. This is typically used when not employing the automatic `.start()` and `.stop()` methods. The step size is determined by the `determinate_speed` and `indeterminate_speed` attributes. ```python progressbar.step() ``` -------------------------------- ### Set CTk Window Maximum Size Source: https://customtkinter.tomschimansky.com/documentation/windows/window This snippet demonstrates how to set the maximum allowable dimensions for the CTk window using the .maxsize() method. This prevents the window from becoming excessively large. ```python app.maxsize(width=1200, height=900) ```