2

How do I connect the submit button to another "window" so the information entered in tk.Entry() is entered in a specified label on a 2nd "window". I want the "submit" button to be what pulls up the 2nd window "anchored" in the main window from that I will have pointed the information in the entry to the label in the 2nd popup window.


import tkinter as tk

#function that prints the entry
def entry_print(widget):
    name = widget.get()
    print(name)

#Setup for the window
window = tk.Tk()
window.title("Title_Name")
window.geometry("250x250")

#widgets
label = tk.Label(window, text = "Manual:", bg = "dark grey", fg = "white")
entry = tk.Entry()
button = tk.Button(text = "submit",
                   command = lambda e = entry: entry_print(e))

#widget placement
label.place(x=0,y=20)
entry.place(x=52,y=21)
button.place(x=177, y=18)

window.mainloop()

2 Answers 2

2

You can create additonal windows via Toplevel widgets. You can refer to the current contents of the Entry widget when creating the Label in the second one by creating a tkinter StringVar and setting the textvariable= option of both the Entry widget and the Label widget on the second window to it.

import tkinter as tk

#Setup for the main window
window = tk.Tk()
window.title("Title_Name")
window.geometry("250x250")

def new_window():
    new_win = tk.Toplevel()
    tk.Label(new_win, textvariable=entered_text).pack()

#widgets
label = tk.Label(window, text="Manual:", bg="dark grey", fg="white")
entered_text = tk.StringVar(value='')  # ADDED
entry = tk.Entry(textvariable=entered_text)
button = tk.Button(text="submit", command=new_window)

#widget placement
label.place(x=0,y=20)
entry.place(x=52,y=21)
button.place(x=177, y=18)

window.mainloop()

Here's a screenshot of it running:

screenshot of it running

Sign up to request clarification or add additional context in comments.

1 Comment

No worries…you're well on your way to gaining the rep.
0

Here is my code. It is much more simple comparitively.

import tkinter as tk

#function that prints the entry
def entry_function(e=None):
    name = entry.get()
    win = tk.Toplevel()
    tk.Label(win, text=name).pack()

#Setup for the window
window = tk.Tk()
window.title("Title_Name")
window.geometry("250x250")

#widgets
label = tk.Label(window, text = "Manual:", bg = "dark grey", fg = "white")
entry = tk.Entry()
button = tk.Button(text = "submit",
                   command = entry_function)

#widget placement
label.place(x=0,y=20)
entry.place(x=52,y=21)
button.place(x=177, y=18)

window.mainloop()

As a bonus, you can make the button such that when you press enter key after writing the name, you can automatically run the entry_funtion function using the code:

window.bind('<Return>', entry_function)

1 Comment

I was curious what would I add/edit to make the entry(s) show up in the same window preferably "in line" .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.