On the Forum there has recently been posted a Question by @Clueless_captain; it was answered by @furas. I am new to stackoverflow so I can not comment in that Conversation. This is the URL to that Conversation: (Tkinter Entry widget stays empty in larger programs (Python 2)). The Code posted by furas is not exactly what I try to do, but the only Thing I can not do myself is finding a way to re-use the Input that has been given in the EntryWidget. I modified the Code written by furas; my Goal was that the Input would be printed before the GUI terminated. For that I bound the Return key to a new Function, this Function was supposed to get the Textstring in this new Function where it would further be processed. It does only do that when I click the Button to get a name for a second time. Is the order of this Code off? I believe the Issue is closely related to the String 'e.bind
' on line ten, but I can not find the Issue.
Best Regards, G_Lehmann
---------- The modified code:
from Tkinter import *
def get_input(text, variable):
win = Toplevel()
win.title("Get value")
f = LabelFrame(win, text=text)
f.pack()
e = Entry(win, textvariable=variable)
e.bind("<Return>", do_more(text, variable, e))
e.pack(side="right")
#I tried e.bind here, but This had no Effect.
b = Button(win, text = "Cancel", command=win.destroy)
b.pack()
#do_more is the new Function I want to use to process the Variable 'data' further.
def do_more(text, variable, e):
data = e.get()
print data
print len(data)
print type(data)
def get_value(text, variable):
get_input(text, variable)
# --- 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 -- (My Edit: I disabled this as I want to bind the Variables before my GUI gets terminated)
"""
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)
"""