Search code examples
python-3.xtkintertkinter-entry

I can't use the text from my Entry widget


There is something in my code that isn't letting me use the text from my Entry widget. I want to use the text a user will enter in my Entry widget, which i've called "textentry", in another part of my code but it doesn't seem to be storing it in a way i can use. In this example i'm just trying to print what is entered into the terminal.

I can get it to print if i uncomment the "print(textentry.get())" in my function.

As it is now i get ".!entry" printed in the terminal. i'm not sure I follow that output either.

I feel like it's probably something simple but i've been struggling a while and many different approaches but still not success.

import tkinter as tk
from tkinter import *

def click():
    textentry.get()
#    print(textentry.get())
    Text_input_window.destroy()

Text_input_window= Tk()
Label (Text_input_window,text="Enter search word:", bg="black", fg="white").grid(row=1, column=0, sticky=W)
textentry = tk.Entry(Text_input_window, width=20, bg="white")
textentry.grid(row=2, column=0, sticky=W)
Button(Text_input_window, text="SUBMIT", width=6, command=click).grid(row=3, column=0, sticky=W)
Text_input_window.mainloop()

print(textentry)

Solution

  • Try it:

    import tkinter as tk
    from tkinter import *
    
    Text_input_window = Tk()
    textentry = StringVar()
    
    def click():
        global textentry
        textentry = textentry_ent.get()
    #    print(textentry.get())
        Text_input_window.destroy()
    Label(Text_input_window, text="Enter search word:",
          bg="black", fg="white").grid(row=1, column=0, sticky=W)
    textentry_ent = tk.Entry(Text_input_window, textvariable=textentry,width=20, bg="white")
    textentry_ent.grid(row=2, column=0, sticky=W)
    Button(Text_input_window, text="SUBMIT", width=6,
           command=click).grid(row=3, column=0, sticky=W)
    
    Text_input_window.mainloop()
    
    print(textentry)