简体   繁体   中英

Closing a Tkinter Entry Box in Python

I am trying to create a simple popup text entry for the user in which a user enters a text and hits submit (a button). Upon clicking submit, I want the popup entry box to close off and continue on with the rest of the code. Following is a sample code for display that I borrowed from an old post here :

from Tkinter import *

root = Tk()
nameLabel = Label(root, text="Name")
ent = Entry(root, bd=5)

def getName():
    print ent.get()

submit = Button(root, text ="Submit", command = getName)

nameLabel.pack()
ent.pack()

submit.pack(side = BOTTOM) 
root.mainloop()

print "Rest of the code goes here" 

I don't have much experience with Tkinter so I am not sure where and how exactly to call the appropriate functions for closing the entry box after the user hits 'Submit'. My guess is it would have to be inside the getName() function?

If I understand you correctly, then all you need to do is call the root window's destroy method at the end of the getName function:

def getName():
    print ent.get()
    root.destroy()

Doing so is equivalent to manually clicking the X button in the corner of the window.

Alternate method:


since there isn't much to your popup you could also eliminate several lines of code in your GUI, save some CPU and get pretty much the same output with this:

submitvariablename=raw_input('Please enter a Name')

same functionality and much faster, cleaner.

Just a thought.

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