CTkListbox

repository·main·Indexed 42 days ago

https://github.com/akascape/ctklistbox

A modern listbox widget designed for the customtkinter library, providing functionality similar to the standard tkinter listbox. It includes support for multiple selection, custom styling for hover and highlight colors, and methods for manipulating list options such as .insert(), .get(), and .delete().

Tokens
854
Snippets
2
Records
4
Agent score
3%

What's inside CTkListbox

  1. Basic usage of CTkListbox

    main

    To use CTkListbox, import it from CTkListbox, initialize it with a parent widget, and use .insert(index, option) to populate it. You can pass a command function to the constructor to handle selection events.

    import customtkinter
    from CTkListbox import *
    
    def show_value(selected_option):
        print(selected_option)
        
    root = customtkinter.CTk()
    
    listbox = CTkListbox(root, command=show_value)
    listbox.pack(fill="both", expand=True, padx=10, pady=10)
    
    listbox.insert(0, "Option 0")
    listbox.insert(1, "Option 1")
    listbox.insert(2, "Option 2")
    listbox.insert(3, "Option 3")
    listbox.insert(4, "Option 4")
    listbox.insert(5, "Option 5")
    listbox.insert(6, "Option 6")
    listbox.insert(7, "Option 7")
    listbox.insert("END", "Option 8")
    
    root.mainloop()
  2. Use CTkListbox methods

    main

    The following methods are available to manipulate the CTkListbox instance:

    • .insert(index, option): add new option to the listbox
    • .get(): get the selected option(s)
    • .delete(index): delete any option from the listbox. Use .delete("all") to delete all options
    • .size(): get the size of the listbox
    • .activate(index): activate any option
    • .deactivate(index): deactivate any option
    • .curselection(): returns indexes of selected options
    • .configure(): change some parameters for the listbox
    • .see(index): move the listbox scrollbar to the index place
    • .move_up(index): reorder option up in the listbox
    • .move_down(index): reorder option down in the listbox
  3. Configure CTkListbox arguments

    main

    When initializing CTkListbox, you can use the following arguments to customize its appearance and behavior:

    ParameterDescription
    masterparent widget
    width(optional) set width of the listbox
    height(optional) set height of the listbox
    fg_colorforeground color of the listbox
    border_colorborder color of the listbox frame
    border_widthwidth of the border frame
    text_colorset the color of the option text
    hoverEnable/Disable hover effect (bool)
    hover_colorset hover color of the options
    button_colorset color of unselected buttons
    highlight_colorset the selected color of the option
    fontset font of the option text (tuple or CTkFont)
    wraplengthchange the number of letter per option line
    justifychange the text anchor; left, right or center
    commandcalls a command when a option is selected
    multiple_selectionselect multiple options in the listbox, default=False
    listvariableuse a tkinter variable to change the listbox content
    *other_parametersall other parameters of ctk_scrollable frame can be passed