Default value for Listbox (Tkinter)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Harlin Seritt

    Default value for Listbox (Tkinter)

    Whenever I set up something similar:

    vals = ['1', '2','3']
    for v in vals:
    listbox.inset(E ND, v)

    I notice that when this listbox is displayed, there is never a default
    value. How can I make sure that one of the indices is selected by
    default?

    Thanks,

    Harlin

  • Jørgen Cederberg

    #2
    Re: Default value for Listbox (Tkinter)

    Harlin Seritt wrote:[color=blue]
    > Whenever I set up something similar:
    >
    > vals = ['1', '2','3']
    > for v in vals:
    > listbox.inset(E ND, v)
    >
    > I notice that when this listbox is displayed, there is never a default
    > value. How can I make sure that one of the indices is selected by
    > default?
    >[/color]

    Hi Harlin,

    you must use the select_set method of the listbox.

    ---
    from Tkinter import *

    root = Tk()
    listbox = Listbox(root)
    listbox.pack()

    vals = ['1', '2','3']
    for v in vals:
    listbox.insert( END, v)

    listbox.select_ set(0) # sets the first element

    root.mainloop()
    ---

    Some good resources for Tkinter:
    An Introduction to Tkinter: Explore the essential Python library for GUI design. This guide covers widgets, event handling, and geometry management to help you build functional, cross-platform desktop apps.



    HTH
    Jørgen Cederberg

    Comment

    • Harlin Seritt

      #3
      Re: Default value for Listbox (Tkinter)

      Thanks Jorgen!

      I was reading the Tkinter tutorial (I was looking at this particular
      page:
      http://www.pythonware.com/library/tk...13-methods.htm)
      and saw this for select_set():


      select_set(inde x), select_set(firs t, last)
      Add one or more items to the selection.


      I think this was why I overlooked it. The description for it says the
      listbox adds one or more items to a selection. I would think the
      description should say the listbox sets the index or element.

      Thanks again,

      Harlin

      Comment

      Working...