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 “*”.

Example:1
Python script to take input for a name and display a welcome message
Sol:

#textbox/Entry
from tkinter import *
window = Tk()
window.title("Use of Textbox / Entry ")
window.geometry('350x200')
lbl = Label(window, text="Enter your name ")
lbl.place(x=20, y=40)
txt = Entry(window,width=10)
txt.place(x=120, y=40)
def clicked():
    res = "Welcome  " + txt.get()
    lbl2=Label(window,text=res)
    lbl2.place(x=20,y=80)


btn = Button(window, text="Click Me", command=clicked)
btn.place(x=180, y=40)
window.mainloop()

Output:

Visitor Counter

0142271