简体   繁体   English

Python Tkinter 循环使用每个新列表值更新的 3 个标签

[英]Python Tkinter loop through 3 labels updating with each new list value

I am creating a list that takes each line from a Tkinter textarea.我正在创建一个列表,该列表从 Tkinter 文本区域中获取每一行。 I want to append each line in the list to three labels,with each index in the list moving to the next label, updating the value that is next in the list when it loops back to label one.我想 append 列表中的每一行到三个标签,列表中的每个索引移动到下一个 label,当它循环回 ZD304BA20E96D8741Z1588EEABAC850E3 时更新列表中的下一个值。

Right now I have this but don't know how to loop back to update the labels:现在我有这个但不知道如何循环更新标签:

    def iterate_linesRest(self):
        for line in self.textarea.get('1.0', 'end-1c').splitlines():
            # Iterate lines
            if line:
                MainFrame.pipelinelist4.append(line)
            labels=[]
            for x in MainFrame.pipelinelist4[]:
                label = Label(self,text =x)
                labels.append(label)


To put it in perspective I want something like this to occur:为了正确看待它,我希望发生这样的事情:

pipelinelist = ["Hello", "Hi", "Apple", "John", "Mike", "Joe"]
Label 1 = Hello         Label2 = Null,    Label 3 = Null

Label 1 = Hi           Label2 = Hello    Label 3 = Null

Label 1 = Apple        Label 2 = Hi      Label 3 = Hello
Label 1 = Mike          Label2 = Apple     Label 3 = Hi
......

until it reaches the end of the list直到它到达列表的末尾

Label 1 = Null           Label 2 = Null    Label 3 = Joe

And then Label 3 would be Null or Empty然后 Label 3 将是 Null 或空

Doing some research, I feel that making a queue of the list would be a much better approach then creating a complicated loop structure做了一些研究,我觉得制作列表的队列会比创建复杂的循环结构更好的方法

I have been using your idea of the queue:我一直在使用您对队列的想法:

import tkinter as tk
from collections import deque

def main():
    app = App()
    app.mainloop()


class App(tk.Tk):
    def __init__(self):
        super().__init__()
        self.geometry("400x400")
        tk.Button(self, text="Labels", command=self.labels).grid(column=0, row=0)

    def labels(self):
        label_names = ['I', 'am', 'a', 'list', 'of', 'placeholders']
        label_names.extend(['Null', 'Null'])
        q = deque(['Null', 'Null', 'Null'])
        for i, val in enumerate(label_names):
            q.pop()
            q.appendleft(val)
            for j, label in enumerate(q):
                tk.Label(self, text=str(label)).grid(column=j, row=i+1)


if __name__ == '__main__':
    main()

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

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