Home > other >  How to access Entry field value in root window from Toplevel window?
How to access Entry field value in root window from Toplevel window?

Time:01-17

I have 2 different files. One has the root window code and the other has the top-level window code in a function that is called by a button in the root file.


I want to insert in the entry_take entry field in the top-level window the value I put in the email_box field in the root window but am unable to do so. (I tried importing the email_box field in the second file but circular import error shows up and I cannot come up with anything else to get the value from email_box)

First file code:

from tkinter import *
from elt2 import window2

root=Tk()

email_box=Entry(root,width=35,borderwidth=2)
email_box.grid(row=0,column=1,pady=(5,0),padx=10)
pass_box=Entry(root,width=35,borderwidth=2)
pass_box.grid(row=1,column=1)

def submitpass():
    if pass_box.get()=="test":
        passshow=Label(root,text=" CORRECT ",background="#2E0063",fg='white')
        passshow.grid(row=3,column=0,columnspan=2,padx=8)
        window2()
    else:
        passshow=Label(root,text="INCORRECT",background="#2E0063",fg='white')
        passshow.grid(row=3,column=0,columnspan=2,padx=8)

checkbtn=Button(root,text="CHECK",command=submitpass)
checkbtn.grid(row=2,column=1,columnspan=2,padx=10,pady=5,ipadx=45)

root.mainloop()

Second file code:

from tkinter import *

def window2():
    root2=Toplevel()
    root2.geometry("250x75")
    entry_take=Entry(root2)
    entry_take.pack()
    root2.mainloop()

CodePudding user response:

As noted by Menno above, adjusting the window 2 function to pass email_box.get() allows the value to be passed between the windows.

First file code:

from tkinter import *
from elt2 import window2

root = Tk()

email_box = Entry(root, width=35, borderwidth=2)
email_box.grid(row=0, column=1, pady=(5, 0), padx=10)
pass_box = Entry(root, width=35, borderwidth=2)
pass_box.grid(row=1, column=1)


def submitpass():
    if pass_box.get() == "test":
        passshow = Label(root, text=" CORRECT ", background="#2E0063", fg='white')
        passshow.grid(row=3, column=0, columnspan=2, padx=8)
        window2(email_box.get())
    else:
        passshow = Label(root, text="INCORRECT", background="#2E0063", fg='white')
        passshow.grid(row=3, column=0, columnspan=2, padx=8)


checkbtn = Button(root, text="CHECK", command=submitpass)
checkbtn.grid(row=2, column=1, columnspan=2, padx=10, pady=5, ipadx=45)


root.mainloop()

Second file code:

from tkinter import *


def window2(entry_value):
    root2 = Toplevel()
    root2.geometry("250x75")
    entry_take = Entry(root2)
    entry_take.insert(0, entry_value)
    entry_take.pack()
    root2.mainloop()
  •  Tags:  
  • Related