user1148257
user1148257

Reputation: 237

Python / Tkinter getting text from Enter box

Apologies for the basic nature of the question. Tkinter has me totally stumped.

I'm trying to construct an application with a menu bar, one of the choices would pull up a dialog where the user enters two values and then either presses an "Enter" or "Cancel" button. Either button press should then close the window.

I can construct the main window and the 'pop-up' to enter the values, I've gone through all examples of extracting text and closing the window after the button press but I'm still coming up empty. Here is the framework I'd like to use:

 from Tkinter import *

 #
 #  Functions to perform functions selected from main window
 #

 def enter_values():
    new_window = Toplevel(root) 
    Label(new_window, text="Value 1").grid(sticky=W,row=0)
    e1=Entry(new_window,width=40).grid(row=0,column=1,sticky=W)
    Label(new_window, text="Value 2").grid(pady=20,sticky=W,row=1)
    e2=Entry(new_window,width=20).grid(row=1,column=1,pady=20,sticky=W)
    ok= Button(new_window, text="Enter",command=lambda: callback("OK")).grid(column=0,row=4,pady=30)
    cancel = Button(new_window,text="Cancel",command=lambda: callback("CANCEL")).grid(column=1,row=4,pady=30)

 def callback(button):
       if button == "OK":
            print "OK"
       elif button == "CANCEL":
            print "Cancel"
       else:
            print "no idea"

 #
 #  Following section defines the display window
 #

 root = Tk()
 root.minsize(500,200)
 root.geometry("800x300")
 root.wm_title("Some clever title here")
 menubar = Menu(root)
 filemenu = Menu(menubar, tearoff=0)
 filemenu.add_command(label="New", command=enter_values)
 filemenu.add_separator()
 filemenu.add_command(label="Exit", command=root.quit)
 menubar.add_cascade(label="File", menu=filemenu)

 root.config(menu=menubar)
 root.mainloop()

Upvotes: 1

Views: 608

Answers (1)

user2987606
user2987606

Reputation: 41

You'll have to close the window by new_window.destroy(). To get the text from entries, define variables, assign them to Entries and get values when needed. Not the best example but something like this would work:

from tkinter import *

#
#  Functions to perform functions selected from main window 
#

def enter_values():
    v1 = StringVar()
    v2 = StringVar()
    new_window = Toplevel(root) 
    Label(new_window, text="Value 1").grid(sticky=W,row=0)
    Entry(new_window,textvariable=v1,width=40).grid(row=0,column=1,sticky=W)
    Label(new_window, text="Value 2").grid(pady=20,sticky=W,row=1)
    Entry(new_window,textvariable=v2,width=20).grid(row=1,column=1,pady=20,sticky=W)
    ok= Button(new_window, text="Enter",command=lambda: callback("OK",new_window,v1,v2)).grid (column=0,row=4,pady=30)
    cancel = Button(new_window,text="Cancel",command=lambda: callback("CANCEL",new_window)).grid(column=1,row=4,pady=30)


def callback(button,new_window,v1=None,v2=None):
    if button == "OK":
        print("OK")
        print(v1.get())
        print(v2.get())
    elif button == "CANCEL":
        print("Cancel")
    else:
        print("no idea")
    new_window.destroy()


#
#  Following section defines the display window
#

root = Tk()
root.minsize(500,200)
root.geometry("800x300")
root.wm_title("Some clever title here")
menubar = Menu(root)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="New", command=enter_values)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=root.quit)
menubar.add_cascade(label="File", menu=filemenu)
root.config(menu=menubar)
root.mainloop() 

Upvotes: 1

Related Questions