【问题标题】:Tkinter - Update changes in variables in previously generated widgets, from dictionaryTkinter - 从字典更新先前生成的小部件中变量的变化
【发布时间】: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


【解决方案1】:

create_widget() 函数内创建的标签在函数范围内。函数结束后,对标签的所有引用都将丢失。所以你需要想办法保存对标签的引用(建议返回语句)并将它们关联到联系人字典。

更新 - 将框架与值相关联

保存对您想要记住的对象的引用以及您想要用来在列表(或字典或元组等)中调用它的名称。然后将所有这些附加到您的全局小部件列表中。例如:

nomb = tk.Label( ... )
widget_parameters = ['Nomb Label', nomb]
global_widget_list.append(widget_parameters)

然后您可以在global_widget_list 中搜索您已命名的任何小部件并获得对该小部件的引用。

我已经包含了一些示例代码来说明您可以实现的一种方法。玩弄它直到你理解它,你将能够在你自己的应用程序中实现它。

from tkinter import *
import time

root = Tk()
root.geometry('300x200')

global_widget_list = []    # List for holding all widgets

def make_label():   # Create label
    text_label = Label(root,text='Text input')
    text_label.pack()
    global_widget_list.append(['Text Label',text_label]) # Add widget to list

def make_input():   # Create entry
    inputvar = StringVar()
    text_input = Entry(root,width=20,textvariable=inputvar)
    text_input.pack()
    global_widget_list.append(['Text Input',text_input,inputvar]) # Add widget to list

make_label()    # Run functions to cretae GUI
make_input()
root.update()   # Take care of updating GUI
time.sleep(3)   # Wait so you have time to see the original, then change it

# Now, loop through all widgets and do what you want
for widget in global_widget_list:
    widget_name = widget[0]
    widget_id = widget[1]
    print(widget_name, 'has identity', widget_id)

    if widget_name == 'Text Label':
        print('Changing test label text to: ALL CAPS')
        widget_id.configure(text='ALL CAPS')

    if widget_name == 'Text Input':
        print('Changing text in entry to: SAMPLE')
        var = widget[2]
        var.set('SAMPLE')

root.update()   # Take care of updating GUI
time.sleep(3)   # Wait so you have time to see the changes, then change it

print('Removing Entry from application GUI')
global_widget_list[1][1].pack_forget() # Remove entry form GUI

【讨论】:

  • 你知道是否可以将框架与值相关联吗?你会怎么做?
  • 太棒了!谢谢!但是当我尝试运行它时,python 崩溃了……你知道为什么会发生吗?
  • 您在运行我的示例中的确切代码吗?您收到任何错误消息吗?否则,您可以尝试通过仅从导入语句开始并逐个添加程序的其余部分来查找问题,然后查看它何时崩溃。这会让你开始调试
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-08
  • 2021-01-18
  • 2023-01-04
  • 2014-03-08
  • 2017-03-02
  • 2011-02-05
相关资源
最近更新 更多