Creating single line textbox in Python utilizing tkinter library
To allow customers to enter single-line textual content in Python GUI primarily based utility, you might use the tkinter entry widget.
- The entry widget permits single-line textual content.
- For a number of line textual content entry, use the textual content widget.
- A person can enter numbers, strings, and combined textual content as utilizing the entry widget of tkinter library.
Syntax of making an entry widget
w = Entry (mum or dad, choices)
Allow us to present you examples of making single line textbox with easy and completely different out there choices.
An instance of easy entry widget
On this instance, we are going to create a label that’s connected with the Entry widget. As you run this system, you will notice:
Enter your identify:
With the entry widget/textbox:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
from tkinter import *
win = Tk()
win.geometry(“250×200”)
#Label for textbox
Lbl = Label(win, textual content=“Enter Your Title:”)
Lbl.pack( aspect = LEFT)
#Making a textbox/entry
Ent = Entry(win)
Ent.pack(aspect = RIGHT)
win.mainloop() |
Output:
Setting the background colour of entry field
By utilizing bg choice of the entry widget, you might set the background colour of the textbox.
It’s possible you’ll present a colour identify e.g. inexperienced, blue, pink, and many others., or a colour worth e.g. #FFF000.
See the instance under.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
#Entry fields background colours
from tkinter import *
win = Tk()
win.geometry(“350×200”)
win.title(“Entry Background Coloration”)
lbl1 = Label(win, textual content = “By Coloration identify:”).place(x = 30,y = 50)
lbl2 = Label(win, textual content = “By Coloration code:”).place(x = 30, y = 90)
txt1 = Entry(win, bg=“blue”).place(x = 120, y = 50)
txt2 = Entry(win, bg=“#B0FFB0”).place(x = 120, y = 90)
win.mainloop() |
Output:
Setting borders of textual content field by bd choice
The bd choice permits us to set the borders of the Entry textbox. The default worth is 2.
Within the instance under, we are going to set the border of two entry widgets. The primary is 5 px and the second is 10 pixels for the demo solely.
Python code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
#Entry fields Border
from tkinter import *
win = Tk()
win.geometry(“350×200”)
win.title(“Entry Borders”)
lbl1 = Label(win, textual content = “Border = 5:”).place(x = 30,y = 50)
lbl2 = Label(win, textual content = “Border = 10:”).place(x = 30, y = 90)
txt1 = Entry(win, bg=“pink”, bd=“5”).place(x = 120, y = 50)
txt2 = Entry(win, bg=“#66B3B3”, bd=“10”).place(x = 120, y = 90)
win.mainloop() |
Output:
An instance of foreground colour/textual content colour
For setting the foreground colour (or textual content colour) of the entry widget, use the fg choice.
The default is black. Nevertheless, as we set the darkish background colour within the above examples, you might have considered trying a lighter colour of the textual content.
The instance under has two entry fields. One with the default black colour and the second is yellow colour for the foreground (fg) choice.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
#Entry fields textual content colour
from tkinter import *
win = Tk()
win.geometry(“350×200”)
win.title(“Entry Textual content Coloration”)
lbl1 = Label(win, textual content = “fg = black”).place(x = 30,y = 50)
lbl2 = Label(win, textual content = “fg = yellow”).place(x = 30, y = 90)
txt1 = Entry(win, bg=“brown”, ).place(x = 120, y = 50)
txt2 = Entry(win, bg=“inexperienced”, fg = “yellow”).place(x = 120, y = 90)
win.mainloop() |
Output:
Present entered textual content as * or another character – password subject
By utilizing present choice of the entry widget, you might show entered characters as the required character.
For instance, you wish to take the person password and show * for the entered character.
The instance under exhibits “^” for the primary entry subject and “*” for the second subject.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
#Entry fields present choice – ^ and *
from tkinter import *
win = Tk()
win.geometry(“350×200”)
win.title(“SHowing Textual content as ^ and *”)
lbl1 = Label(win, textual content = “Show ^”).place(x = 30,y = 50)
lbl2 = Label(win, textual content = “Show *”).place(x = 30, y = 90)
txt1 = Entry(win, bg=“#800000”, present=“^” , fg = “#FFFFC6”).place(x = 120, y = 50)
txt2 = Entry(win, bg=“#008080”, present=“*”, fg = “#FFFFC6”).place(x = 120, y = 90)
win.mainloop() |
Output:
An instance of disabled entry fields
By utilizing state=DISABLED choice, the entry subject is seen as grayed-out aspect within the kind. The default worth of the state is NORMAL.
The instance under shows two entry fields. The primary is disabled and the opposite is regular.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
#Disbaled Entry subject instance
from tkinter import *
win = Tk()
win.geometry(“350×200”)
win.title(“Disabled Entry Discipline”)
lbl1 = Label(win, textual content = “Disabled: “).place(x = 30,y = 50)
lbl2 = Label(win, textual content = “Regular: “).place(x = 30, y = 90)
txt1 = Entry(win, bg=“#800000”, state=DISABLED , fg = “#FFFFC6”).place(x = 120, y = 50)
txt2 = Entry(win, bg=“#008080”, state=NORMAL, fg = “#FFFFC6”).place(x = 120, y = 90)
win.mainloop() |
Output:
An instance of utilizing entry in a message field
On this instance, we are going to get the worth of the entry subject and show it within the tkinter message field.
For that, execute this system and enter textual content into the entry subject. After this, press the button and it’ll show the textual content of the entry in a message field.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
#import tkinter library and messagebox
from tkinter import *
from tkinter import messagebox
#Create an occasion of tkinter body or window
win = Tk()
win.geometry(“350×200”)
win.title(“Get entry worth and show”)
#Perform to get entry worth and show textual content within the message field
def disp_text():
ent_txt=txt1.get()
messagebox.showinfo(“You Entered”, ent_txt)
#Taking person enter
txt1= Entry(win, bg=“#800000”, fg = “#FFFFC6”, width=30)
txt1.pack(pady= 50)
#Create a button to show the textual content of entry widget
btn= Button(win, textual content=“Press to See in message field?”, command= disp_text)
btn.pack()
win.mainloop() |
Pattern output: