Giter Site home page Giter Site logo

Tkinter ListBox about guizero HOT 10 CLOSED

lawsie avatar lawsie commented on July 27, 2024
Tkinter ListBox

from guizero.

Comments (10)

lawsie avatar lawsie commented on July 27, 2024

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

from guizero.

martinohanlon avatar martinohanlon commented on July 27, 2024

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.

from guizero.

TomMalkin avatar TomMalkin commented on July 27, 2024

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().

from guizero.

martinohanlon avatar martinohanlon commented on July 27, 2024

@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)

from guizero.

martinohanlon avatar martinohanlon commented on July 27, 2024

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

from guizero.

TomMalkin avatar TomMalkin commented on July 27, 2024

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?

from guizero.

martinohanlon avatar martinohanlon commented on July 27, 2024

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.

from guizero.

martinohanlon avatar martinohanlon commented on July 27, 2024

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.

from guizero.

martinohanlon avatar martinohanlon commented on July 27, 2024

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.

from guizero.

martinohanlon avatar martinohanlon commented on July 27, 2024

in 0.5.1 just released

from guizero.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.