2

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)
artman41
  • 402
  • 2
  • 5
  • 15
  • Fix your indention please, otherwise is difficult for future visitors to understand your code. By the way, you don't need a StringVar to get the content of an Entry: http://stackoverflow.com/questions/10727131/tkinter-get-entry-content-with-get – nbro Mar 09 '15 at 19:39

3 Answers3

8

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()
nbro
  • 15,395
  • 32
  • 113
  • 196
Terry Jan Reedy
  • 18,414
  • 3
  • 40
  • 52
  • ah, thanks for clearing that up, I just took the code out of the program since this was the offending code - the indentation must have changed when I pasted it here. This explains my problem however, thanks :D – artman41 Mar 09 '15 at 17:48
  • `root.mainloop` is needed when running the code from a command line but not, for this experiment, when running from the Idle editor. – Terry Jan Reedy Mar 09 '15 at 22:40
  • @TerryJanReedy But you should not even run Tkinter apps from the IDLE as a rule of dumb, I think. – nbro Mar 09 '15 at 23:36
  • @Rinzler Except for text-mode apps that gain from having a console (shell) that can display the full BMP subset of unicode, I agree that apps should not be run through Idle for production use, after development. But Idle is great, not dumb, for interactive development, especially for tkinter widget properties and layout. For instance, if one runs the code above without the `mainloop()` call, one can manipulate and even add widgets interactively. For instance, `>>> check['text'] = 'new-title'` immediately changes the button title in the tk window. Or try `>>> check.pack(side='right')`. – Terry Jan Reedy Mar 11 '15 at 02:48
0

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
Freddy Mcloughlan
  • 4,129
  • 1
  • 13
  • 29
0

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.