0

I am trying to build a GUI creator using Python and Tkinter, but ran into a problem.

My problem is How to add\update widgets in runtime?

for example:

  • I have created the main window.
  • In that main window, I have created a frame name w_frame which contains a bunch of widget.
  • Based on my input in the Text or Entry widget beside the w_frame, I want to update a particular widget.
  • Lets say w_frame contains a Entry widget, radio button, button and label all available with the basic or main attributes need to display it.
  • Now I want to change the background color of label.
  • In short I want to write the code label_name.property_name=value or for example a_label.bg=red in the text widget and as soon as I press apply button, the widget should change.

I have searched on web, but not able to find the required solution. Also tried using How can i update a certain widget in tkinter, but that does not work depending on my input.

from tkinter import *
root=Tk()
w_frame=Frame()
w_frame.pack()
def update_Frame():
    a=u_text_wid.get("1.0",END)
    b.config(a)
    root.update()
def add_wid_in_frame():
    global a,b
    a=Button(w_frame,text='heelo')
    a.pack()
    b=Label(w_frame,text='heelo')
    b.pack()
u_text_wid=Text()
u_text_wid.pack()
button1=Button(text="add",command=add_wid_in_frame)
button1.pack()
button1=Button(text="update",command=update_Frame)
button1.pack()
root.mainloop()

this results me in an error

unknown option "-bg="red"

Note: I want to update the widget based on the property value provided by the user, so it wont be hard-code into the script.

16
  • 2
    You need to provide a minimal reproducible example and the full error traceback as you said "ran into a problem". Commented Mar 17, 2022 at 15:40
  • Have you actually tried to do label_name.config(bg="red")? Can you show us an example that doesn't work? Calling config on a widget "at runtime" is a very common thing to do. Commented Mar 17, 2022 at 15:46
  • @acw1668 , I have added an example, please let me know if there are anyother problems. Commented Mar 17, 2022 at 15:55
  • @BryanOakley , I think you didnt get me, i dont want to hard code label_name.config(bg="red") into the script. According the input provided by the user in the text box, the widget would change its property. Commented Mar 17, 2022 at 15:56
  • You need to pass keyword options instead of a string to .config(). Commented Mar 17, 2022 at 15:57

2 Answers 2

1

You are getting the error because every thing you retrieve from Text widget is a string and you cannot directly pass an string to .config method, you need a keyword and then you can assign value which can be string.

According to your question and the comments on the question, what i have figured out is:

  • You want to run lable.config(bg='red') from the Text widget.
  • You want to change the property of specific widget.

Here's what you can do:

  1. To run Tkinter code form Text widget, you can use:
  • getattr method
  • eval method
  1. Just to change property of widget:
def update_Frame():
    global bcd
    a = u_text_wid.get("1.0", "end-1c")
    b=a.split(",")
    c=[tuple(i.split("=")) if "=" in i else i for i in b]
    d=dict(i for i in c)
    for key,value in d.items():
        bcd[key]=value
  • We can use string to change property only in this format widget_name[key]=value.

Some Useful Links:

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

Comments

1

For your case, you can use ast.literal_eval() to convert a JSON string to dictionary and use the dictionary in .config():

from ast import literal_eval
...
def update_Frame():
    a = u_text_wid.get("1.0", "end-1c") # don't include ending newline
    cnf = literal_eval(a) # convert JSON string to dictionary
    b.config(cnf)

Example input of the JSON string:

{"fg":"yellow", "bg":"red"}

Note that you can also use json module to convert the JSON string as well.

4 Comments

but the output from the text widget wont be in json format, so why to convert
My suggestion requires JSON format.
Can you provide me the advantage of using json as i have figured out a way to change the property witb same logic of your code but without json.
I think using JSON is simple and straight forward. But if you find your way, just use it.

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.