Python Tkinter to Display Data in Textboxes

In this Python tutorial, we will discuss how to display data in textboxes using Python Tkinter.

Python Tkinter to Display Data in Textboxes

  • Let us see, how to display data in Textboxes or Entry widgets in Python Tkinter. Textboxes are called Entry widgets in Python Tkinter.
  • To display data we have to use insert() function. insert function takes two parameters.
    • index position
    • information to be inserted
  • In our program, we have used entry widgets to capture the information provided by the user, and then we have to frame a sentence & that sentence is displayed in another Entry widget.

Code:

from tkinter import *

def frame_sentence():
    name = name_tf.get()
    age = int(age_tf.get())
    desc = descipline_tf.get()

    disp_tf.insert(0,f'{age} years old {name} became {desc}.')

ws = Tk()
ws.title('PythonGuides')
ws.geometry('400x300')
ws.config(bg='#0f4b6e')

name_tf = Entry(ws)
age_tf = Entry(ws)
descipline_tf = Entry(ws)

name_lbl = Label(
    ws,
    text='Name',
    bg='#0f4b6e',
    fg='white'
)
age_lbl = Label(
    ws,
    text='Age',
    bg='#0f4b6e',
    fg='white'
)

descipline_lbl = Label(
    ws,
    text='Descipline',
    bg='#0f4b6e',
    fg='white'
)

name_lbl.pack()
name_tf.pack()
age_lbl.pack()
age_tf.pack()
descipline_lbl.pack()
descipline_tf.pack()

btn = Button(
    ws,
    text='Frame Sentence',
    relief=SOLID,
    command=frame_sentence
)
btn.pack(pady=10)

disp_tf = Entry(
    ws, 
    width=38,
    font=('Arial', 14)
    )

disp_tf.pack(pady=5)


ws.mainloop()

Output:

Here is the output of the above code, It is the interface wherein the user can fill in the information and can click on the frame sentence button.

Python Tkinter to Display Data in Textboxes
Python Tkinter to Display Data in Textboxes

Here is the second image of the output wherein user details has been filled.

Display data in textboxes using Python Tkinter
Display data in textboxes using Python Tkinter

In this output, after filling details user has clicked on the frame sentence button as a result of which a sentence has been displayed using provided information.

How to display data in textboxes using Python Tkinter
How to display data in textboxes using Python Tkinter

You may like the following Python tkinter tutorials:

In this tutorial, we have learned how to display data in textbox using python tkinter.