为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕的地8271018 2021-06-08 13:01:50
基本上,我正在尝试制作一个每秒更新自己的变量列表,但我只能获取要更新的最后一个标签。我对 tkinter 不太熟悉,没有任何帮助。我认为主要问题是我已经得到了它,但我不知道任何其他方式,如果有人可以帮助我解决我的问题,甚至帮助检修程序,那将非常感激。import timeimport textwrapfrom tkinter import Tk, Label, Buttonfrom pathlib import Pathwhile True:    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()    FileName = Name+".txt"    P = Path(FileName)    if P.exists():        class MyFirstGUI:            def __init__(self, master):                with open(FileName, "r") as file:                    global Points                    global Item1                    global Item2                    global Item3                    global PPC                    global PPS                    global Item1Cost                    global Item2Cost                    global Item3Cost                    read = file.read().splitlines()                    Points = read[0]                    Item1 = read[1]                    Item2 = read[2]                    Item3 = read[3]                    PPC = 1 + int(Item3)                    PPS = int(Item1)*1 + int(Item2)*5                    Item1Cost = read[6]                    Item2Cost = read[7]                    Item3Cost = read[8]                    Points = int(Points) + int(PPS)                VarList = [str(Points), str(Item1), str(Item2), str(Item3), str(PPC), str(PPS), str(Item1Cost), str(Item2Cost), str(Item3Cost)]                with open(FileName, "w") as file:                    for List in VarList:                        file.write(List+'\n')                root = Tk()                self.master = master                master.title("Menu")                self.label = Label(master, text=Points, anchor='w')                self.label.pack(fill='both', padx=10)                self.label = Label(master, text=Item1, anchor='w')                self.label.pack(fill='both', padx=10)
查看完整描述

2 回答

?
慕勒3428872

TA贡献1848条经验 获得超6个赞

您可以使用字典来存储所有标签,因为字典允许键和值之间的映射。这对您来说可能是什么样子的示例:


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.label用tkinter),只是,只要他们:

  • 不要以数字开头

  • 仅包含字母、数字和_'s

  • 不是保留的 python 关键字/函数(不建议覆盖函数,尽管它是可能的。)


查看完整回答
反对 回复 2021-06-15
  • 2 回答
  • 0 关注
  • 196 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号