Python Forum
[Tkinter] Entry widget : unable to get the text entered - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [Tkinter] Entry widget : unable to get the text entered (/thread-387.html)



Entry widget : unable to get the text entered - dlemper - Oct-08-2016

System is Windows 8.1  Python 3.5
 
The entry window appears and text can be typed, but I've been unable to get/use that text.
Have reread most websites and tried seven ways to Sunday, but nothing works.
Below is one attempt :
from tkinter import *
root = Tk()
root.geometry('600x400+80+40')

def getentry() :
    ewidget = Entry(root, width = 32)    
    ewidget.pack()
    textstring = ewidget.get()
    print("Entered  =  ",textstring)
    
def main() :
    getentry()
    mainloop()

if __name__ == '__main__' :
    main()  
 
Above prints in command line
Output:
 >>> Entered =                                 < nothing >
Surprisingly this is printed before anything is typed into Entry window.
Some sites describe use of StringVar() as below
def getentry() :

    estring = StringVar()
    ewidget = Entry(root, textstring = estring, width = 32)
    ewidget.pack()
    print("Entered  =  ",textstring)
This fails also, with Error message :
Error:
unknown option "-textstring"
Any help greatly appreciated.


RE: Entry widget : unable to get the text entered - metulburr - Oct-08-2016

Its textvariable, not textstring
import tkinter as tk
root = tk.Tk()
root.geometry('600x400+80+40')

class App:
    def __init__(self, root):
        self.entry_var = tk.StringVar()
        self.entry = tk.Entry(root, textvariable=self.entry_var)
        self.entry.bind('<Return>', self.show_output)
        self.entry.pack()
        
    def show_output(self, event):
        print(self.entry_var.get())
        
App(root)
tk.mainloop()



RE: Entry widget : unable to get the text entered - dlemper - Oct-11-2016

Thanks so much Metulburr.
That App prints the entry to the command line.

But I'm struggling with OOP and I've been unable
to have the App yield the entry to the running script.

Putting in a return statement at the end just yields
<__main__.App object at 0x01B242D0>
ie return self.entry_var.get()