亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 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

請記?。簶俗R符名稱可以是任何你想要的(他們不都只是self.label用tkinter),只是,只要他們:

  • 不要以數字開頭

  • 僅包含字母、數字和_'s

  • 不是保留的 python 關鍵字/函數(不建議覆蓋函數,盡管它是可能的。)


查看完整回答
反對 回復 2021-06-15
  • 2 回答
  • 0 關注
  • 210 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號