0

Trying to assign a list element to a label in a loop, there are 6 elements & 6 labels. I can get it to work if I explicitly assign the labels, but there must be a neater way, which I cannot as yet fathom. :-( Code is below

import tkinter as tk
import random as rand

window = tk.Tk()

def gen_nums():
    my_rand = sorted(rand.sample(range(1,49), 6))

    for x in range(0, 6):
        my_str = lbl_value+str(x)
        my_str = str(my_rand[x])       

"""
    lbl_value0["text"] =  str(my_rand[0])
    lbl_value1["text"] =  str(my_rand[1])
    lbl_value2["text"] =  str(my_rand[2])
    lbl_value3["text"] =  str(my_rand[3])
    lbl_value4["text"] =  str(my_rand[4])
    lbl_value5["text"] =  str(my_rand[5])
"""
        

window.rowconfigure([0, 1,2,3,4,5,6], minsize = 50, )
window.columnconfigure(0, minsize = 150, )

window.rowconfigure([0,1,2,3,4,5,6], minsize = 50, )
window.columnconfigure(0, minsize = 20, )

btn_gen = tk.Button(master=window,
                    width = 20,
                    font = "arial",
                    text = "Generate a set",
                    command = gen_nums,
                    fg = "red",
                    bg = "white")

btn_gen.grid(row=0, column=0, sticky="news")

lbl_value0 = tk.Label(master=window, text = " ")
lbl_value1 = tk.Label(master=window, text = " ")
lbl_value2 = tk.Label(master=window, text = " ")
lbl_value3 = tk.Label(master=window, text = " ")
lbl_value4 = tk.Label(master=window, text = " ")
lbl_value5 = tk.Label(master=window, text = " ")
lbl_value0.grid(row=1, column=0, sticky = "news")
lbl_value1.grid(row=2, column=0, sticky = "news")
lbl_value2.grid(row=3, column=0, sticky = "news")
lbl_value3.grid(row=4, column=0, sticky = "news")
lbl_value4.grid(row=5, column=0, sticky = "news")
lbl_value5.grid(row=6, column=0, sticky = "news")

window.mainloop()`
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
KevP
  • 1
  • 2
  • 2
    Create a list of `tk.Label` objects, rather than the individual variables `lbl_value0` etc. See: https://stackoverflow.com/questions/1373164/how-do-i-create-variable-variables?noredirect=1&lq=1 – slothrop Jul 30 '23 at 12:28
  • 1
    If you want to keep the label names then you could use a Dictionary with the keys as `"lbl_value"+str(x)` and the associated values as `str(my_rand[x])` – user19077881 Jul 30 '23 at 12:43
  • Examples of above 2 please, still struggling. :-( – KevP Jul 30 '23 at 14:50

3 Answers3

0

Rather than using separate variables called lbl_value0 and so on, you can create a list of labels in a loop:

num_labels = 6
labels_list = []
for i in range(num_labels):
   lbl = tk.Label(master=window, text = " ")
   lbl.grid(row=i+1, column=0, sticky = "news")
   labels_list.append(lbl)

Then your gen_nums function can also use a loop. The zip builtin gives a convenient way of picking corresponding pairs of label and random value:

def gen_nums():
    my_rand = sorted(rand.sample(range(1,49), num_labels))

    for lbl, val in zip(labels_list, my_rand):
        lbl["text"] = str(val) 
slothrop
  • 3,218
  • 1
  • 18
  • 11
  • 1
    OK, thanks for that, I was getting close to what you showed above, but not quite close enough obviosly! Think my waning Tcl?Tk knowledge is confusing me a bit? – KevP Jul 30 '23 at 15:46
0

Example of using a Dictionary:

import random as rand

my_dict = {}
def gen_nums():
    my_rand = sorted(rand.sample(range(1,49), 6))

    for x in range(0, 6):
        my_dict["lbl_value"+str(x)] = str(my_rand[x])
        
gen_nums()

print(my_dict)

gives (for example):

{'lbl_value0': '2',
 'lbl_value1': '3',
 'lbl_value2': '9',
 'lbl_value3': '11',
 'lbl_value4': '18', 
 'lbl_value5': '45'}

The values can then be accessed using the keys.

user19077881
  • 3,643
  • 2
  • 3
  • 14
  • Thanks everyone, all sorted for now. I'm sure there'll be lots more on my jPython journey! – KevP Aug 01 '23 at 16:43
0

but there must be a neater way, which I cannot as yet fathom.

By using looping to create iterate widgets Label and grid manager layout. In order, reducing coding.

Snippet:

import random as rand
import tkinter as tk

window = tk.Tk()

def gen_nums():
    my_rand = sorted(rand.sample(range(1,49), 6))

    for x in range(0, 6):                 
        my_str = str(my_rand[x])
        t = tk.Label(window, text=my_str )
        t.grid(row=x+1, column=0, sticky = "news")

        t.config(text=my_str)
   

window.rowconfigure([0, 1,2,3,4,5,6], minsize = 50, )
window.columnconfigure(0, minsize = 150, )

window.rowconfigure([0,1,2,3,4,5,6], minsize = 50, )
window.columnconfigure(0, minsize = 20, )

btn_gen = tk.Button(window,
                    width = 20,
                    font = "arial",
                    text = "Generate a set",
                    command = gen_nums,
                    fg = "red",
                    bg = "white")

btn_gen.grid(row=0, column=0, sticky="news")

window.mainloop()

Screenshot:

enter image description here

toyota Supra
  • 3,181
  • 4
  • 15
  • 19