Tkinter, how to update label on canvas?

Asked

Viewed 699 times

0

Hello, I am trying to build a dynamic table that updates the values automatically but I am not able to update the Labels, I am using the following code:

import tkinter as tk


def populate(frame):
    global widgets
    widgets={}

    '''Put in some fake data'''
    tk.Label(frame, text='X',background='white').grid(row=0, column=0,padx=10,pady=5)
    tk.Label(frame, text='K',background='white').grid(row=0, column=1,padx=10,pady=5)
    tk.Label(frame, text='S',background='white').grid(row=0, column=2,padx=10,pady=5)
    tk.Label(frame, text='(%)',background='white').grid(row=0, column=3,padx=10,pady=5)

    for row in range(1,100):

        tk.Label(frame, text="%s" % row, width=3, borderwidth="1", 
                 relief="solid").grid(row=row, column=0)
        t="this is the second column for row %s" %row
        tk.Label(frame, text=t).grid(row=row, column=1)
        tk.Label(frame, text='ANOTHER COLUMN'+str(row)).grid(row=row,column=2)


def onFrameConfigure(canvas):
    '''Reset the scroll region to encompass the inner frame'''
    canvas.configure(scrollregion=canvas.bbox("all"))

table_root = tk.Tk()
canvas = tk.Canvas(table_root, borderwidth=0, background="#ffffff")
frame = tk.Frame(canvas, background="#ffffff")
vsb = tk.Scrollbar(table_root, orient="vertical", command=canvas.yview)
canvas.configure(yscrollcommand=vsb.set)

vsb.pack(side="right", fill="y")
canvas.pack(side="left", fill="both", expand=True)
canvas.create_window((4,4), window=frame, anchor="nw")

frame.bind("<Configure>", lambda event, canvas=canvas: onFrameConfigure(canvas))

populate(frame)

table_root.mainloop()

Can anyone tell me how I can update the Abels?

Thank you!

1 answer

0

I think an easy solution would be to create the Abels put them in a list, since you’re creating all of them inside a loop for, it’s kind of the best way not to have to create 100 variables, something like

def populate(frame):
    label_list = []
    for row in range(1,100):
        ind =tk.Label(frame, text="%s" % row, width=3, borderwidth="1", 
                     relief="solid").grid(row=row, column=0)
        t="this is the second column for row %s" %row
        labelc1 = tk.Label(frame, text=t).grid(row=row, column=1)
        labelc2 = tk.Label(frame, text='ANOTHER COLUMN'+str(row)).grid(row=row,column=2)
        lista = [ind, labelc1, labelc2]
        label_list.append(lista)

Soon you have a list with the Indice, first label and second label. This way, if you have to change some value in a specific line you can use the Indice, if you are going to do an update of all the lines at the same time I think you do not need it, anyway, to perform the configuration of the label you can use the . config

def troca():
    for x in label_list:
        if x[0]["text"] == '5':
            x[1].config(text = "Olá mundo")
            x[2].conf(text = "Tchau mundo")

if you are going to update everything at the same time by pulling the value of something just remove the if and update using x[1]. config and x[2]. config.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.