1

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Aasam Tasaddaq
  • 645
  • 4
  • 13
  • 23

2 Answers2

3

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.

0

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.

Amazingred
  • 1,007
  • 6
  • 14