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_framewhich 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_framecontains 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=valueor for examplea_label.bg=redin 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.
label_name.config(bg="red")? Can you show us an example that doesn't work? Callingconfigon a widget "at runtime" is a very common thing to do.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..config().