I am trying to update the values in a list from values that are entered into an entry box created in 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.
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.
I have also tried to specify a lambda function that updates i[1] individually, however this does not work - bad syntax. Besides the quit() function seems to be the only way to continue the iteration.
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.
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()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.