0% found this document useful (0 votes)
13 views13 pages

4 Entry Widget

The document provides an overview of various Tkinter widgets, including Entry, Text, Message, and Messagebox widgets, detailing their syntax, options, and methods. It includes example code for creating user interfaces with these widgets, demonstrating how to collect user input and display messages. Each widget serves a specific purpose, such as entering text, displaying multiline text, or showing message boxes to the user.

Uploaded by

darshibhai2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views13 pages

4 Entry Widget

The document provides an overview of various Tkinter widgets, including Entry, Text, Message, and Messagebox widgets, detailing their syntax, options, and methods. It includes example code for creating user interfaces with these widgets, demonstrating how to collect user input and display messages. Each widget serves a specific purpose, such as entering text, displaying multiline text, or showing message boxes to the user.

Uploaded by

darshibhai2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Entry Widget

The Entry Widget is a Tkinter Widget used to Enter or display a single line of text.

Syntax :
entry = tk.Entry(parent, options)

• Options of Entry widgets are bg, bd, font, fg, justify, relief, textvariable, and show.
• Show :Normally, the characters that the user types appear in the entry.
To make a .password. entry that echoes each character as an asterisk, set show=”*”.
Methods: The various methods provided by the entry widget are:

get() : Returns the entry’s current text as a string.


delete() : Deletes characters from the widget
insert ( index, ‘name’) : Inserts string ‘name’ before the character at the given index.
import tkinter as tk
root=tk.Tk()

# setting the windows size


root.geometry("600x400")

# declaring string variable for storing name and password


name_var=tk.StringVar()
passw_var=tk.StringVar()

# defining a function that will get the name and password and
# print them on the screen
def submit():
name=name_var.get()
password=passw_var.get()
print("The name is : " + name)
print("The password is : " + password)
name_var.set("")
passw_var.set("")
# creating a label for
# name using widget Label
# placing the label and entry in
name_label = tk.Label(root, text = 'Username',
# the required position using grid
font=('calibre',10, 'bold'))
# method
name_label.grid(row=0,column=0)
# creating a entry for input
name_entry.grid(row=0,column=1)
# name using widget Entry
passw_label.grid(row=1,column=0)
name_entry = tk.Entry(root,textvariable = name_var,
passw_entry.grid(row=1,column=1)
font=('calibre',10,'normal'))
sub_btn.grid(row=2,column=1)
# creating a label for password
# performing an infinite loop
passw_label = tk.Label(root, text = 'Password', font =
# for the window to display
('calibre',10,'bold'))
root.mainloop()
# creating a entry for password
passw_entry=tk.Entry(root, textvariable = passw_var,
font = ('calibre',10,'normal'), show = '*')

# creating a button using the widget


# Button that will call the submit function
sub_btn=tk.Button(root,text = 'Submit', command =
submit)
Text Widget
Text Widget is used where a user wants to insert multiline text fields.

Syntax :
entry = tk.Text(parent, options)

• Options of Text widgets are bg, bd, font, fg, height, width, cursor, padx, pady, relief,
textvariable & others such as.
• insetofftime – The time in milliseconds for which the cursor blink is off.
• insertontime – the time in milliseconds for which the cursor blink is on.
• state – defines if the widget will be responsive to mouse or keyboards movements.
• insertionwidth – defines the width of insertion character.
• yscrollcommand – to make the widget vertically scrollable.
• xscrollcommand – to make the widget horizontally scrollable.
Methods: The various methods provided by the entry widget are:

index(index) – To get the specified index.

insert(index) – To insert a string at a specified index.

see(index) – Checks if a string is visible or not at a given index.

get(startindex, endindex) – to get characters within a given range.

delete(startindex, endindex) – deletes characters within specified range.


Example…
import tkinter as tk
root = Tk()
l.pack()
# specify size of window. T.pack()
root.geometry("250x170") b1.pack()
b2.pack()
# Create text widget and specify size.
T = Text(root, height = 5, width = 52) # Insert The Fact.
T.insert(tk.END, Fact)
# Create label tk.mainloop()
l = Label(root, text = "Fact of the Day")
l.config(font =("Courier", 14))
Fact = """A man can be arrested in
Italy for wearing a skirt in public.""“

# Create button for next text.


b1 = Button(root, text = "Next", )

# Create an Exit button.


b2 = Button(root, text = "Exit",command = root.destroy)
Message Widget
The Message widget is used to show the message to the user. The message text
contains more than one line.

Syntax :
msg = tk.Message(parent, options)

Options of Message widgets are bg, bd, font, fg, height, width, cursor, padx, pady, relief,
textvariable etc.

from tkinter import * Example


root = Tk()
root.geometry("300x200")
w = Label(root, text ='GeeksForGeeks', font = "50")
w.pack()
msg = Message( root, text = "A computer science portal for
geeks")
msg.pack()
root.mainloop()
Messagebox Widget
The Messagebox widget is used display the message boxes in the python
applications. It requires separate module called “messagebox” import from tkinter.

Syntax :
messagebox.Function_Name(title, message [, options])
• Function_Name: This parameter is used to represents an appropriate message box function.
• title: This parameter is a string which is shown as a title of a message box.
• message: This parameter is the string to be displayed as a message on the message box.
Function_Name: There are functions or methods available in the messagebox widget.

• showinfo(): Show some relevant information to the user.


• showwarning(): Display the warning to the user.
• showerror(): Display the error message to the user.
• askquestion(): Ask question and user has to answered in yes or no.
• askokcancel(): Confirm the user’s action regarding some application activity.
• askyesno(): User can answer in yes or no for some action.
• askretrycancel(): Ask the user about doing a particular task again or not.
from tkinter import *
from tkinter import messagebox
root = Tk()
root.geometry("300x200")
w = Label(root, text ='GeeksForGeeks', font = "50")
w.pack()

messagebox.showinfo("showinfo", "Information")

messagebox.showwarning("showwarning", "Warning")

messagebox.showerror("showerror", "Error")

messagebox.askquestion("askquestion", "Are you sure?")

messagebox.askokcancel("askokcancel", "Want to continue?")

messagebox.askyesno("askyesno", "Find the value?")

messagebox.askretrycancel("askretrycancel", "Try again?")

root.mainloop()
Output After clicking “Click me”
Python Code

from tkinter import *


import tkinter.messagebox
root = tkinter.Tk()

root.title("When you press a button the message will pop up")


root.geometry('500x300')

def onClick():
tkinter.messagebox.showinfo("Welcome to GFG.", "Hi I'm your message")

button = Button(root, text="Click Me", command=onClick, height=5, width=10)

button.pack(side='bottom')
root.mainloop()

You might also like