By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,558 Members | 1,373 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,558 IT Pros & Developers. It's quick & easy.

How to update Tkinter labels using a button.

P: 3
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
Expand|Select|Wrap|Line Numbers
  1. update_idletasks
but it doesn't work. Here is my code:

Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2. import sys
  3. f=open("stats")
  4. line1=f.readline()
  5. line2=f.readline()
  6. line3=f.readline()
  7. line4=f.readline()
  8. line5=f.readline()
  9. line6=f.readline()
  10. line7=f.readline()
  11. line8=f.readline()
  12. line9=f.readline()
  13. line10=f.readline()
  14.  
  15. def write():
  16.       f=open("stats", "w")
  17.       f.write(e1.get())
  18.       f.write(" ")
  19.       f.write(e2.get())
  20.       f.write("\n")
  21.       e1.delete(0, END)
  22.       e2.delete(0, END)
  23.  
  24. root=Tk()
  25. root.title("Basketball Stats")
  26. l1=Label(text="Player").grid(column=0, row=0)
  27. l2=Label(text="Number").grid(column=0, row=1)
  28. e1=Entry().grid(column=1, row=0)
  29. e2=Entry().grid(column=1, row=1)
  30. b1=Button().grid(column=1, row=3)
  31. b1.config(text="Submit", command=write)
  32. l3=Label(text=line1).grid(column=0, row=4)
  33. l4=Label(text=line2).grid(column=0, row=5)
  34. l5=Label(text=line3).grid(column=0, row=6)
  35. l6=Label(text=line4).grid(column=0, row=7)
  36. l7=Label(text=line5).grid(column=0, row=8)
  37. l8=Label(text=line6).grid(column=0, row=9)
  38. l9=Label(text=line7).grid(column=0, row=10)
  39. l10=Label(text=line8).grid(column=0, row=11)
  40. l11=Label(text=line9).grid(column=0, row=12)
  41. l12=Label(text=line10).grid(column=0, row=13)
  42. root.mainloop()
  43.  
Sep 13 '15 #1
Share this Question
Share on Google+
1 Reply


Expert 100+
P: 618
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.
Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2.  
  3. ## write a test file
  4. fname="./stats_2"
  5. with open(fname, "w") as f:
  6.     for ctr in range(1, 11):
  7.         f.write("line %d\n" % (ctr))
  8.  
  9. records=[]
  10. with open(fname, "r") as f:
  11.     for ctr in range(10):
  12.         records.append(f.readline().strip())
  13.  
  14. def write():
  15.     first=e1.get()
  16.     second=e2.get()
  17.     with open(fname, "a") as f:   ## append not (over)write
  18.        f.write("%s %s\n" % (first, second))
  19.     e1.delete(0, END)
  20.     e2.delete(0, END)
  21.  
  22.     ## update any labels for an example
  23.     l1["text"]=first
  24.     l1["bg"]="lightblue"
  25.     l2["text"]=second
  26.     l2["bg"]="lightyellow"
  27.  
  28. root=Tk()
  29. root.title("Basketball Stats")
  30.  
  31. ## in your code, "l1" (and everything else) is None
  32. ## because it contains the return from grid(), which is None
  33. l1=Label(text="Player")
  34. l1.grid(column=0, row=0)
  35. l2=Label(text="Number")
  36. l2.grid(column=0, row=1)
  37. e1=Entry(root)
  38. e1.grid(column=1, row=0)
  39. e2=Entry(root)
  40. e2.grid(column=1, row=1)
  41. b1=Button(root)
  42. b1.grid(column=1, row=3)
  43. b1.config(text="Submit", command=write)
  44. Button(root, text="quit", command=root.quit, bg="orange"). grid(column=1, row=4)
  45.  
  46. r=4
  47. for ctr in range(10):
  48.     ## this line returns None so no point in catching it
  49.     Label(root, text=records[ctr]).grid(column=0, row=r+ctr)
  50.  
  51. root.mainloop() 
Sep 14 '15 #2

Post your reply

Sign in to post your reply or Sign up for a free account.