0

I am working on a project that uses XML to create a Tkinter GUI and need to know how to convert an object name to a string. For example:

# In the actual program, the value of widget variables is set by the values of XML attributes

label_name = "mylabel"

root = Tk()

exec(label_name+" = Label("+str(root)+", text='Hello World')")

Using str(root) does not work. What can I do to make this work?

0

1 Answer 1

1

Just use the string root:

exec(label_name + " = Label(root, text='Hello World')")

Demo:

In [31]: root = tkinter.Tk()
In [32]: from tkinter import Label
In [33]: exec("label_name = Label(root, text='Hello World')")
In [34]: print(label_name.grid_size())
(0, 0)
Sign up to request clarification or add additional context in comments.

2 Comments

This would be great... but root isn't the only possibility for a master argument... for example I might be placing it inside a Frame instead of the root
@CaptainCucumber, makes no difference just put the string in there and exec will take care of the rest

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.