1

So I am trying to when the person clicks on the button I created, to display the text they wrote on the input field but for some reason when I click the button it displays:

.!entry

And I don't know what I am doing wrong since I am new to python so I wanted to know how to fix this problem, here's my code and thank you since any help is appreciated!

from tkinter import *

screen = Tk()

def print_input():
    text2 = Label(screen, text=input_field)
    text2.grid(row=1, columnspan=3)

text = Label(screen, text="Write to print:")
text.grid(row=0, column=0)

input_field = Entry(screen)
input_field.grid(row=0, column=1)

submit_button = Button(screen, text="Print!", fg="yellow", bg="purple",             
command=print_input)
submit_button.grid(row=0, column=2)

screen.mainloop()

1 Answer 1

3

Change:

def print_input():
    text2 = Label(screen, text=input_field)

to:

def print_input():
    text2 = Label(screen, text=input_field.get())
    #                                     ^^^^^^

You're telling the text of the label to be set to the Entry widget instead of the Entry widget's content. To get the content of the Entry widget, use the .get() method.

The funky string you're seeing in the label is the tkinter name for the Entry widget.

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

4 Comments

ohhhhh, makes a lot more sense now, thank you for your answer since it solved my problem! :)
Happy to hear it. If my answer solved your problem, please select my answer as the correct answer. And happy coding!
have to wait 2 minutes ;) btw another problem appeared meanwhile, the input results are being displayed all on top of each other, do you know how to stop that from happening? (screenshot: gyazo.com/b8ed1218674eb610c2882b7eba936bae)
This is happening because your code creates a new Label each time the print_input() routine is called. You should restructure your code to only create the Label once, and the print_input() routine should only change the existing Label's text, not create a new Label.

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.