1

How would I get Tkinter labels to update at the click of a button? I am creating a Tkinter program to read and write a file then print each line into a label. I have got a section to add text to the file so I need a way to get the label to update.

Here is my code so far:

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()
Jonah Fleming
  • 1,187
  • 4
  • 19
  • 31
  • possible duplicate of [Update Tkinter Label from variable](http://stackoverflow.com/questions/2603169/update-tkinter-label-from-variable) – Reblochon Masque Sep 12 '15 at 03:30

1 Answers1

1

Edit: Judging by what I think you're trying to go for and to make the code more pythonic. I simplified the process of making labels while allowing for new inputs to be appended (not overwriting the existing contents) to "stats". Your write function will now add a new Label at the bottom every time the Submit button is pressed.

from Tkinter import *
import sys

def write():
    new_player = e1_var.get() + " " + e2_var.get()
    with open('stats', 'a') as file:
        file.write("\n" + new_player + "\n")

    players[new_player] = Label(text=new_player).grid(column=0)
    e1_var.set("")
    e2_var.set("")

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_var = StringVar()
e1 = Entry(textvariable=e1_var).grid(column=1, row=0)

e2_var = StringVar()
e2 = Entry(textvariable=e2_var).grid(column=1, row=1)

b1=Button(text="Submit", command=write).grid(column=1, row=3)

players = {}
with open("stats", "r") as file:
    for line in file.read().split('\n'):
        players[line] = Label(text=line).grid(column=0)

root.mainloop()

This is your new and better code!

Jonah Fleming
  • 1,187
  • 4
  • 19
  • 31
qwertyuip9
  • 1,522
  • 2
  • 16
  • 24
  • This didn't work. I get the **AttributeError: NoneType has no attribute 'update_idletasks()** – Jonah Fleming Sep 13 '15 at 06:44
  • All of the ones that print the text from the file. – Jonah Fleming Sep 13 '15 at 20:43
  • In your script - when the user presses submit, your text file is completely overwritten and adds just the new player/number that the user inputted. Is that what you want? I'm guessing you want additions to be added onto the stats.txt instead? – qwertyuip9 Sep 13 '15 at 21:11
  • Yes that's right! Thanks. I did actually change the 'w' to an 'a' but forgot to in this script. I'm just a beginer so I'm just going to ask a few questions about the code you put up: – Jonah Fleming Sep 13 '15 at 21:40
  • First, when you create a new label in the write function, it always has the same name. Wouldn't this not work? – Jonah Fleming Sep 13 '15 at 21:42
  • Also you have put players={}. What does the {} do? And what do the square brackets with new_player in them after players do. Could you use normal brackets? – Jonah Fleming Sep 13 '15 at 21:46
  • I used a dictionary so I didn't have to explicitly name it and allows me to continue referencing the label as need be. Say my new line was "PlayerA Number1", then players["PlayerA Number1"] will refer to the actual Label object. The variable names are the keys in the players dictionary. The key is unique and is given by the new input (which should be different than the existing player/numbers in your stats file). Dictionaries cannot have two of the same key, – qwertyuip9 Sep 13 '15 at 21:46
  • Dictionaries are created using the curly brackets. Dictionaries hold key and value pairs. I can set a key-value pair by using the notation: your_dictionary[key] = value. Suggest reading the [python doc for dictionaries](https://docs.python.org/2/tutorial/datastructures.html#dictionaries) – qwertyuip9 Sep 13 '15 at 21:49
  • Thanks was the = supposed to be there? I have suggested an edit. – Jonah Fleming Sep 13 '15 at 21:52
  • P.S pls +1 me cause i would like to +1 you but can't with 13 rep. – Jonah Fleming Sep 13 '15 at 22:02
  • I get an error saying `IOError: [Errno 2] No such file or directory: 'stats'` but I have got a stats txt file where i am running the script. This is on a mac. Any ideas? – Jonah Fleming Sep 13 '15 at 22:21
  • If it's a txt file, you should put "stats.txt" in the open statement. – qwertyuip9 Sep 13 '15 at 23:30