Varun Singh
Varun Singh

Reputation: 73

Unable to store entered Input in Multiple Window in Tkinter

I have created multiple windows and i want to print and store data entered in one of the TopLevel windows but it not able to store and print the data. The same thing when Iam doing without multiple windows,Iam able to do that. What can be wrong here, let me know.

from tkinter import *


def Read():
    name = namevalue.get()
    print('Name:',name)
    with open('attendance_data/detail.csv','a+') as f:
        f.write(name)

def New():
    top1 = Toplevel()

    top1.geometry('500x500')
    top1.resizable(False,False)
    top1.title('Existing Employee Details')
    
    l1 = Label(top1,text='New Employee Registeration',font='comicsans 14 bold',padx=10).grid(row = 0,column=3,pady=50)

    name = Label(top1,text='Name',padx=20)
    name.grid(row=1,column=2)
    namevalue = StringVar()
    nameEntry = Entry(top1,textvariable=namevalue).grid(row=1,column=3,pady=25)
    Button(top1,text='Submit',command=Read).grid(row=4,column=3,pady=25)  # command

    top1.mainloop()  

root = Tk()

root.geometry('500x500')
root.resizable(False,False)
root.title('Main Window')

l2 = Label(root,text='New Employee Registeration',font='comicsans 14 bold',padx=10).grid(row = 0,column=2,pady=50,padx=50)
b1 = Button(text='New Employee',bg='black',fg='red',font='comicsansms 12 bold',command=New).grid(row=10,column=2,pady=50)
b2 = Button(text='Existing Employee',bg='black',fg='red',font= 'comicsansms 12 bold').grid(row = 11,column=2,pady=50)

root.mainloop()

I am able to print and store the entered data when iam not using Mutiple Windows

from tkinter import *

def Read():
    name = namevalue.get()
    print('Name:',name)
    with open('attendance_data/detail.csv','a+') as f:
        f.write(name)

root = Tk()

root.geometry('500x500')
root.resizable(False,False)
root.title('Main Window')

name = Label(root,text='Name',padx=20)
name.grid(row=1,column=2)
namevalue = StringVar()
nameEntry = Entry(root,textvariable=namevalue).grid(row=1,column=3,pady=25)
Button(root,text='Submit',command=Read).grid(row=4,column=3,pady=25)  # command

root.mainloop()

Help me in this.

Upvotes: 0

Views: 98

Answers (1)

Delrius Euphoria
Delrius Euphoria

Reputation: 15098

You forgot to add a command to the second button:

b2 = Button(..., command=Read).grid(row = 11,column=2,pady=50)

Thing to note is, you don't have to store the value in variable as the value of b2 is None, so you might as well remove the variable.

Edit: You need to make the entry, namevalue, a global variable so it can be accessed outside New and inside Read and other functions.

def New():
    global namevalue
    ....

Upvotes: 1

Related Questions