Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tkinter ListBox #43

Closed
TomMalkin opened this issue Oct 3, 2017 · 10 comments
Closed

Tkinter ListBox #43

TomMalkin opened this issue Oct 3, 2017 · 10 comments
Projects

Comments

@TomMalkin
Copy link

Are there plans to add the Tkinter ListBox?

@lawsie
Copy link
Owner

lawsie commented Oct 4, 2017

Not really thought about it, but no reason why not!

@martinohanlon martinohanlon added this to Todo in 0.6 Apr 10, 2018
@martinohanlon martinohanlon removed this from Todo in 0.6 Apr 30, 2018
@martinohanlon
Copy link
Collaborator

I am going to add ListBox into the next release.

This is what Im thinking:

  • ListBox will inherit from TextWidget
  • The interface will be very similar to Combo having a list of options a selected and the ability to add an option and clear
  • tkinter doesnt provide a command for a Listbox so I will add an event to detect mouse clicks and use that to generate a command when the ListBox changes
  • scrollbars will be needed, Ill make this a option on the constructor, like a multiline TextBox

Any thoughts, greatly received.

@TomMalkin
Copy link
Author

Things to think about would be how the command parameter would deal with multi selection. Would it pass as argument one of the command function the selected items from the ListBox in a list?

To be honest I don't think you would need a command argument for the first crack at a ListBox. If the ListBox had a selected() function that returns a list of selected items, then you could use a PushButton with the ListBox as one of the list parameter, and inside that command function linked to the button use ListBox.selected().

@martinohanlon
Copy link
Collaborator

@harlekuin thanks for the input.

Im not sure about the command yet either... but I think its important that there is an event that fires when the selection of a ListBox changes, it doesnt necessarily have to return anything though.

Re a selected() function. I will implement a value property (to be consistent with other guizero widgets) which either returns single value if the ListBox is a "single select" listbox or a tuple of values if the ListBox is "multi select" .

multiselect will be a parameter passed in at construction, defaulted to False.

e.g.

to create a single select list box:

lb = ListBox(app)

or a multiselect listbox:

lb = ListBox(app, multiselect=True)

@martinohanlon
Copy link
Collaborator

Ok, so update..

In regards to ListBox API.

Ignoring all the stuff which ListBox will inherit from TextWidget (bg, text_color, font, after, etc, etc) it will have the following properties and methods.

  • __init__(self, master, items=None, selected=None, command=None, args = [], grid=None, align=None, visible=True, enabled=None, multiselect=False)
    • master - container the widget is 'owned' by
    • items - a list of string for items to add into the ListBox
    • selected - item or items to select at init
    • command - a function which will get called when the ListBox selection changes
    • args - a list of arguments to pass to the command
    • grid - grid position
    • align - alignment in grid
    • visible - if visible at init
    • enabled = if enabled at init
    • multiselect - if the ListBox should allow multiple items to be selected
  • value - Sets or returns the items selected in a ListBox None if 0 items are selected. If the ListBox is a multiselect, value is a tuple of items selected, if not value is a single integer.
  • insert(index, item) - Insert a new item at index.
  • append(item) - Appends a new item to the end of the ListBox.
  • remove(item) - Removes an item from the ListBox.
  • clear() - Clears all the items from a ListBox
  • items - Returns a list of items in the ListBox

@TomMalkin
Copy link
Author

Another thought - a problem with having a command run on selection change is that it might not match the common use case for a multi-select listbox where the user selects several options before running a function on the "finalised" selections. In the pizza toppings case, the user would be picking x out of y possible toppings, and it doesn't make sense in this case (I think) to run the command for ('cheese'), then ('cheese', 'ham') then ('cheese', 'ham', 'pineapple'). Maybe this is just an example where a button with the finalised function would make more sense.

Also, would there be the listbox.value as the first argument as default of any command function?

What are your thoughts on making multiselect=True as the default? not a big thing but a single-select listbox is quite similar to a combobox in use cases so I can see multi-select being used more often.

will remove(item) find the first instance of the item in the box? or will it remove multiples if they exist?

@martinohanlon
Copy link
Collaborator

martinohanlon commented May 5, 2018

My thoughts for including a command is to support knowing when the value within a ListBox has changed, not to return what the value is / was. I think this is the right approach as with a listbox its possible the change to the value could be that something has been deselected so there is no value.
Command will be optional so its a feature rather than a requirement.

My choice for multiselect = False is that it is consistent with tkinter and in-line with multiline in TextBox.

The append, insert, remove methods will be consistent with a Python list, so yes remove will remove the first instance of an item found.

@martinohanlon
Copy link
Collaborator

I am thinking perhaps command should return an optional parameter if the callback accepts it, similar to combo, buttongroup, etc. As @harlekuin said it could just return the value of value.

@martinohanlon
Copy link
Collaborator

This is in dev and will be in 0.5.1 - I think the internal code will need refactoring in the future, its was surprisingly difficult to add a scrollbar to the listbox and still have it work in a "guizero" way.

I think we can do better, perhaps as part of the layouts work for 0.6.0, but for the moment this is good and the API works well.

@martinohanlon
Copy link
Collaborator

in 0.5.1 just released

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
0.5.1
Awaiting triage
Development

No branches or pull requests

3 participants