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
- from tkinter
- import * login = Tk()
- login.title("Login")
- login.geometry("300x250")
- Label(login, text = "Username", bg = "pink", bd = 10, \fg = "black", font = "Castellar").pack()
- username = Entry(login, textvariable = "username", \bg = "green", bd = 10, fg = "white", font = "Castellar", cursor = "target")
- username.pack()
- Label(login, text = "Password", bg = "pink", bd = 10, \fg = "black", font = "Castellar").pack()
- password = Entry(login, textvariable = "password", \show = '*', bg = "green", bd = 10, fg = "white", font = "Castellar", cursor = "target")
- password.pack()
- Button(login, text = "Login", width = 10, height = 1, bg = "red", cursor = "watch").pack()
- login.mainloop()
Output
-
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
- from tkinter
- import * a = Tk()
- a.geometry("400x400")
- a.title("test")
- def hello(): a.configure(bg = "red")
- a.configure(bd = 10)
- L1 = Label(a, text = "c# corner").pack()
- b = Button(a, text = "click me!", command = hello).pack()
- a.mainloop()
Output
-
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
- from tkinter
- import * a = Tk()
- a.geometry("400x400")
- a.title("test")
- Label(a, text = "Username").pack()
- username = Entry(a, textvariable = "username", \state = DISABLED, justify = CENTER, relief = "solid", width = 10)
- username.pack()
- a.mainloop()
Output
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.