How to set the width of a Tkinter Entry widget in pixels?


Tkinter has an Entry widget to accept single-line user input. It has many properties and attributes that can be used to configure the Entry widget. To change the size (width or height of the Entry widget), we can use Internal Padding Properties – ipadx and ipady. By defining the value of internal padding, we can actually change the width and height of the Entry widget.

Example

#Import the required Libraries
from tkinter import *
from tkinter import ttk
#Create an instance of tkinter frame
win = Tk()
#Set the geometry of tkinter frame
win.geometry("750x350")

#Define a function to submit the validate the value of Entry widget
def submit_name():
   Label(frame, text="Hello "+ entry.get(), font=('Helvetica',12, 'bold')).pack(pady=20)
   submit.configure(state= "disabled")

#Creates a Frame
frame = LabelFrame(win, width= 400, height= 180, bd=3)
frame.pack()
#Create an Entry widget in the Frame for Accepting the Username
entry = ttk.Entry(frame, width= 40)
entry.insert(INSERT, "Enter Your Name")
entry.pack(ipadx= 30, ipady=30)

#Set the focus on Entry1
entry.focus_set()

#Create a submit button
submit= ttk.Button(win, text= "submit",command=submit_name)
submit.pack(pady=10)
win.mainloop()

Output

Run the above code to display the Entry widget which has some internal padding in it.

In the given output, we will click the "submit" button and it will display the greeting message as the output on the screen.

Updated on: 03-May-2021

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements