0

I'm having some trouble with stringVars from TK.

from tkinter import *

root = Tk()
strVar = StringVar()
tmp1 = ['one','two','three']
print(tmp1)
print(len(tmp1))

strVar.set(tmp1)
tmp2=strVar.get()
print(tmp2)
print(len(tmp2))

Output is:

['one', 'two', 'three'] 
3
('one', 'two', 'three')
23

As you can see, the format is different. Obviously, the list of strings is internally converted into one string with quotes. What ist the reason and how can I avoid it? For my script I would like to have a list of strings for further processing.

6
  • 1
    While the idea to assign a list to a string var is funny, that's actually not a bad question, What is going on there? Why is the list first converted to tuple and then to string? E.g. if you pass a number or a dict to the string var, those are converted to str only. Commented Oct 1, 2018 at 15:51
  • 1
    Why do you feel like you need to keep your list of strings in a StringVar? What's wrong with using just a normal python list? Commented Oct 1, 2018 at 17:33
  • 1
    A better idea might be to just have the user enter a number of strings separated by some special character (space, comma, semicolon, whatever), and then str.split the value of strVar.get() accordingly. That's even easier to use than having to enter all those [, ', etc. Commented Oct 1, 2018 at 17:52
  • Hi, thanks for your replies, I have to admit that I am pretty new to python and especially to TK. Background is that I get a list of strings from a system command like result = subprocess.run(['...']).stdout.decode('ascii') which I pass to a listbox via StringVar. At a later point I want to get the data back from this StringVar to use it further. Commented Oct 2, 2018 at 6:10
  • @Jensenmann: you don't need to use a StringVar to move data in and out of a listbox. Commented Oct 2, 2018 at 12:39

3 Answers 3

1

@tjt263, it's very long time ago, but I think I solved it like

    fdupesSV.set(fdupesList)
    tmp = subprocess.run(['fdupes', '-r',directory.get()], stdout=subprocess.PIPE)

    #first convert bytes to ascii, then split at '\n', then assign so fdupesOut
    fdupesList=tmp.stdout.decode('ascii').split('\n')
    fdupesSV.set(fdupesList)

I am not sure if I remember the issue correctly but I hope it helps you

Cheers Jens

Sign up to request clarification or add additional context in comments.

Comments

0
from tkinter import *
import ast

root = Tk()
strVar = StringVar()
tmp1 = ['one','two','three']
print(type(tmp1), tmp1, len(tmp1))

strVar.set(tmp1)
x = ast.literal_eval('[' + strVar.get()[1:-1] + ']')
print(type(x), x, len(x))

3 Comments

Hi thanks for the post. Even if this works, it seems to me not very clean. Background is that I get a list of strings from a system command like result = subprocess.run(['...']).stdout.decode('ascii') which I pass to a listbox via StringVar. I learned now that StringVar is not designed to hold any lists, so I think I should rather keep the list in a separate python list variable and just convert the list to string before setting the stringvar for the listbox.
@Jensenmann, Hi, that's exactly what I'm trying to do. Did you find a good solution?
@tjt263, please find my answer above
0

I've just tested an easy way to do this, which was suggested by tobias_k's comment above

Start with your list of strings, but rather than encoding them, just join the list of strings into a single string using newline or another separator (if your strings might include newline).

Then just split the strings after getting the value.

For example:

# To set the string variable:

list_of_strings = ['Str 1', 'Str 2', 'Str 3']  # your data

list_var = tk.StringVar()
list_var.set('\n'.join(list_of_strings)

# To read the string variable:
received_list = list_var.get().split('\n')

if your initial list of strings includes newlines, then you could use any other character to join the strings.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.