t1=tk.Entry(parent_window,attributes)
parent_window
: Declared Parent window attributes
: Various attributes can be added, list is given below. import tkinter as tk
my_w = tk.Tk()
my_w.geometry("250x150")
my_w.title("plus2net.com") # Adding a title
l1 = tk.Label(my_w, text='Your Name', width=10 ) # added one Label
l1.grid(row=1,column=1)
e1 = tk.Entry(my_w, width=20,bg='yellow') # added one Entry box
e1.grid(row=1,column=2)
my_w.mainloop()
bg | Color of the background. We can use Hex value or colour name. ( bg='green' or bg='#ffff00') | |
bd | Border size, default value is 1 | |
cursor | Shape of the cursor while moving over the Entry widget. A list of shapes are available at Button Page. | |
font | font style ,size used. Check the example below | |
fg | font colour. Check the example below | |
justify | Alignment of text. Values are 'left','center' or 'right' | |
relief | The borders 3 D effect style. It can take these values 'raised' , 'sunken' ,'flat', 'ridge', 'solid' & 'groove' | |
selectbackground | Colour of the background when text is selected | |
selectforeground | Colour of the font when text is selected
| |
selectborderwidth | Size of the border around the selection, default value is 0 | |
show | Entries can be masked, especially useful for passwords.. Check the example below. | |
state | Values can be 'disabled', 'normal','readonly'. Check the example below. | |
textvariable | variable associated with the Entry. Used for setting or reading the user entered data. Usually StringVar is used. | |
width | width of the widget, default value is 20 | |
xscrollcommand | Connecting Entry widget to a scrollbar |
e1.get()
e1.delete(0,END)
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("380x200")
e1=tk.Entry(my_w,text='plus2net',font=24)
e1.grid(row=0,column=0,padx=5,pady=20)
b1=tk.Button(my_w,text='Reset',font=24,command=lambda:e1.delete(0,'end'))
b1.grid(row=0,column=1,padx=10,pady=20)
my_w.mainloop()
for widget in my_w.winfo_children():
if isinstance(widget, tk.Entry): # If this is an Entry widget
widget.delete(0,'end') # Delete all entries
How Reset button is used to delete all user enterd data and selections
e1_str=tk.StringVar(my_w) # Declaring String variable
e1 = tk.Entry(my_w,textvariable=e1_str,width=15) # added one Entry box
e1.grid(row=1,column=2)
e1_str.set('Welcome')
Using the above methods we will create one application where on Click of a button the data entered by the user in Entry box is displayed and default text is changed to Welcome. def my_upd():
my_str.set(e1.get()) # read and assign text to StringVar()
e1_str.set('Welcome')# Update the Entry box text
Here on Click of the button the function my_upd() is executed. Inside this function we will first read the text entered by user and assign the same to another Label ( l2 ) . import tkinter as tk
my_w = tk.Tk()
my_w.geometry("250x150")
my_w.title("plus2net.com") # Adding a title
l1 = tk.Label(my_w, text='Your Name', width=10 ) # added one Label
l1.grid(row=1,column=1)
e1_str=tk.StringVar()
e1 = tk.Entry(my_w,textvariable=e1_str,width=15) # added one Entry box
e1.grid(row=1,column=2)
b1 = tk.Button(my_w, text='Update', width=8,bg='yellow',
command=lambda: my_upd())
b1.grid(row=1,column=3)
my_str = tk.StringVar()
# added one Label
l2 = tk.Label(my_w, text='Output',textvariable=my_str, width=10 )
l2.grid(row=2,column=1)
def my_upd():
my_str.set(e1.get()) # read and assign text to StringVar()
e1_str.set('Welcome')# Update the Entry box text
my_w.mainloop()
We can change or manage different attributes by using config. One example is here .
e1.config(bg='red') # change background color to red
Similarly other properties can be updated. One similar list is available with Text entry.
e1 = tk.Entry(my_w, width=20,bg='yellow', state='disabled')
e1.grid(row=1,column=2)
By using config we can manage the state also.
e1 = tk.Entry(my_w, width=20,bg='yellow')
e1.grid(row=1,column=2)
e1.config(state='disabled')
show='*'
or we can use other chars show='#'
l2 = tk.Label(my_w, text='Output',textvariable=my_str,show='*' )
Show or hide password based on Checkbutton
for options in e1.config():
print(options + ": " + str(e1[options]))
Sample output is here ( there are more attributes .... )
background: Aqua
bd: 1
bg: Aqua
borderwidth: 1
cursor: xterm
-----
-----
import tkinter as tk
my_w = tk.Tk()
from tkinter import *
my_w.geometry("250x150")
my_w.title("plus2net.com") # Adding a title
l1 = tk.Label(my_w, text='First' ) # added one Label
l1.grid(row=1,column=1)
e1_str=tk.StringVar()
e1 = tk.Entry(my_w,textvariable=e1_str) # added one Entry box
e1.grid(row=1,column=2)
l2 = tk.Label(my_w, text='Second' ) # added one Label
l2.grid(row=2,column=1)
e2_str=tk.StringVar()
e2 = tk.Entry(my_w,textvariable=e2_str) # added one Entry box
e2.grid(row=2,column=2)
b1 = tk.Button(my_w, text='Update', width=8,
command=lambda: my_upd())
b1.grid(row=3,column=1)
b2 = tk.Button(my_w, text='Reset', width=8,
command=lambda: my_reset())
b2.grid(row=3,column=2)
## buttons for changing font colour of Entries
b3 = tk.Button(my_w, text='fg=green', width=8,
command=lambda: my_config('fg','green'))
b3.grid(row=4,column=1)
b4 = tk.Button(my_w, text='fg=red', width=8,
command=lambda: my_config('fg','red'))
b4.grid(row=4,column=2)
b5 = tk.Button(my_w, text='bg=yellow', width=8,
command=lambda: my_config('bg','yellow'))
b5.grid(row=5,column=1)
b6 = tk.Button(my_w, text='bg=blue', width=8,
command=lambda: my_config('bg','blue'))
b6.grid(row=5,column=2)
def my_upd():
e2_str.set(e1.get()) # read and assign text to StringVar()
def my_reset():
e1.delete(0,END) # Delete first Entry
e2.delete(0,END) # Delete Second Entry
e1.config(bg='#ffffff',fg='#000000') # reset background
e2.config(bg='#ffffff',fg='#000000') # reset background
def my_config(type,col):
if type=='fg':
e1.config(fg=col)
e2.config(fg=col)
elif type=='bg':
e1.config(bg=col)
e2.config(bg=col)
my_w.mainloop()
e1.bind("<FocusIn>",lambda x: e1.select_range(0,tk.END))
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("350x150")
my_w.title("plus2net.com") # Adding a title
l1 = tk.Label(my_w,text='Your Name',width=10,font=20)
l1.grid(row=1,column=1,padx=5,pady=10)
e1 = tk.Entry(my_w,bg='yellow',font=28)
e1.grid(row=1,column=2)
str1=tk.StringVar(value=0) # StringVar for l2
l2=tk.Label(my_w,text=0,
bg='lightgreen',textvariable=str1,width=2,font=28)
l2.grid(row=1,column=3)
e1.bind("<KeyRelease>",
lambda x : str1.set(str(len(e1.get()))))
my_w.mainloop()
<FocusIn>
: When we click the Entry widget or it is in focus and mouse is inside it. <FocusOut>
: When focus is moved out of the Entry widget. Tab key is pressed or Mouse clicked outside. <KeyRelease>
: When any key is released.<KeyPress>
: When any key is pressed.
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("350x150")
my_w.title("plus2net.com") # Adding a title
l1 = tk.Label(my_w, text='Name', width=5,font=22 ) #
l1.grid(row=1,column=1,pady=50)
e1 = tk.Entry(my_w,bg='yellow',width=12,font=22)
e1.grid(row=1,column=2,padx=2)
str1=tk.StringVar()
e2 = tk.Entry(my_w,bg='#BFEFFF',width=12,font=22,textvariable=str1)
e2.grid(row=1,column=3,padx=5)
def my_upd(event):
str1.set(e1.get())
e1.bind('<FocusIn>',lambda n:e1.config(bg='lightgreen'))
e1.bind('<FocusOut>',lambda n:e1.config(bg='lightyellow'))
e1.bind('<KeyRelease>',my_upd)
my_w.mainloop()
e1.bind('<KeyPress>',my_upd)
On KeyPress the event is triggered first before the char enteres the first entry box ( e1 ). So while copying the data from first entry ( e1) to second entry box (e2 ) the last char ( most recent key used ) is not copied.
from sqlalchemy import create_engine
# Connect to database using your user id and password
my_conn = create_engine("mysql+mysqldb://userid:password@localhost/database_name")
r_set = my_conn.execute("SELECT name FROM student WHERE id=6") # execute query
my_result = r_set.fetchone() # get the data
print(my_result[0]) # print to console
# Create Tkinter window and show the value
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("250x150")
my_w.title("plus2net.com") # Adding a title
l1 = tk.Label(my_w, text="Your Name", width=10) # added one Label
l1.grid(row=1, column=1)
e1_str = tk.StringVar(value=my_result[0]) # default value taken from database
e1 = tk.Entry(my_w, width=20, bg="yellow", textvariable=e1_str) # added one Entry box
e1.grid(row=1, column=2)
my_w.mainloop()
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.