473,503 Members | 1,625 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to update Tkinter labels using a button.

3 New Member
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
1 1756
dwblas
626 Recognized Expert Contributor
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

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

Similar topics

4
2300
by: Shane | last post by:
I would like to update Access by using a txtBox. Is it possible to update Access this way? Example: If I selected the txtBox and input a line of text I would like that line of text to be sent...
6
7585
by: kareemaffan | last post by:
Hello Everyone I want to insert values into MS Access database through VB.NET in Visual Studio 2005 . I have used the following code which is not working. Private Sub Button2_Click(ByVal sender...
2
7570
by: rameshgohil | last post by:
I am using grid view and a button column in it using <itemTemplate> but I am not able to rerive cell value of a selected row from grid view. I have tried the following to methods in Row_command...
7
6497
by: ykhamitkar | last post by:
Hi there, I have a form where I want to add new row using button and done want a default new record, I have changed "Allow Additions" to No for the forms properties and have added following...
3
4390
by: KKSohaib | last post by:
how I update sql table using datagridview cell..... I want to edit cell and then press enter it update the sql table ... ??? It possible or not ... ?
2
1052
by: atultaj | last post by:
without using button how we can insert the value in databas using textbox
0
7063
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6970
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7441
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5558
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4987
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4663
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3156
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
720
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
366
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.