1

Is it possible to have a list as a Tkinter Listbox listvariable? Or does it have to be a tuple? I would really like to make changes in the list and therefore I can't use a tuple. Any thoughts?

gary
  • 4,227
  • 3
  • 31
  • 58
Myone
  • 1,103
  • 2
  • 11
  • 24

1 Answers1

1

Have you tried using the list() and/or tuple() functions? Using these you could make a list into a tuple or a tuple into a list.

alist = [1,2,3]
atuple = (4,5,6)

new_tuple = tuple(alist)   # returns (1,2,3) which is a tuple
new_list = list(atuple)    # returns [4,5,6] which is a list

So if the listvariable returns data as a tuple, then you could read the list (using the get() method), change it to a list type (using list()), make your changes and pass your data back (using the tuple() and set() methods). However, in general if some widget only works with a given variable type, think about why that is and why your data is not of the same type.


There are a couple of good questions here about when/why to use lists or tuples in Python.

python: list vs tuple, when to use each?

  • the accepted answer suggests tuples are commonly used for heterogeneous data and lists for data that is homogeneous
  • another answer points out that tuples are immutable (data cannot be changed in place once assigned) while lists are mutable

Another question (What's the difference between list and tuples in Python?) mentions these same points and links to a couple articles on the topic.

Lastly, this link on the Listbox widget mentions the listvariable and its get/set methods.

Community
  • 1
  • 1
gary
  • 4,227
  • 3
  • 31
  • 58