Bottom Page

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.
Quote
#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()
Quote
#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()
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  POPUP on widget Entry taratata2020 4 368 Mar-10-2020, 05:04 PM
Last Post: taratata2020
  Entry Widget issue PA3040 13 276 Mar-06-2020, 10:24 AM
Last Post: Larz60+
  [Tkinter] manipulation of string in Text widget Stauricus 2 145 Feb-17-2020, 09:23 PM
Last Post: Stauricus
  Transfer Toplevel window entry to root window entry with TKinter HBH 0 913 Jan-23-2020, 09:00 PM
Last Post: HBH
  The coordinates of the Entry widget (canvas) when moving berckut72 8 311 Jan-07-2020, 09:26 AM
Last Post: berckut72
  [Tkinter] Entry widget to variable Zeiphar 4 427 Sep-21-2019, 05:41 AM
Last Post: Zeiphar
  [Tkinter] Paste Operation not working in Text Widget Code_Enthusiast 1 400 Sep-11-2019, 08:49 PM
Last Post: Larz60+
  [Kivy] [CRITICAL] [App ] Unable to get a Text provider, abort. Exsul 6 3,748 Aug-17-2019, 11:54 PM
Last Post: Exsul
  [Tkinter] Inserting numbers into entry box in a widget richard13 1 744 Aug-11-2019, 11:37 AM
Last Post: Yoriz
  [PyQt] Collect entry from textline Widget via UI file mart79 3 423 Aug-05-2019, 01:40 PM
Last Post: Denni

Forum Jump:


Users browsing this thread: 1 Guest(s)