【发布时间】:2018-10-03 09:03:35
【问题描述】:
我需要在“ventana2”中表示在“ventana”中输入的对,以便在密钥为新时出现一个新帧。当键已经存在于字典中时,我需要更改先前为该键创建的框架中的旧值(新值是添加新旧值)。 我无法通过密钥获得与我的字典伙伴永久相关的帧。 非常感谢你,对不起我的英语。 以下是代码摘要:
import tkinter as tk
ventana = tk.Tk()
ventana2 = tk.Tk()
name = tk.StringVar()
tk.Entry(ventana, textvariable=name, width=30).grid(row=0, column=1)
tk.Label(ventana, text = 'Nombre').grid(row=0, column=0)
value = tk.StringVar()
tk.Entry(ventana, textvariable=value, width=30).grid(row=1, column=1)
tk.Label(ventana, text = 'Celular').grid(row=1, column=0)
contactos={}
def intro():
nom = name.get()
if nom in contactos:
cel = contactos[nom] + float(value.get())
contactos[nom] = cel
else:
cel = float(value.get())
contactos[nom] = cel
create_widget()
def create_widget():
frame = tk.Frame(ventana2)
frame.pack()
nomb = tk.Label(frame, text=name.get()).pack(side=tk.LEFT)
telf = tk.Label(frame, text=contactos[name.get()]).pack(side=tk.RIGHT)
intro_btn = tk.Button(ventana, text='Intro', command = intro)
intro_btn.grid(row=2, column=0, columnspan=2, sticky = 'ew')
ventana.mainloop()
【问题讨论】:
-
永远不要同时创建多个
Tk实例。 -
在我的真实代码中,不是这样的。在这里我这样做只是为了轻松表示它。
标签: python dictionary tkinter widget