How to update Tkinter labels using a button.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jonah
    New Member
    • Sep 2015
    • 3

    #1

    How to update Tkinter labels using a button.

    Hi I am trying to create a simple program that writes a few lines to a file then displays those lines in labels below. The labels don't update though when I update the file and I have tried
    Code:
    update_idletasks
    but it doesn't work. Here is my code:

    Code:
    from Tkinter import *
    import sys
    f=open("stats")
    line1=f.readline()
    line2=f.readline()
    line3=f.readline()
    line4=f.readline()
    line5=f.readline()
    line6=f.readline()
    line7=f.readline()
    line8=f.readline()
    line9=f.readline()
    line10=f.readline()
    
    def write():
          f=open("stats", "w")
          f.write(e1.get())
          f.write(" ")
          f.write(e2.get())
          f.write("\n")
          e1.delete(0, END)
          e2.delete(0, END)
    
    root=Tk()
    root.title("Basketball Stats")
    l1=Label(text="Player").grid(column=0, row=0)
    l2=Label(text="Number").grid(column=0, row=1)
    e1=Entry().grid(column=1, row=0)
    e2=Entry().grid(column=1, row=1)
    b1=Button().grid(column=1, row=3)
    b1.config(text="Submit", command=write)
    l3=Label(text=line1).grid(column=0, row=4)
    l4=Label(text=line2).grid(column=0, row=5)
    l5=Label(text=line3).grid(column=0, row=6)
    l6=Label(text=line4).grid(column=0, row=7)
    l7=Label(text=line5).grid(column=0, row=8)
    l8=Label(text=line6).grid(column=0, row=9)
    l9=Label(text=line7).grid(column=0, row=10)
    l10=Label(text=line8).grid(column=0, row=11)
    l11=Label(text=line9).grid(column=0, row=12)
    l12=Label(text=line10).grid(column=0, row=13)
    root.mainloop()
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    We don't have the file so don't know what it contains, nor did you say how you want the labels updated. Initially you open "stats" as read only and never close it. When you open it again in the write() function, the result is unpredictable because the file is already open. The wite() function's file pointer is garbage collected/closed when the function exits. Later, the file pointer from the original open is closed when the program exits, so the file remains the same because it (probably) keeps the original dimensions from the first file pointer, i.e. truncates the new data. Note that "l1" can look like 11 or ll in some type fonts.
    Code:
    from Tkinter import *
    
    ## write a test file
    fname="./stats_2"
    with open(fname, "w") as f:
        for ctr in range(1, 11):
            f.write("line %d\n" % (ctr))
    
    records=[]
    with open(fname, "r") as f:
        for ctr in range(10):
            records.append(f.readline().strip())
     
    def write():
        first=e1.get()
        second=e2.get()
        with open(fname, "a") as f:   ## append not (over)write
           f.write("%s %s\n" % (first, second))
        e1.delete(0, END)
        e2.delete(0, END)
    
        ## update any labels for an example
        l1["text"]=first
        l1["bg"]="lightblue"
        l2["text"]=second
        l2["bg"]="lightyellow"
     
    root=Tk()
    root.title("Basketball Stats")
    
    ## in your code, "l1" (and everything else) is None
    ## because it contains the return from grid(), which is None
    l1=Label(text="Player")
    l1.grid(column=0, row=0)
    l2=Label(text="Number")
    l2.grid(column=0, row=1)
    e1=Entry(root)
    e1.grid(column=1, row=0)
    e2=Entry(root)
    e2.grid(column=1, row=1)
    b1=Button(root)
    b1.grid(column=1, row=3)
    b1.config(text="Submit", command=write)
    Button(root, text="quit", command=root.quit, bg="orange"). grid(column=1, row=4)
    
    r=4
    for ctr in range(10):
        ## this line returns None so no point in catching it
        Label(root, text=records[ctr]).grid(column=0, row=r+ctr)
    
    root.mainloop()

    Comment

    Working...