Python tKinter 6

Get input using Entry class (Tkinter textbox)

In the previous Python GUI examples, we saw how to add simple widgets, now let’s try getting the user input using Tkinter Entry class (Tkinter textbox).

We can create a textbox using Tkinter Entry class :

txt = Entry(window,width=10)

Then you can add it to the window using grid function as usual

This widget renders a single-line text box for accepting the user input. For multi-line text input use the Text widget. Apart from the properties already mentioned, the Entry class constructor accepts the following:

bd : border size of the text box; default is 2 pixels.
show : to convert the text box into a password field, set show property to “*”.

Python script to take input for a name and display a welcome message

from tkinter import *
window = Tk()
window.title("Use of Textbox / Entry ")
lbl = Label(window, text="Enter your name "), y=40)
txt = Entry(window,width=10), y=40)
def clicked():
    res = "Welcome  " + txt.get()

btn = Button(window, text="Click Me", command=clicked), y=40)