Not able to access list in another window in tkinter python

Viewed 92
from tkinter import *
root=Tk()
root.title("")
#root.geometry("500x500")
global c
c=[]
def new():
    win=Toplevel()
    win.title("")
    for i in range(0,25):
        name=Label(win,text=str(c[i]),bg='#ADD8E6',fg='white',width=20)
        name.grid(column=1,row=i)
for i in range(1,26):
    name=Label(root,text='enter name of team'+str(i),bg='#ADD8E6',fg='white',width=20)
    name.grid(column=1,row=i)
    entry=Entry(root,bg='white',fg='black')
    entry.grid(column=2,row=i)
    c.append(entry.get())
submit=Button(root,text='Submit',bg='red',fg='white',command=new)    
submit.grid(column=1,row=26,columnspan=3)
root.mainloop()

after taking the input form different boxes in the first window , it does not put the same name in the 2nd window when required. It just sends a window whith the speciifed bgcolour

PLaese HElp

1 Answers

The reason why you are not getting the right values is that when the program is run the Entry widgets are empty by default so the list is getting no values and when the button is pressed, it. displays labels text = "" on the second window.

To fix this, get the values of Entry widgets dynamically means get the values when the button is pressed. You can store each Entry widget into a list and later access them in the new function.

In your code, you just need to change two lines.

  1. Change c.append(entry.get()) with c.append(entry).
  2. In the new function change the argument value of "text" of label name from text=str(c[i]) to text=str(c[i].get()).
Related