Search code examples
pythonpython-3.xtkintertk-toolkittkinter-entry

How to use input of Entry on another window


from tkinter import *

enter_details = Tk()
enter_details.title("Fill details here")
enter_details.geometry("500x200")
Label(enter_details,text = "Name ").grid(row = 0)
e1=Entry(enter_details)
e1.grid(row=0,column=1)
Label(enter_details,text ="Enrollment no. ").grid(row=1)
e2=Entry(enter_details)
e2.grid(row=1,column=1)
def show():
    Label(enter_details,text="Name : "+e1.get()).grid(row=4)
    Label(enter_details,text="Enrollment no. : "+e2.get()).grid(row=5)
Button(enter_details, text='Check',command=show).grid(row=3,column=0)
Button(enter_details, text='Done',command=enter_details.destroy).grid(row=3,column=1)
enter_details.mainloop()

marksheet = Tk()
marksheet.title("Third sem marksheet")
marksheet.geometry("720x900")

clg_name= Label(marksheet)
clg_name.config(text='TECHNOCRATS INSTITUTE OF TECHNOLOGY')
clg_name.config(font=('times',20,'bold'))
clg_name.config(fg='red')
clg_name.pack()
show_name=Label(marksheet)
show_name.config(text=e1.get())
show_name.pack()
b1=Button(marksheet, text='Done',command=marksheet.destroy)
b1.pack(expand=YES)
marksheet.mainloop()

I want to print e1 entry inputed in enter_details window in marksheet window but I am not able to do this. I tried finding solution but failed so I am there.

from tkinter import *
global e1_input
enter_details = Tk()
enter_details.title("Fill details here")
enter_details.geometry("500x700")
Label(enter_details,text = "Name :").grid(row = 0)
e1=Entry(enter_details)
e1.grid(row=0,column=1)
e1_input = e1.get()
Label(enter_details,text ="Enrollment no. :").grid(row=1)
e2=Entry(enter_details)
e2.grid(row=1,column=1)
Label(enter_details,text = "Father's name :").grid(row=2)
e3=Entry(enter_details)
e3.grid(row=2,column=1)
Label(enter_details,text = "Previous CGPA :").grid(row=3)
e4=Entry(enter_details)
e4.grid(row = 3,column=1)
Label(enter_details,text = "Semester :").grid(row=4)
e5=Entry(enter_details)
e5.grid(row = 4,column=1)


def show():
    Label(enter_details,text="Name : "+e1.get()).grid(row=6)
    Label(enter_details,text="Enrollment no. : "+e2.get()).grid(row=7)
    Label(enter_details,text="Father's name : "+e3.get()).grid(row=8)
    Label(enter_details,text="previous CGPA : "+e4.get()).grid(row=9)
    Label(enter_details,text="Semester : "+e5.get()).grid(row=10)
Button(enter_details, text='Check',command=show).grid(row=5,column=0)
Button(enter_details, text='Done',command=enter_details.destroy).grid(row=5,column=1)
enter_details.mainloop()


marksheet = Tk()
marksheet.title("Third sem marksheet")
marksheet.geometry("720x900")

clg_name= Label(marksheet)
clg_name.config(text='TECHNOCRATS INSTITUTE OF TECHNOLOGY')
clg_name.config(font=('times',20,'bold'))
clg_name.config(fg='red')
clg_name.pack()
show_name=Label(marksheet)
show_name.config(textvariable=e1_input)
show_name.pack()
b1=Button(marksheet, text='Done',command=marksheet.destroy)
b1.pack(expand=YES)


marksheet.mainloop()

I edited my previous code but still it isn't working as nothing is showing on marksheet windows while trying to print value stored in e1_input global variable. please let me know if you know how it should be done .


Solution

  • When the program quits the first mainloop, the widgets are destroyed and you can't access them anymore. You can save the input value in e1 in a variable and access it later in the second mainloop. This needs to be done before enter_details.destroy.

    In practical terms, you can define a function enter_details_quit:

    def enter_details_quit():
        global e1_input
        e1_input = e1.get()
        enter_details.destroy()
    

    and assign it to the command of your 'Done' button:

    Button(enter_details, text='Done',command=enter_details_quit).grid(row=3,column=1)
    

    If you want to avoid globals, you can think of using a class construction to store the relevant input values.