I can't run your large program - probably you have wrong indentions.
I see two problems:
- program should have only one
mainloop()
- and it can be your problem.
- we use
Tk()
to create main window, and Toplevel()
to create other windows.
Besides you use name getname
as function name and as second window instance so it is very misleading.
I create global var_name
to keep name and later I use it inside function.
from Tkinter import *
def get_name(usertype):
win = Toplevel()
win.title("Get name popup")
f = LabelFrame(win, text = "Your name:")
f.pack()
# use global `name` which was created outside function
e = Entry(win, textvariable=var_name)
e.pack(side="right")
e.bind("<Return>", lambda event: win.destroy())
b = Button(win, text = "Cancel", command=win.destroy)
b.pack()
# --- main --
root = Tk()
root.title("Ask-name-SUB")
# global variable
var_name = StringVar()
b = Button(root, text="Enter your name", command=lambda: get_name("testuser"))
b.pack()
b = Button(root, text="Cancel", command=root.destroy)
b.pack()
root.mainloop()
# --- after --
name = var_name.get()
print "Print name, its length, its type"
print name, len(name), type(name)
EDIT:
To makes popup window more universal you can use arguments - displayed text and variable for result.
def get_value(text, variable):
and then you can use it with different text and different variable - ie. for name or for address.
from Tkinter import *
def get_value(text, variable):
win = Toplevel()
win.title("Get value")
f = LabelFrame(win, text=text)
f.pack()
e = Entry(win, textvariable=variable)
e.pack(side="right")
e.bind("<Return>", lambda event: win.destroy())
b = Button(win, text = "Cancel", command=win.destroy)
b.pack()
# --- main --
root = Tk()
root.title("Ask-name-SUB")
# global variables
var_name = StringVar()
var_address = StringVar()
b = Button(root, text="Enter your name", command=lambda: get_value("Your name:", var_name))
b.pack()
b = Button(root, text="Enter your address", command=lambda: get_value("Your address:", var_address))
b.pack()
b = Button(root, text="Cancel", command=root.destroy)
b.pack()
root.mainloop()
# --- after --
name = var_name.get()
print "Print name, its length, its type"
print name, len(name), type(name)
address = var_address.get()
print "Print address, its length, its type"
print address, len(address), type(address)