Search code examples
pythonpython-3.xtkintertkinter-entry

How to get a value entered in Top level widget in tkinter and use it in main window to display?


I'm new to Python GUI and trying to learn using tkinter, I have created a main window on clicking the button "Continue" in the main window, a top level window opens and a number can be entered in the entry box, I'm not able to read the value in main window. Can anybody help me with how to read the value entered in the top level window and enter the same in the main window

from tkinter import *


# global variable
blank_2 = [] 
blank_1 = []

# Function to create a top level window with entry boxes
def open_new_window():
    # Toplevel object which will be treated as a new window
    popup = Toplevel(main)
    global blank_2
    global blank_1
    # sets the title of the Toplevel widget
    popup.title("Enter the value")

    # sets the geometry of toplevel
    popup.geometry("480x220")
    popup.geometry("+500+250")
    popup.configure(background='grey')
    PL = Label(popup, text="Enter the 1st value :", fg="white", bg="grey",
               font=('century gothic', 12))
    PL.place(x=20, y=20)
    GW = Label(popup, text="Enter the 2nd value :", fg="white", bg="grey",
               font=('century gothic', 12))
    GW.place(x=20, y=70)
    blank_2 = Entry(popup, justify=LEFT, font=12, fg="black", width=15)
    blank_2.place(x=210, y=20)
    blank_1 = Entry(popup, justify=LEFT, font=12, fg="black", width=15)
    blank_1.place(x=210, y=70)
    exitp = Button(popup, text='Quit', font=8, width=5, height=1, bd=4, command=popup.destroy)
    setp = Button(popup, text='Set', font=8, bd=4, command=sum)
    exitp.place(x=300, y=120)
    setp.place(x=240, y=120)
    blank_2.focus()

# Function to insert the value from the top level window to main window
def sum():
    global blank_2, blank_1
    LS.insert(END, blank_2)
    LS.insert(END, blank_1)


main = Tk()
main.title("TEST")                        # required text on window title
main.configure(background='grey')                   # Background color of Main window
main.geometry("+600+150")                           # Position of the window on the screen
main.geometry("400x450")                            # Size of the window

# text window to display the value in main window
LS = Text(main, bd=3, height=3, width=30, bg="light cyan")
LS.place(x=20, y=190)

# buttons to open a top level window and to quit the main window
C = Button(main, text='Continue', font=8, bd=4, command=open_new_window)
C.place(x=40, y=45)
Q = Button(main, text='Quit', font=8, width=5, height=1, bd=4, command=main.destroy)
Q.place(x=150, y=45)


mainloop()

The output in the main window


Solution

  • You need to call the get method of the Entry´s like

    LS.insert(END, blank_2.get())
    LS.insert(END, blank_1.get())
    

    since you defined blank_1 and blank_2 as Entry´s:

    blank_2 = Entry(popup, justify=LEFT, font=12, fg="black", width=15)
    blank_1 = Entry(popup, justify=LEFT, font=12, fg="black", width=15)
    

    .!toplevel.!entry

    is the reference for tkinter to the wdiget.