How To Add Entry In Tkinter In Python🧐

Introduction

 
In this article, I will explain how to add entry in Tkinter in Python.
 
Definition
 
The Entry keyword is used for single-line text strings from a user. If you want to display multiple lines of text that can be edited, then you should use the Text widget.
 
Syntax
 
w = Entry( master, option, ... )
 
Parameters
 
a − This represents the parent window of the computer.
 
option − The list of most used options for this widget.
 

Option & Description

  • Bd - The Border is used to change the width in pixels. Default pixels value is 2.
  • Bg - The background is used to change normal background color.
  • Fg - The fg is used to change the normal foreground (text) color.
  • Font - The font is used to change the text font to be used for the button's label.
  • Cursor - The cursor is used to set this option to a cursor name, the mouse cursor will change to that pattern when it is over the entry.
  • Show - The show keyword is used to change the characters typed into password type (show="*").
Program
  1. from tkinter  
  2. import * login = Tk()  
  3. login.title("Login")  
  4. login.geometry("300x250")  
  5. Label(login, text = "Username", bg = "pink", bd = 10, \fg = "black", font = "Castellar").pack()  
  6. username = Entry(login, textvariable = "username", \bg = "green", bd = 10, fg = "white", font = "Castellar", cursor = "target")  
  7. username.pack()  
  8. Label(login, text = "Password", bg = "pink", bd = 10, \fg = "black", font = "Castellar").pack()  
  9. password = Entry(login, textvariable = "password", \show = '*', bg = "green", bd = 10, fg = "white", font = "Castellar", cursor = "target")  
  10. password.pack()  
  11. Button(login, text = "Login", width = 10, height = 1, bg = "red", cursor = "watch").pack()  
  12. login.mainloop()   
Output
 
How To Add Entry In Tkinter In Python
  • Selectbackground - The Selectbackground color to use displaying selected text.
  • Selectborderwidth - The Selectborderwidth of the border to use around selected text. Default is pixel 1.
  • Command - The command is used to call a function or method when the button is clicked
Program
  1. from tkinter  
  2. import * a = Tk()  
  3. a.geometry("400x400")  
  4. a.title("test")  
  5. def hello(): a.configure(bg = "red")  
  6. a.configure(bd = 10)  
  7. L1 = Label(a, text = "c# corner").pack()  
  8. b = Button(a, text = "click me!", command = hello).pack()  
  9. a.mainloop()   
Output
 
How To Add Entry In Tkinter In Python
  • Relief - Relief is used to specify the type of the border. The values are SUNKEN, RAISED, GROOVE, and RIDGE.
  • State - The state is used to set this option to DISABLED to gray out the button and make it unresponsive.
  • Width- The width is used to change the width of the button in letters.
  • Justify - The justify is used to change the arrangement order LEFT, CENTER, RIGHT.
  • Textvariable - The Textvariable is used to be able to retrieve the current text from your entry widget, you must set the StringVar class.
Program
  1. from tkinter  
  2. import * a = Tk()  
  3. a.geometry("400x400")  
  4. a.title("test")  
  5. Label(a, text = "Username").pack()  
  6. username = Entry(a, textvariable = "username", \state = DISABLED, justify = CENTER, relief = "solid", width = 10)  
  7. username.pack()  
  8. a.mainloop()   
Output
 
How To Add Entry In Tkinter In Python
 

Conclusion

 
In this article, we have seen how to add an entry in Tkinter in Python. I hope this article was useful to you. Thanks for reading.


Recommended Free Ebook
Similar Articles