我正在嘗試使用標簽從函式內的 for 回圈中清除我以網格格式列印的第二列資訊。我的目標是當我單擊“生成”按鈕時,舊的輸出將被清除。
這是代碼:
root = Tk()
root.geometry("700x700")
root.title("NPC Maker")
root.config(bg="light grey")
#Define details label
details_label = Label(root)
npc = {}
#Set NPC Properties list
npc_properties = ["Race",
"Sex",
"Name",
"Height",
"Weight",
"Hair Style",
"Hair Color",
"Face",
"Eye Color",
"Skin Tone",
"Voice Tone"]
#Print NPC properties as row headers
for index, row in enumerate(npc_properties):
properties_label = Label(root, text = row ":", font=("Aerial 12 bold"))
properties_label.grid(row=index 1, column=0, sticky=W, pady=18)
#Generate funciton
def generate():
state = True
while state == True:
if sex_choice.get() == "Male":
if race_choice.get() == "Dwarf":
npc = choose_dwarf_male()
elif race_choice.get() == "Elf":
npc = choose_elf_male()
elif race_choice.get() == "Gnome":
npc = choose_gnome_male()
elif race_choice.get() == "Halfling":
npc = choose_halfling_male()
elif race_choice.get() == "Human":
npc = choose_human_male()
elif sex_choice.get() == "Female":
if race_choice.get() == "Dwarf":
npc = choose_dwarf_female()
elif race_choice.get() == "Elf":
npc = choose_elf_female()
elif race_choice.get() == "Gnome":
npc = choose_gnome_female()
elif race_choice.get() == "Halfling":
npc = choose_halfling_female()
elif race_choice.get() == "Human":
npc = choose_human_female()
else:
pass
for index,(key, value) in enumerate(npc.items()):
details_label = Label(root, text = value, font=("Aerial 12 bold"))
details_label.grid(row=index 1, column=1, sticky=W, pady=18)
state = False
details_label.destroy()
return npc
#Choose Sex of NPC
sex_options = ["Male", "Female"]
sex_choice = StringVar()
sex_choice.set("Choose Sex")
#Choose Race of NPC
race_options = ["Dwarf", "Elf", "Gnome", "Halfling", "Human"]
race_choice = StringVar()
race_choice.set("Choose Race")
#Sex dropdown menu
sex_dropdown_menu = OptionMenu(root, sex_choice, *sex_options).grid(row=0, column=0, pady=15, padx=5)
#Race dropdown menu
race_dropdown_menu = OptionMenu(root, race_choice, *race_options).grid(row=0, column=1, pady=15, padx=5, sticky=W)
#Generate NPC Button
generate_button = Button(root, text="Generate NPC", command=generate).grid(row=0, column=2, pady=15, padx=5, sticky=W)
root.mainloop()
如您所見,我嘗試執行一個 while 回圈并結束狀態并破壞網格輸出,但似乎不起作用。我無法在該函式中訪問該 details_label 標簽以將其清除。
uj5u.com熱心網友回復:
由于您在 for 回圈中使用相同的變數details_label,因此它僅參考最后創建的標簽。建議改為details_label串列來存盤標簽的實體。然后,您可以使用此串列在填充新標簽之前銷毀以前創建的標簽。
也不需要while回圈。
以下為修改generate():
details_label = []
npc = {}
#Generate funciton
def generate():
# need to declare npc as global, otherwise the global one will not be updated
global npc
if sex_choice.get() == "Male":
if race_choice.get() == "Dwarf":
npc = choose_dwarf_male()
elif race_choice.get() == "Elf":
npc = choose_elf_male()
elif race_choice.get() == "Gnome":
npc = choose_gnome_male()
elif race_choice.get() == "Halfling":
npc = choose_halfling_male()
elif race_choice.get() == "Human":
npc = choose_human_male()
elif sex_choice.get() == "Female":
if race_choice.get() == "Dwarf":
npc = choose_dwarf_female()
elif race_choice.get() == "Elf":
npc = choose_elf_female()
elif race_choice.get() == "Gnome":
npc = choose_gnome_female()
elif race_choice.get() == "Halfling":
npc = choose_halfling_female()
elif race_choice.get() == "Human":
npc = choose_human_female()
else:
npc = {}
# destroy current labels
for w in details_label:
w.destroy()
# clear the list
details_label.clear()
# populate new labels
for index,(key, value) in enumerate(npc.items()):
details_label.append(Label(root, text=value, font=("Aerial 12 bold")))
details_label[-1].grid(row=index 1, column=1, sticky=W, pady=18)
# since this function is triggered by a button click
# the return value will be ignored/discarded
return npc
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/488459.html
上一篇:回圈遍歷陣列并一一記錄值
下一篇:向資料框添加條件列
