简体   繁体   中英

How do I make my button know what`s in the entry box and then print that value in a new window?

I want after you write something in the entry box and then you press the button a new window to pop up and the number that was written in the entry box to be printed in the new window as " Your height is: "value" but after many tries I still don`t understand how to do it.

my code:

import tkinter as tk
root = tk.Tk()
root.geometry("250x130")
root.resizable(False, False)

lbl = tk.Label(root, text="Input your height", font="Segoe, 11").place(x=8, y=52)
entry = tk.Entry(root,width=15).place(x=130, y=55)
btn1 = tk.Button(root, text="Enter", width=12, height=1).place(x=130, y=85) #command=entrytxt1

root.mainloop()

This is what I got:

import tkinter as tk

root = tk.Tk()
root.resizable(False, False)

def callback():
    # Create a new window
    new_window = tk.Toplevel()
    new_window.resizable(False, False)

    # `entry.get()` gets the user input
    new_window_lbl = tk.Label(new_window, text="You chose: "+entry.get())
    new_window_lbl.pack()

    # `new_window.destroy` destroys the new window
    new_window_btn = tk.Button(new_window, text="Close", command=new_window.destroy)
    new_window_btn.pack()

lbl = tk.Label(root, text="Input your height", font="Segoe, 11")
lbl.grid(row=1, column=1)

entry = tk.Entry(root, width=15)
entry.grid(row=1, column=2)

btn1 = tk.Button(root, text="Enter", command=callback)
btn1.grid(row=1, column=3)

root.mainloop()

Basically when the button is clicked it calls the function named callback . It creates a new window, gets the user's input ( entry.get() ) and puts it in a label.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM