首先,你的代码示例有两个问题:
1) .pack() 返回None,因此当您执行aa=Label(root,text=dict['a']).pack() 时,您将None 存储在变量aa 中,而不是标签中。你应该这样做:
aa = Label(root,text=dict['a'])
aa.pack()
2) 按钮的command 选项将函数作为参数,但您使用command=setit('a',dict,3),因此您在按钮创建时执行该函数。要将带有参数的函数传递给按钮命令,您可以使用lambda:
Button(command=lambda: setit('a',dict,3))
然后,要在字典中的值更改时更新标签,您可以将标签存储在具有相同键的字典中,并使用 label.configure(text='new value') 更改相应标签的文本:
import tkinter as tk
def setit(point, adic, label_dic, number):
adic[point] = adic[point] + number # change the value in the dictionary
label_dic[point].configure(text=adic[point]) # update the label
root = tk.Tk()
dic = {'a': 4, 'b': 8, 'c': 3}
# make a dictionary of labels with keys matching the ones of dic
labels = {key: tk.Label(root, text=dic[key]) for key in dic}
# display the labels
for label in labels.values():
label.pack()
tk.Button(command=lambda: setit('a', dic, labels, 3)).pack()
root.mainloop()