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

2024-10-02 08:21:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在创建一个列表,它从Tkinter文本区域获取每一行。我想将列表中的每一行附加到三个标签上,列表中的每个索引都移动到下一个标签上,当它循环回标签1时,更新列表中下一个的值

现在我有这个,但不知道如何循环回更新标签:

    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)


从长远来看,我希望发生类似的事情:

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
......

直到它到达列表的末尾

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

然后标签3将为Null或空

做了一些研究,我觉得创建一个列表队列比创建一个复杂的循环结构要好得多


Tags: inselfapplehello列表forline标签
1条回答
网友
1楼 · 发布于 2024-10-02 08:21:04

我一直在使用您对队列的想法:

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()

相关问题 更多 >

    热门问题