I want to make a program where, after clicking on a button, a user gets asked for its name, after which the program continues. I'm stuck on making the popup return the textstring that has been entered in the popup. At first I thought it was my code, but the I decided to make a second program where it just asks the name, prints it, prints it length and its type. In that second program, everything works as it should. I'm having a very hard time figuring out why it doesn't in the first (larger) program. I've already read (Why is Tkinter Entry's get function returning nothing?) and, even though my .get()
function occurs after my .mainloop
, it still doesn't work; in that same thread they propose using classes, which is something I know absolutely nothing about. If possible can anyone point out what I'm missing in my larger program?
Large Program
from Tkinter import *
root = Tk()
root.title("Ask-name-SUB")
def getname(usertype):
getname = Tk()
getname.title("Get name popup")
def abort():
getname.destroy()
name = StringVar()
c = LabelFrame(getname, text = "Your name:")
c.pack()
d = Entry(getname, textvariable=name)
d.pack(side="right")
d.bind("<Return>", lambda event: getname.destroy())
e = Button(getname, text = "Cancel", command=lambda: abort())
e.pack()
getname.mainloop()
name = (name.get())
print "Print name, its length, its type"
print name
print len(name)
print type(name)
top = Frame(root)
top.pack(side="top")
bottom = Frame(root)
bottom.pack(side="bottom")
def killit():
root.destroy()
cancel = Button (bottom, text = "Cancel", command=lambda: killit())
cancel.pack()
askname = Button (top, text = "Enter your name", command=lambda: getname("testuser"))
askname.pack()
root.mainloop()
Small Program
from Tkinter import *
def getname(usertype):
getname = Tk()
getname.title("Get name popup")
def abort():
getname.destroy()
name = StringVar()
c = LabelFrame(getname, text = "Your name:")
c.pack()
d = Entry(getname, textvariable=name)
d.pack(side="right")
d.bind("<Return>", lambda event: getname.destroy())
e = Button(getname, text = "Cancel", command=lambda: abort())
e.pack()
getname.mainloop()
name = (name.get())
print "Print name, its length, its type"
print name
print len(name)
print type(name)
getname("testuser")