简体   繁体   中英

How do I show updated values in the 'label' widget in Python Tkinter GUI?

I have a python Tkinter based GUI in which I wish to show values of several variables when the "read" button is clicked. Ideally, this should happen in a certain frame in a window where the updated variables are shown, without disturbing the rest of the GUI widgets, which have other functions.

The problem I'm encountering is this- Each time I hit "read", the updated variables are listed right underneath the older variables instead of overwriting in that location. I seem to have an incorrect understanding of the working of the label, and padx, pady (the placement of the labels). I have pasted the code below. What should be the correct way so that the previous data is erased and new data is pasted in that very location?

def getRead():
    #update printList
    readComm()
    #update display in GUI
    i=0
    while i < (len(printList)):
        labelR2=Tk.Label(frameRead, text=str(printList[i]))
        labelR2.pack(padx=10, pady=3)
        i=i+1

frameRead=Tk.Frame(window)
frameRead.pack(side=Tk.TOP)
btnRead = Tk.Button(frameRead, text = 'Read', command= getRead)
btnRead.pack(side = Tk.TOP)  

window.mainloop()

The code above successfully displays elements of printList in one column type display. However, each time getRead is called (when the read button is clicked), it appends to the previous display.

PS - If there is a better way to display data, other than label widget, then please suggest that.

The problem is you're creating a new set of labels each time you run getRead . It sounds like what you want to do is update the text of the existing labels, not create new ones. Here's one way to do that:

labelR2s = []

def getRead():
    global labelR2s
    #update printList
    readComm()
    #update display in GUI

    for i in range(0, len(labelR2s)):               # Change the text for existing labels
        labelR2s[i].config(text=printList[i])

    for i in range(len(labelR2s), len(printList)):  # Add new labels if more are needed
        labelR2s.append(Tk.Label(frameRead, text=str(printList[i])))
        labelR2s[i].pack(padx=10, pady=3)

    for i in range(len(printList), len(labelR2s)):  # Get rid of excess labels
        labelR2s[i].destroy()
    labelR2s = labelR2s[0:len(printList)]

    window.update_idletasks()

I'm answering my own question, by modifying the answer Brionius gave, since his answer is giving a referencing error. This code below works fine for me.

labelR2s=[]
def getRead():
    #update printList
    readComm()
    #update display in GUI

    for i in range(0, len(printList)):               # Change the text for existing labels
        if (len(printList)>len(labelR2s)):           # this is the first time, so append
            labelR2s.append(Tk.Label(frameRead, text=str(printList[i])))
            labelR2s[i].pack(padx=10, pady=3)
        else:
            labelR2s[i].config(text=printList[i])    # in case of update, modify text

    window.update_idletasks()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM