在tkinter中配置标签时,如何区分标签?

时间:2018-09-11 02:39:22

标签: python tkinter label configure

基本上,我试图制作一个每秒更新一次的变量列表,但我只能获取最后一个要更新的标签。我对tkinter不太熟悉,没有任何帮助。我认为主要的问题是我已经获得了定义,但是我不知道其他任何方式,如果有人可以帮助我解决我的问题,或者甚至帮助检修该程序,那将非常感激。 / p>

package.config

2 个答案:

答案 0 :(得分:1)

您可以使用字典来存储所有标签,因为字典允许键和值之间的映射。可能会为您显示的示例:

self.labels = {} #Creates an empty dictionary
self.labels["points"] = Label(master, text=Points, anchor='w')
self.labels["points"].pack.pack(fill='both', padx=10)
self.labels["Item1"] = Label(master, text=Item1, anchor='w')
self.labels["Item1"].pack(fill='both', padx=10)
#.... rest of labels here

或者,您可以使用列表来存储所有标签,并使用索引访问每个标签。这样,您不必在创建每个标签后手动打包它们:

self.labels = []
self.labels.append(Label(master, text=Points, anchor='w'))
self.labels.append(Label(master, text=Item1, anchor='w'))
self.labels.append(Label(master, text=Item2, anchor='w'))
#.... rest of labels here

for label in self.labels:
    label.pack(fill='both', padx=10)

最后,您可以为标签指定不同的名称。这可能是最清晰,最直接的选择:

self.points_label = Label(master, text=Points, anchor='w')
self.Item1_label = Label(master, text=Item1, anchor='w')
self.Item2_label = Label(master, text=Item2, anchor='w')
self.Item3_label = Label(master, text=Item3, anchor='w')
self.Item1Cost_label = Label(master, text=Item1Cost, anchor='w')
#.... rest of labels here. Don't forget to pack each one

请记住:标识符名称可以是您想要的任何名称(不要必须是self.labeltkinter),只要它们是:

  • 不要以数字开头
  • 仅包含字母,数字和_'s
  • 不保留python关键字/函数(尽管有可能,建议不要重写函数。)

答案 1 :(得分:0)

这里是一个使用几个列表来管理变量并更新它们的示例。

这使我们可以使用更少的代码来轻松阅读正在做的事情。

import textwrap
import tkinter as tk
from pathlib import Path

print(textwrap.fill("Please enter the name that you entered for the main document. It must have the exact same characters and is case sensitive.", 70))
Name = input()
file_name = "./{}.txt".format(Name)

if Path(file_name).exists() == True:

    class MyFirstGUI(tk.Tk):
        def __init__(self, fn):
            tk.Tk.__init__(self)
            self.title("Menu")
            self.file_name = fn
            self.var_list = []
            self.lbl_list = []

            with open(self.file_name, "r") as file:
                read = file.read().splitlines()
                self.var_list = [read[0], read[1], read[2], read[3], 1 + int(read[3]),
                                int(read[1])*1 + int(read[2])*5, read[6], read[7], read[8]]

            with open(self.file_name, "w") as file:
                for lst in self.var_list:
                    file.write("{}\n".format(lst))

            for i in range(9):
                self.lbl_list.append(tk.Label(self, text=self.var_list[i], anchor="w"))
                self.lbl_list[i].pack(fill='both', padx=10)

            self.task()

        def task(self):
            with open(self.file_name, "r") as file:
                read = file.read().splitlines()
                self.var_list = [read[0], read[1], read[2], read[3], 1 + int(read[3]),
                                int(read[1])*1 + int(read[2])*5, read[6], read[7], read[8]]

            self.var_list[0] = int(self.var_list[0]) + 1 # added this to visuallize some active change.

            with open(self.file_name, "w") as file:
                for lst in self.var_list:
                    file.write("{}\n".format(lst))

            for i in range(9):
                self.lbl_list[i].config(text=self.var_list[i])

            self.after(1000, self.task)


    app = MyFirstGUI(file_name)
    app.mainloop()

else:
    print(textwrap.fill("You didn't enter a valid name, please try again.", 70))