Home > Mobile >  StringVar() with Listbox multiple or extended - lists converting to string
StringVar() with Listbox multiple or extended - lists converting to string

Time:11-02

I have a tkinter Listbox and I'm using a StringVar().set() to set the values from a list. When I get the values back using StringVar().get(), they are converted to an awkward string format, like this:

"('Item1', 'Item2', 'Item3')"

What's the best way to avoid this conversion in the first place, or failing that, to convert the string back to the initial list?

You can use this snippet of code to reproduce the problem in its most simple form:

import tkinter as tk

root = tk.Tk()
values = tk.StringVar(root)
values.set(['Item1', 'Item2','Item3'])
print(values.get())

It's not pretty, but I had come up with this:

values.get()[2:-2].split("', '")

CodePudding user response:

As StringVar is inherited from Variable with overridden get() function which converts the stored value to string.

Using Variable will not have such conversion.

  • Related