Default value for Listbox (Tkinter)

H

Harlin Seritt

Whenever I set up something similar:

vals = ['1', '2','3']
for v in vals:
listbox.inset(END, 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
 
?

=?ISO-8859-1?Q?J=F8rgen_Cederberg?=

Harlin said:
Whenever I set up something similar:

vals = ['1', '2','3']
for v in vals:
listbox.inset(END, 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?

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:
http://www.pythonware.com/library/tkinter/introduction/index.htm
http://infohost.nmt.edu/tcc/help/pubs/tkinter/

HTH
Jørgen Cederberg
 
H

Harlin Seritt

Thanks Jorgen!

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


select_set(index), select_set(first, 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,250
Latest member
Charlesreero

Latest Threads

Top