TomSchimansky / CustomTkinter

A modern and customizable python UI-library based on Tkinter
MIT License
10.77k stars 1.03k forks source link

Unable to update text with .configure(text='new text') #2430

Closed ohshitgorillas closed 1 month ago

ohshitgorillas commented 1 month ago

I have a label that I am creating in one part of my code:

        r43_t0int_label  = ctk.CTkLabel(stats_frame, text=f't₀: {analysis.ratio43_t0int:.3f} ± {analysis.ratio43_uncert:.3e}', anchor='w')
        r43_t0int_label.pack(fill='x')
        r43_t0err_label  = ctk.CTkLabel(stats_frame, text=f'error: {analysis.ratio43_puncert:.3f}%', anchor='w')
        r43_t0err_label.pack(fill='x')

and I'm trying to update it in another section of my code:

    if analysis_stats[0] != analysis.ratio43_t0int:
        r43_t0int_label = stats_frame.winfo_children()[1]
        r43_t0int_label.configure(text=f't₀: {analysis.ratio43_t0int:.3f} ± {analysis.ratio43_uncert:.3e}')
        r43_t0err_label = stats_frame.winfo_children()[2]
        r43_t0err_label.configure(text=f'error: {analysis.ratio43_puncert:.3f}%')

According to the documentation, this is the correct approach, however, I get

ValueError: ['text'] are not supported arguments. Look at the documentation for supported arguments.
Akascape commented 1 month ago

Why are you using method this before configure?

r43_t0int_label = stats_frame.winfo_children()[1]

By doing this you are replacing the ctklabel variable with some other widget, where text is not configurable. You don't need to add that line, try to make the r43_t0int_label a global variable.

ohshitgorillas commented 1 month ago

Yes, this was correct, thank you! Here is the corrected code structure:

def write_stats(stats, stats_frame):
    global labels
    if not stats_frame.winfo_children():
        labels = {}
        create_labels(labels, stats_frame)
    fill_labels(labels, stats)

where fill_labels(labels, stats) uses label.configure(text='new text')