【问题标题】:Python Tkinter loop through 3 labels updating with each new list valuePython Tkinter 循环使用每个新列表值更新 3 个标签
【发布时间】:2020-12-01 02:43:20
【问题描述】:

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

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

    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 或 Empty

做了一些研究,我觉得制作列表的队列会比创建复杂的循环结构更好的方法

【问题讨论】:

    标签: python list loops tkinter label


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2019-12-20
      • 2021-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-28
      • 2018-06-09
      相关资源
      最近更新 更多