artman41
artman41

Reputation: 404

How to get text from Entry widget

I've looked at several posts on stackOverflow that explain the answer but no matter which I use, I never can get the string from my entry widget; it just detects a string of ""

here's my code:

def buttonTest():
    global score
    gui.title("Test")
    for child in gui.winfo_children():
        child.destroy()
    global questionText
    global questionAnswer
    questionText = StringVar()
    questionAnswer = 0    
    question = Label(gui, textvariable = questionText, fg = "black", bg = "white")
    question.grid(row = 0, column = 1)
    userInput = StringVar()
    input = Entry(gui, textvariable = userInput)
    input.grid(row = 1, column = 0)

swapQuestion()

checkAns = Button(text = "Check answer", command = partial(checkAnswer, userInput.get(), questionAnswer), fg = "black", width=10)
checkAns.grid(row = 1, column = 2)

Upvotes: 2

Views: 35877

Answers (3)

LeoThaGodKing
LeoThaGodKing

Reputation: 31

This is an example of capturing the text from an entry box via a button click when using Tkinter in Python.

Remember to capture the entry box's text using the button's event handler. Otherwise you will not capture the entry box's current text value and you will continue to detect a string value of "". Hope this helps!

    import tkinter
    
    window = tkinter.Tk()
    window.title("Entry Input Example")
    window.minsize(width=350, height=100)
    
    
    # Event Handlers
    def my_button_clicked():
        entry_value_label.config(text=f"{my_entry.get()}")  # Captures current entry box text and updates label text.
    
    
    # Entries
    my_entry = tkinter.Entry(width=10)
    my_entry.focus()
    my_entry.grid(column=0, row=0)
    
    # Labels
    entry_value_label = tkinter.Label(text=f"{my_entry.get()}")  # Label to be updated upon a button click.
    entry_value_label.grid(column=0, row=1)
    
    my_label = tkinter.Label(text=f"{my_entry.get()} is the current value from the entry box.")
    my_label.grid(column=1, row=1)
    
    # Buttons
    my_button = tkinter.Button(text="Click Me", command=my_button_clicked)  # Sets my_button_clicked function to be called.
    my_button.grid(column=0, row=2)
    
    window.mainloop()  # Ensures the window remains open.

Upvotes: 0

Freddy Mcloughlan
Freddy Mcloughlan

Reputation: 4496

Simple example:

from tkinter import *

# Get Entry contents
def print_input():
    print(input_variable.get())


window = Tk()

# Create widgets
input_variable = StringVar()
entry_variable = Entry(window, textvariable=input_variable).grid(row=0, column=0)

button_submit = Button(window, text="Submit",command=print_input).grid(row=1, column=0)

window.mainloop()

Where:

  • input_variable is your variable
  • entry_variable is the entry box
  • button_submit calls print_input() to fetch and print the entry_variable's contents which is stored in input_variable

Upvotes: 1

Terry Jan Reedy
Terry Jan Reedy

Reputation: 19144

Please read and follow this SO help page. Your code is missing lines needed to run and has lines that are extraneous to your question. It is also missing indentation.

Your problem is that you call userInput.get() just once, while creating the button, before the user can enter anything. At that time, its value is indeed ''. You must call it within the button command function, which is called each time the button is pressed.

Here is a minimal complete example that both runs and works.

import tkinter as tk

root = tk.Tk()

user_input = tk.StringVar(root)
answer = 3

def verify():
    print(int(user_input.get()) == answer)  # calling get() here!

entry = tk.Entry(root, textvariable=user_input)
entry.pack()
check = tk.Button(root, text='check 3', command=verify)
check.pack()

root.mainloop()

Upvotes: 10

Related Questions