im writing a personal program thats storing scores.
The layout is done using Tkinter using labels buttons and entry widgets,
however, because there are a lot of entry widgets needing created i just stuck
their creation in a loop,

for m in range(1,10):
            self.input_score = Entry(frame,width=6)
            self.input_score.grid(column=5, row=m)

so i have 9 entries, but only the last widget has a name (self.input_score).
What i want to do is have say a list of variable names and have them
automatically assigned to the widget....

this is probably a bad example

Names = ["one","two","three","four","five","six,"seven","eight","nine"]
for m in range(1,10):
    self.input_score.Names[m-1]= Entry(frame,width=6)

where Names[m-1] actually get replaced with the name in the list.

Therefore the first Entry widget will be stored in the variable self.input_scoreone

8 Years
Discussion Span
Last Post by Shakis

I usually use a dictionary or list

for m in range(1, 10):
     self.input_score_dic[m] = Entry(frame,width=6)

i was trying to avoid puttin them in a dictionary...

thats why ij just wanted the last part of the variable to be the part to change

loop 1
input_score_one = entry
input_score_two = entry


If you insist on giving each entry field a recognizable name, you can try this:

# create named multi entries in Tkinter

import Tkinter as tk

class MyFrame(tk.Frame):
    entries are in the same grid
    def __init__(self, master):
        tk.Frame.__init__(self, master)

        words = ["one","two","three","four","five","six","seven","eight","nine"]
        # create a list of entries
        ent_list = []
        for w in words:
            ent_list.append(tk.Entry(self, width=6))

        # assign each entry in the list a grid position
        for ix, ent in enumerate(ent_list):
            ent.grid(row=ix, column=0)
        # rename the entries in the list
        for ix, ent in enumerate(ent_list):
            rename = "self.input_score_%s = ent" % words[ix]
        # create a test button
        tk.Button(self, text="press to test", command=self.test).grid()

        # frame
    def test(self):
        # test it ...
        self.input_score_three.insert(0, "test3")
        self.input_score_eight.insert(0, "test8")

root = tk.Tk()
frame = MyFrame(root)

hmmm i find it quite difficult to get my head round that lol ive never looked at using % in that manner before,

im trying to streamline my code a bit to save me writing this out for every player:

def load_scores(self):
        i = 1
        line = scores.readline()
        line = line[:-1]
        line = split(line, ",")
        self.lastscore_Bill= Label(frame, text=line[0])
        self.totalscore_Bill = Label(frame, text=line[1])
        self.worstscore_Bill = Label(frame, text=line[2])
        self.worstscore_Bill = Label(frame, text=line[3])
        self.bestscore_Bill = Label(frame, text = line[4])
        i= i + 1
        line = scores.readline()
        line = split(line, ",")
        line[-1] = line[-1][:-1]
        self.lastscore_John = Label(frame, text=line[0])
        self.totalscore_John = Label(frame, text=line[1])
        self.worstscore_John = Label(frame, text=line[2])
        self.worstscore_John = Label(frame, text=line[3])
        self.bestscore_John = Label(frame, text = line[4])
        i= i + 1

all that is changing between these two chunks of code really is the suffix on the var names from Bill to John

is the way you described the only way ?


It's time to learn dictionaries as that is the solution to both problems. Give it a try and post back if you can't get something to work. (You are using worstscore twice in the Label but only have to store it once)
self.score_dic["Bill"]=[lastscore, totalscore, worstscore, bestscore]
so self.scores_Bill = self.score_dic["Bill"]
You would never use worstscore_Bill, but would use self.scores_Bill[2] or self.score_dic["Bill"][2]
If you don't use a dictionary, then going beyond a few players gets real tedious real fast.


so how do i fit the labels into this?

i need each label to have a name so i can destroy it to up date the scores

so i need to be able to do self.varname.destroy()


Just update the label instead of destroy then re-create. Look for the quote that follows at this link http://effbot.org/tkinterbook/label.htm Hopefully that is what you want. If not, my apologies but a dictionary can still be used as it is just a container for the variable, Tkinter.Label or whatever else you may use. I've never tried doing it this way, but you could
self.input_score_dic[m] = Label(frame,width=6) or whatever
self.input_score_dic[m] = None
self.input_score_dic[m] = Label(frame,width=6) with new text

You can associate a Tkinter variable with a label. When the contents of the variable changes, the label is automatically updated:

v = StringVar()
Label(master, textvariable=v).pack()

v.set("New Text!")


yeah dictionaries all the way lol sorry im an idiot and couldnt work it out sooner lmao ....thankx for all the help guys :D


This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.