Akascape / CTkListbox

A simple listbox for customtkinter (extenstion/add-on)
MIT License
153 stars 13 forks source link

Add feature: Option reordering #18

Closed CheemaOTB closed 11 months ago

CheemaOTB commented 1 year ago

This pull request addresses the open issue #17 by introducing a new feature: Option Reordering. Users can now easily reorder options as needed. The changes include two new methods, move_up and move_down. This enhancement aims to resolve the reported issue and improve the overall usability of the widget.

Below is an example code snippet demonstrating how the new option reordering feature can be implemented by users:

import customtkinter
from CTkListbox import *

# Create the main application window
root = customtkinter.CTk()

# Function to move the selected item up in the list
def moveup():
    index = animalBox.curselection()
    if index > 0:
        animalBox.move_up(index)
        animalList[index - 1], animalList[index] = animalList[index], animalList[index - 1]

# Function to move the selected item down in the list
def movedown():
    index = animalBox.curselection()
    if index < animalBox.size() - 1:
        animalBox.move_down(index)
        animalList[index], animalList[index + 1] = animalList[index + 1], animalList[index]

# Function to handle the selection event in the listbox
def show_value(selected_option):
    print(selected_option)

# Initial list of animals
animalList = ['Cat', 'Dog', 'Bear', 'Dolphin', 'Kangaroo']

# Create a Tkinter StringVar with the initial list
animalString = customtkinter.StringVar(value=animalList)

# Create a custom listbox using CTkListbox
animalBox = CTkListbox(root, command=show_value, listvariable=animalString)
animalBox.grid(row=1, column=0, padx=20, pady=10)

# Create buttons for moving items up and down
moveupButton = customtkinter.CTkButton(root, text="Move Up", command=moveup, width=200, height=35)
moveupButton.grid(row=2, column=0, padx=20, pady=10)

movedownButton = customtkinter.CTkButton(root, text="Move Down", command=movedown, width=200, height=35)
movedownButton.grid(row=3, column=0, padx=20, pady=10)

# Start the Tkinter main loop
root.mainloop()
SilasPDJ commented 1 year ago

Esteemed @CheemaOTB,

First of all: as a user I appreciate your PR. However, in my humble opinion there's a issue:

The scroll bar isn't going up/down, which is expected when some invisible option gets selected, so this way it'll be into user's visibility

I used this testing list for this feedback animalList = [ 'Cat', 'Dog', 'Bear', 'Dolphin', 'Kangaroo', 'Cat', 'Dog', 'Bear', 'Dolphin', 'Kangaroo', 'Cat', 'Dog', 'Bear', 'Dolphin', 'Kangaroo', 'Cat', 'Dog', 'Bear', 'Dolphin', 'Kangaroo', 'Cat', 'Dog', 'Bear', 'Dolphin', 'Kangaroo', 'Cat', 'Dog', 'Bear', 'Dolphin', 'Kangaroo', 'Cat', 'Dog', 'Bear', 'Dolphin', 'Kangaroo', 'Cat', 'Dog', 'Bear', 'Dolphin', 'Kangaroo', ]

Edit:

I know this is not a big deal, I'd appreciate if it was implemented together though.

Thanks in advance.

CheemaOTB commented 1 year ago

Hey @SilasPDJ

Thanks for the heads up! I'll look into the scroll bar issue right away and make sure to address it.

Appreciate it!