0

I have this simple problem with this code. the purpose is to make a label showing whatever the user has typed in the Entry widget.

def action():

    output = tk.Label(master, text = #this should be the user input in the Entry widget)
    output.grid(row=2)

import tkinter as tk

master = tk.Tk()

e = tk.Entry(master, bg = 'orange').grid(row=0)
b = tk.Button(master, text = 'do it', bg = 'white', command = action).grid(row = 1)

master.mainloop()

1 Answer 1

1

You Have to Define variable of StringVar. then pass it through Entry widget initialization and then you can get text of the widget by setting get function for StringVar variable. here is the code.

content = StringVar()
entry = Entry(parent, text=caption, textvariable=content)

# Getting Text from Entry Widget
text = content.get()

# Setting text to Entry widget
content.set(text)

you can read documentation here : https://effbot.org/tkinterbook/entry.htm

Sign up to request clarification or add additional context in comments.

4 Comments

i get this error now : NameError: name 'StringVar' is not defined. also can you tell me where to add the #content.set(text)
use tk.StringVar() . i forgot to mention in the code
thanks alot. it works now but I did not use the last line of code you suggested
You don't have to use a StringVar. You can, but it's quite possible to get and set the text in a label or entry without it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.