[英]Updating values in a list from a Tkinter entry box - Python
I am trying to update the values in a list from values that are entered into an entry box created in TKinter. 我试图从输入到TKinter中创建的输入框中的值更新列表中的值。 In my example the user enters the real name of the people in a list. 在我的示例中,用户在列表中输入人员的真实姓名。 Their real name will replace the 'x' in the example_list. 它们的真实名称将替换example_list中的“x”。
I have specified the variable as global in the method but the change only applies to the second value iterated in the list - the first is the initialised value - 99 in the code below. 我在方法中将变量指定为全局变量,但更改仅适用于列表中迭代的第二个值 - 第一个值是下面代码中的初始化值 - 99。
I have also tried to specify a lambda function that updates i[1] individually, however this does not work - bad syntax. 我还尝试指定一个单独更新i [1]的lambda函数,但这不起作用 - 语法错误。 Besides the quit() function seems to be the only way to continue the iteration. 除了quit()函数似乎是继续迭代的唯一方法。
Is there a way to do this cleanly and with the first item in the list updated? 有没有办法干净地执行此操作并更新列表中的第一项?
from Tkinter import *
example_list = [['Jimmy Bob','x','78'],[" Bobby Jim",'x','45'] ,["Sammy Jim Bob",'x','67'] ] #Nickname/Real Name/Age
newValue = 99
def replace():
global newValue
newValue = e1.get()
print("Their real name is %s" %(e1.get()))
#return(newValue)
win.quit()
root = Tk()
for i in example_list:
win = Toplevel(root)
#win.lift()
e1 = Entry(win)
e1.grid(row=1, column=0)
var = StringVar()
var.set(i[0])
Label(win, textvariable = var).grid(row=0, column=0)
Button(win, text='Enter Real Name', command=replace).grid(row=2, column=0, pady=4)
#Button(win, text='Enter Real Name', command=lambda: i[1] =replace()).grid(row=2, column=0, pady=4)
i[1] = newValue
win.mainloop( )
root.mainloop()
for i in example_list:
print(i)
Thanks to Wayne Werner who directed me towards the tksimpledialog, the solution to the problem is here. 感谢Wayne Werner指导我走向tksimpledialog,问题的解决方案就在这里。
from Tkinter import *
import tkSimpleDialog
example_list = [['Jimmy Bob','x','78'],[" Bobby Jim",'x','45'] ,["Sammy Jim Bob",'x','67'] ]
root = Tk()
root.geometry("400x400")
Label(root, text = "Enter the names in the dialog").grid(row=0, column=0)
for i in example_list:
root.lower()
i[1] = tkSimpleDialog.askstring('Enter their real name', 'What is %s real name' %i[0])
print(i[1])
root.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.