Home > Net >  Tkinter - how to print list values in label with each value on different line, newest value first
Tkinter - how to print list values in label with each value on different line, newest value first

Time:09-05

I am trying to print values from list in a tkinter label. It should work this way: there will be generated some coordinates (in code below a wrote just randint - like example) and every new value will be added on the first position with max. 10 values in list. Problem is that if I just iterate through the list and print value - it works - but if I want to place it on a label via f-string, it only shows one value... For loop I used because it will print every value on different line

list = []


def listappend():
    list.insert(0, random.randint(0, 10))
    if len(list) > 10:
        label_coords_list.pop()
        for x in list:
            print(x)
            label.config(text=f'Current coordinates:\n{x}')
    else:
        for x in list:
            print(x)
            label.config(text=f'Current coordinates:\n{x}')

test_button = Button(root, text='Test', width=13, command=listappend)
test_button.place(x=330, y=50)

print(x) shows this:

6

5

4

0

6

7

0

1

6

each number on its own line

and label_coords.config(text=f'Current coordinates:\n{x}') prints only '6' on the label a doesn not update any more.

...as usual - I guess that there are some silly mistakes :) will be grateful for any hint.

CodePudding user response:

Does replacing :

        for x in list:
            print(x)
            label.config(text=f'Current coordinates:\n{x}')

with

        message = ''
        for x in list:
            print(x)
            message  = '\n'   str(x)
        label.config(text=f'Current coordinates:{message}')
        label.pack()

correspond to what you want ?

  • Related