简体   繁体   中英

Tkinter Input/Entry in Python

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()

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM