Tkinter, how to update label on canvas?

0

Hello, I'm trying to build a PivotTable that automatically updates values but I'm not able to update the Labels, I'm 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()

Does anyone know how to update the labels?

Thank you!

    
asked by anonymous 24.02.2018 / 15:49

1 answer

0

I think an easy solution would be to create the labels putting them in a list, since you are creating them all inside a for loop, it would probably be the best way to not 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 index, first label and second label. That way, if you have to change some value in a specific line you can use the index, if you do an update of all the lines at the same time I think you do not need this, anyway, to perform the configuration of the label you can use o .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 updating 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.

    
27.02.2018 / 15:58