简体   繁体   English

Tkinter - 如何打印 label 中的列表值,每个值在不同的行上,最新值在前

[英]Tkinter - how to print list values in label with each value on different line, newest value first

I am trying to print values from list in a tkinter label.我正在尝试从 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.它应该以这种方式工作:将生成一些坐标(在下面的代码中,只写 randint - 例如示例),并且每个新值都将添加到第一个 position 上,最大值为。 10 values in list.列表中有 10 个值。 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问题是,如果我只是遍历列表并打印值 - 它可以工作 - 但如果我想通过 f-string 将它放在 label 上,它只显示一个值......我使用的循环是因为它会打印每个值在不同的线路上

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: print(x)显示了这一点:

6 6

5 5

4 4

0 0

6 6

7 7

0 0

1 1

6 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.并且label_coords.config(text=f'Current coordinates:\n{x}')在 label 上仅打印 '6' a 不再更新。

...as usual - I guess that there are some silly mistakes:) will be grateful for any hint. ...像往常一样 - 我想有一些愚蠢的错误:) 将不胜感激任何提示。

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?对应你想要什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM