Python Forum
[Tkinter] Entry widget : unable to get the text entered
Thread Rating:
  • 2 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Entry widget : unable to get the text entered
#1
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.
Reply
#2
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()
Recommended Tutorials:
Reply
#3
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  ValueError: could not convert string to float: '' fron Entry Widget russellm44 5 489 Mar-06-2024, 08:42 PM
Last Post: russellm44
  [Tkinter] entry widget DPaul 5 1,426 Jul-28-2023, 02:31 PM
Last Post: deanhystad
  [Tkinter] The Text in the Label widget Tkinter cuts off the Long text in the view malmustafa 4 4,664 Jun-26-2022, 06:26 PM
Last Post: menator01
  Tkinter Exit Code based on Entry Widget Nu2Python 6 2,871 Oct-21-2021, 03:01 PM
Last Post: Nu2Python
  [Tkinter] Text widget inert mode on and off rfresh737 5 3,781 Apr-19-2021, 02:18 PM
Last Post: joe_momma
  Line numbers in Text widget rfresh737 3 5,308 Apr-15-2021, 12:30 PM
Last Post: rfresh737
  tkinter text widget word wrap position chrisdb 6 7,442 Mar-18-2021, 03:55 PM
Last Post: chrisdb
  method to add entries in multi columns entry frames in self widget sudeshna24 2 2,212 Feb-19-2021, 05:24 PM
Last Post: BashBedlam
  Entry Widget issue PA3040 16 6,647 Jan-20-2021, 02:21 PM
Last Post: pitterbrayn
  [Tkinter] password with Entry widget TAREKYANGUI 9 5,772 Sep-24-2020, 05:27 PM
Last Post: TAREKYANGUI

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020