0

here i have a list containing n string values

    str = ['var1','var2','var3',...]
    variable = StringVar(str[0])
    variable.set('hello world')

I want to convert the all string values in the list into String Var() This program shows error like str object has no object _root

I tried in may ways but I can't

thanks in advance for answers...!!

1
  • It is variable = StringVar(value=str[0]). Commented Jun 19, 2022 at 15:30

2 Answers 2

1

You need to create root window before defining strVars. Try like this:

import tkinter

root=tkinter.Tk()

strs = ['var1','var2','var3']

strvars = []
for s in strs:
   strvar = tkinter.StringVar(value=s)
   strvars.append(strvar)

print(strvars)

root.mainloop()
Sign up to request clarification or add additional context in comments.

Comments

0
import tkinter

root=tkinter.Tk()
variable = StringVar(root)
for st_r in ['var1','var2','var3',...]:
 variable.set(st_r)
root.mainloop()

Update:

import tkinter as tk

root=tk.Tk()
variable = tk.StringVar(root)
tk.Label(root, textvariable=variable).grid(row=0, column=3)
for st_r in ['var1','var2','var3',...]:
 variable.set(st_r)
 root.update()
 input()
root.mainloop()

3 Comments

What the point of the for loop because the result is the same as setting the last item in the list to variable?
I don't understand well your comment, but I have updated my answer. hope it be as you ask. thank for commenting
I mean that the final result of the for loop is the same as variable.set(["var1", ...][-1]).

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.