Class Tkinker WIDGETS
Class Tkinker WIDGETS
• b.grid(row=1,column=2,padx,pady)
• b.place(x=20,y=30,widht=100,height=200)
• There are various controls used in a GUI application. Such as:
Buttons
Labels
Scrollbars
radio buttons
text boxes
Button If you want to add a button in your application then Button widget will be used.
Canvas To draw a complex layout and pictures (like graphics, text, etc.)Canvas Widget will be used.
If you want to display a number of options as checkboxes then Checkbutton widget is used. It allows
CheckButton you to select multiple options at a time.
Entry To display a single-line text field that accepts values from the user Entry widget will be used.
In order to group and organize another widgets Frame widget will be used. Basically it acts as a
Frame container that holds other widgets.
Label To Provide a single line caption to another widget Label widget will be used. It can contain images too.
To provides commands to the user Menu widget will be used. Basically these commands are inside the
Menu Menubutton. This widget mainly creates all kinds of Menus required in the application.
Menubutton The Menubutton widget is used to display the menu items to the user.
The message widget mainly displays a message box to the user. Basically it is a multi-line text which is
Message non-editable.
Name of Widget Description
If you want the number of options to be displayed as radio buttons then the Radiobutton widget will be used.
Radiobutton You can select one at a time.
Scale Scale widget is mainly a graphical slider that allows you to select values from the scale.
Scrollbar To scroll the window up and down the scrollbar widget in python will be used.
The text widget mainly provides a multi-line text field to the user where users and enter or edit the text and it is
Text different from Entry.
Toplevel The Toplevel widget is mainly used to provide us with a separate window container
The SpinBox acts as an entry to the "Entry widget" in which value can be input just by selecting a fixed value of
SpinBox numbers.
The PanedWindow is also a container widget that is mainly used to handle different panes. Panes arranged
PanedWindo inside it can either Horizontal or vertical
w
LabelFrame The LabelFrame widget is also a container widget used to mainly handle the complex widgets.
• The Tkinter Frame widget is used to group and organize the widgets in a better and friendly way.
The Frame widget is basically a container (an invisible container) whose task is to hold other
widgets and arrange them with respect to each other. The Tkinter frame widget makes up
a rectangular region on the screen.
w = Frame(master, option=value)
master --- denotes the parent window.
Options --- change the look of the frame
Tkinter Frame Widget Options:
W = Button(master, options)
• master --- denotes the parent window.
• Options --- change the look of the buttons
Option name Description
This option indicates the background of the button at the time when the mouse hovers
activebackground
the button.
The command option is used to set the function call which is scheduled at the time when
command
the function is called.
This option mainly represents the font color of the button when the mouse hovers the
activeforeground
button.
This option indicates the height of the button. This height indicates the number of text
height
lines in the case of text lines and it indicates the number of pixels in the case of images.
Highlightcolor This option indicates the highlight color when there is a focus on the button
This option is used to indicate the way by which the multiple text lines are represented. For left
Justify justification, it is set to LEFT and it is set to RIGHT for the right justification, and CENTER for the
center justification.
Padx This option indicates the additional padding of the button in the horizontal direction.
Pady This option indicates the additional padding of the button in the vertical direction.
This option specifies the width of the button. For textual buttons, It exists as a number of
Width
letters or for image buttons it indicates the pixels.
In the case, if this option's value is set to a positive number, the text lines will be wrapped in
Wraplength
order to fit within this length.
This option's value set to DISABLED to make the button unresponsive. The ACTIVE mainly
State
represents the active state of the button.
Tkinter Button Widget Example
• we created a simple window of given width and height. Then we added a button widget to it with
providing the window created as the master of that button and adding a text for the button.
win.geometry("200x100")
win.mainloop()
Tkinter Button Widget - Add style and Event handler
import tkinterfrom tkinter
import *from tkinter import
messageboxtop = Tk()
top.geometry("300x150")
def click():
messagebox.showinfo("Hello", "Green Button clicked")
• we have added some styling using different options and we have added an event handler to handle the click
event of the 3rd button.
• So whenever you will make a click on the button with text Green, you will see a Tkinter messagebox with a
message.
Python Tkinter Entry Widget
• If you need to get a little bit of text from a user, like a name or an email address or a contact number
then use an Entry widget.
The Entry widget is mainly used to display a small text box that the user can type some
text into.
There are the number of options available to change the styling of the Entry Widget.
It is important to note that the Entry widget is only used to get a single-line text from the
user because in the case of multiline text the text widget will be used.
This widget is mainly used to accept text strings from the user.
w = Entry(master, option=value)
master --- denotes the parent window.
Options --- change the styling of the entry widget
Tkinter Entry Widget Options:
Option Name Description
Bg This option is used for the background color of the widget.
Bd This option is used for the width of the border in pixels. Its default value is 2 pixels.
This option helps in changing the mouse pointer to the cursor type and set it to the
cursor
arrow, dot, etc.
It is important to note that By Default, the text that is written inside the entry box
exportselection will get automatically copied to the clipboard. If you do not want to copy the text
then set the value of exportselection to 0.
Fg This option is used to indicate the color of the text.
Font This option is used to represent the font type of the text
This option is used to represent the color to display in the traversal highlight
highlightbackground
region when the widget does not have the input focus.
This option is used to represent the color to use for the traversal highlight
highlightcolor
rectangle which is drawn around the widget when the widget has an input focus.
This option is used to specify how the text is organized in the case if the text
justify
contains multiple lines.
Option Name Description
This option is used to indicate the type of border. The default value of this option
Relief
is FLAT. It has more values like GROOVE, RAISED,RIGID.
Selectbackground This option is used to indicate the background color of the selected text.
Selectforeground It is used to set the font of the selected task.
selectborderwidth This option indicates the width of the border to display around the selected task
Width This option indicates the width of the image or width of text to display.
With the help of this option, you will be able to retrieve the current text from your
textvariable
entry widget, you need to set this option to an instance of the StringVar class.
This option is used to show the entry text of some other type instead of the string. For
Show
example, we type the password using stars (*).
You can link the entry widget to the horizontal scrollbar if you want the user to enter
xscrollcommand
more text rather then the actual width of the widget.
This option mainly represents the color to use as a background in the area covered by
insertbackground the insertion cursor. and thus this color will normally override the normal background
for the widget.
Tkinter Entry Widget Methods:
This method is mainly used to insert the specified string(s) before the character
insert(index, s)
placed at the specified index
This method mainly sets the anchor index position to the character specified by the
select_form(index)
index.
select_range(start, end) This method is used to select the characters to exist between the specified range
This method mainly selects all the characters from the beginning to the specified
select_to(index)
index
xview(index) This method is used to link the entry widget to a horizontal scrollbar
xview_scroll(number, what) This method is mainly used to make the entry widget scrollable horizontally
Tkinter Entry Widget Example
win.mainloop()
In the above code example, we have done the following:
1.Create Tkintor Labels to name the text input fields. For all the 3 text input fields(Entry
widget), we have created three labels.
2.We have used the place() geometry manager to place the labels on the application
window.
3.Then we have created a Button which is the submit button. And used the place()
geometry manager to position it on the application GUI.
4.And finally, we have the three Entry widgets which will create the three text input fields.
And used the place() geometry manager to position it on the application GUI.
Output:
Python Tkinter Label Widget
• The label widget in Tkinter is used to display boxes where you can place your images and text.
The label widget is mainly used to provide a message about the other
widgets used in the Python Application to the user.
You can change or update the text inside the label widget anytime you want.
This widget uses only one font at the time of displaying some text.
You can perform other tasks like underline some part of the text and you can
also span text to multiple lines.
w = Label(master, option=value)
master --- denotes the parent window.
Options --- change the configure the text.
Tkinter Label Widget Options
Name of the option Description
This option is mainly used for controlling the position of text in the provided
anchor widget size. The default value is CENTER which is used to align the text in center
in the provided space.
This option is used for the border width of the widget. Its default value is 2
bd
pixels.
This option is used to set the bitmap equals to the graphical object specified so
bitmap
that now the label can represent the graphics instead of text.
bg This option is used for the background color of the widget.
This option is used to specify what type of cursor to show when the mouse is
cursor
moved over the label. The default of this option is to use the standard cursor.
This option is used to specify the foreground color of the text that is written
fg
inside the widget.
font This option specifies the font type of text inside the label.
height This option indicates the height of the widget
Name of the option Description
win = Tk()
label.pack()
win.mainloop()
Tkinter Label Widget - Another Example
from tkinter import *
win = Tk()
win.geometry("400x250")
#creating a label
username = Label(win, text = "Username").place(x = 30,y = 50)
#creating second label
password = Label(win, text = "Password").place(x = 30, y = 90)
submitbutton = Button(win, text = "Submit",activebackground = "red", activeforeground = "blue").place(x = 30, y = 120)
e1 = Entry(win,width = 20).place(x = 100, y = 50)
e2 = Entry(win, width = 20).place(x = 100, y = 90)
win.mainloop()
Output:
• Whenever you will run the above code, after putting values into username and password label,
when you will click on the submit button then its color gets changed to red.
Python Tkinter Listbox Widget
• Tkinter Listbox widget in Python which is used to display different types of items to the user in
form of a List inside a box and the user can select the items.
The items contain the same type of font and the same font color.
It is important to note here that only text items can be placed inside a Listbox widget.
From this list of items, the user can select one or more items according to the
requirements.
bd This option is used to represent the size of the border. The default value is 2 pixels.
cursor With the help of this option, the mouse pointer will look like the cursor type like dot, arrow, etc.
This option is used to represents the count of the lines shown in the Listbox. The default value of
height
this option is 10.
highlightcolor This option is used to indicate the color of the Listbox items when the widget is under focus.
Name of Option Description
relief This option indicates the type of border. The default value is SUNKEN.
selectbackground This option is used to indicate the background color that is used to display the selected text.
This option is used to determine the number of items that can be selected from the list. It can set
selectmode
to BROWSE, SINGLE, MULTIPLE, EXTENDED.
xscrollcommand This option is used to let the user scroll the Listbox horizontally.
yscrollcommand This option is used to let the user scroll the Listbox vertically.
Tkinter ListBox Widget Methods:
Method Description
activate(index) This method is mainly used to select the lines at the specified index.
This method is used to return a tuple containing the line numbers of the selected element or
curselection()
elements, counting from 0. If nothing is selected, return an empty tuple.
delete(first, last = None) This method is used to delete the lines which exist in the given range.
get(first, last = None) This method is used to get the list of items that exist in the given range.
index(i) This method is used to place the line with the specified index at the top of the widget.
This method is used to insert the new lines with the specified number of elements before the
insert(index, *elements)
specified index.
Method Description
This method is used to return the index of the nearest line to the y
nearest(y)
coordinate of the Listbox widget.
This method is used to adjust the position of the Listbox to make the lines
see(index)
specified by the index visible.
This method returns the number of lines that are present in the Listbox
size()
widget.
xview() This method is used to make the widget horizontally scrollable.
This method is used to make the Listbox horizontally scrollable by the fraction
xview_moveto(fraction)
of the width of the longest line present in the Listbox.
This method is used to make the listbox horizontally scrollable by the number
xview_scroll(number, what)
of characters specified.
yview() This method allows the Listbox to be vertically scrollable.
This method is used to make the listbox vertically scrollable by the fraction of
yview_moveto(fraction)
the width of the longest line present in the Listbox.
This method is used to make the listbox vertically scrollable by the number of
yview_scroll (number, what)
characters specified.
Tkinter ListBox Widget Example
from tkinter import *
top = Tk()
top.geometry("200x250")
lbl = Label(top, text="List of Programming Languages")
listbox = Listbox(top)
listbox.insert(1,"Python") Output:
listbox.insert(2, "Java")
listbox.insert(3, "C")
listbox.insert(4, "C++")
lbl.pack()
listbox.pack()
top.mainloop()
Python Tkinter Message Widget
• The Tkinter Message Widget in Python is mainly used to show some message to the user who is
using the application.
w = message(master, option=value)
master --- denotes the parent window.
Options --- change the look of the Message.
Tkinter Message Widget Options:
It is mainly used to decide the exact position of the text within the provided space. The default value of this
anchor
option is CENTER.
bd This option is used to indicate the border width of the widget. The default value of this is 2 pixels.
In order to display graphics on the widget, this option will be used. You can set it to any graphical or image
bitmap
object.
With the help of this option, the mouse pointer will be changed to a specific cursor type like an arrow, dot,
cursor
etc.
fg This option is used to indicate the font color of the widget text.
font This option is used to indicate the font type of the widget text.
height This option is used to indicate the vertical dimension of the message widget.
image This option is used to indicate the image on the widget.
Name of the option Description
justify This option is used for the justification of the text on the widget. It can be CENTER, LEFT, RIGHT
relief This option is used to specify the border type. Its default value is FLAT
This option can be set to an existing number in order to specify that nth letter of the string will be underlined. Its
underline
default value is -1 which indicates no underline.
If you want to display one or more lines of text in a label widget you need to set this option to a string containing
text
the text. You can use "\n" just in order to enter multiple lines
This option is used to control the text represented by the widget. The textvariable can be set to the text that is
textvariable
needed to be shown on the widget.
This option is used to indicate the horizontal dimension of the widget in the number of characters and not in
width
pixels.
This option is used to wrap the text to the number of lines just by setting this option to the desired number so
wraplength
that each line contains only that number of characters.
Tkinter Message Widget Example
from tkinter import *
win = Tk()
win.geometry("300x200")
msg.pack()
win.mainloop()
Output:
• In the code example above, we have created a simple label widget and a message
widget with some text message in it.
Python Tkinter Radiobutton Widget
• Tkinter radiobutton widget is used to implement multiple-choice options that are mainly created in
user input forms.
This widget offers multiple selections to the user and allows the user to select only
one option from the given ones. Thus it is also known as implementing one-of-many
selection in a Python Application.
Also, different methods can also be associated with radiobutton.
You can also display multiple line text and images on the radiobutton.
Each radiobutton displays a single value for a particular variable.
You can also keep a track of the user's selection of the radiobutton because it is
associated with a single variable
w = Radiobutton(master, option=value)
master --- denotes the parent window.
Options --- change the look of the Radiobutton.
Tkinter Radiobutton Widget Options:
Name of the option Description
This option is used to represent the exact position of the text within the widget, in the case of the widget
anchor
contains more space than the requirement of the text. The default value of this option is CENTER.
bitmap If you want to display graphics on the widget then you can set this widget to any graphical or image object.
This option is used to set the procedure which must be called every time when the state of the radiobutton is
command
changed.
cursor This option will convert the mouse pointer to the specified cursor type and it can be set to an arrow, dot, etc.
font This option is used to represent the font type of the text of the widget.
fg This option is used to represent the foreground color of the text of the widget.
Name of the option Description
This option indicates the horizontal dimension of the widget and it is represented as the number of
width
characters.
highlightcolor This option is used to represent the color of the focus highlight when the widget is under the focus
highlightbackground This option is used to represent the color of the focus highlight when the widget is not under the focus.
image If you want to display an image on the widget then this option will be set to an image rather than the text
This option is used to represent the justification of the multiline text. The default value is CENTER. Other
justify
values are LEFT, RIGHT.
relief This option is used to represent the type of border. The default value is FLAT.
selectimage This option indicates the image to be displayed on the radiobutton when it is selected
This option is used to represent the state of the radio button. The default state of the Radiobutton
state
is NORMAL. You can also set the state to DISABLED in order to make the radiobutton unresponsive.
This option is used to control the text represented by the widget. The textvariable can be set to the
textvariable
text that is needed to be shown on the widget.
This option can be set to an existing number in order to specify that nth letter of the string will be
underline
underlined. Its default value is -1 which indicates no underline
This option is also known as the control variable which is used to keep the track of user's choices.
variable
Thus this variable is shared among all radiobuttons.
value This option of each radiobutton is assigned to the control variable when it is turned on by the user.
This option is used to wrap the text to the number of lines just by setting this option to the desired
wraplength
number so that each line contains only that number of characters.
Tkinter Radiobutton Widget Methods:
deselect() This method is used to deselect or turns off the radio button
This method is generally used to flash the radio button between its
flash()
normal and active colors many times.
Tkinter Radiobutton Widget Example
#firstly ImportTkinter module
from tkinter import *
from tkinter.ttk import *
# Creating parent Tkinter window
win = Tk()
win.geometry("200x200")
# let us create a Tkinter string variable
# that is able to store any string value
Output:
v = StringVar(win, "1")
# here is a Dictionary to create multiple buttons
options = {" Option A" : "1", "Option B" : "2", “Option C" : "3", "Option D" : "4" }
# We will use a Loop just to create multiple
# Radiobuttons instaed of creating each button separately
for (txt, val) in options.items():
Radiobutton(win, text=txt, variable=v, value=val).pack(side = TOP, ipady = 4)
mainloop()
Tkinter Radiobutton Widget Another Example
from tkinter import *
from tkinter.ttk import *
win= Tk()
win.geometry('200x200')
v = StringVar(win, "1")
# we will add Style class to add style to Radiobutton
style = Style(win)
style.configure("TRadiobutton", background = "light blue",foreground = "orange", font = ("arial", 14, "bold"))
# Dictionary to create multiple buttons
values = {"RadioButton 1" : "1","RadioButton 2" : "2", "RadioButton 3" : "3" }
Output:
for (text, value) in values.items():
Radiobutton(win, text = text, variable = v, value = value).pack(side = TOP, ipady = 3)
mainloop()
Python Tkinter Scrollbar Widget
• The Tkinter Scrollbar widget in Python, using which we can add a scrollbar to the user interface of
our Tkinter application.
To scroll up or down or right or left the content in a Python desktop application, the
Tkinter Scrollbar widget is used.
To scroll the content of other widgets like Listbox, canvas, etc we use this widget.
Both Horizontal and Vertical scrollbars can be created in the Trinket Entry widget.
• The syntax of the Scrollbar widget is given below:
w = Scrollbar(master, option=value)
master --- denotes the parent window.
Options --- change the look of the Scrollbar.
Tkinter Scrollbar Widget Options
activebackground This option represents the background color of the widget when it is under focus.
bd This option represents the border size of the widget. The default value is 2 pixels.
With the help of this option, the mouse pointer will be changed to a specific cursor type and it can
cursor
be an arrow, dot, etc.
This option will be set to the procedure associated which is called every time the scrollbar is
command
moved.
This option mainly represents the border width around the arrowheads and the slider. The default
elementborderwidth
value of this option is -1.
highlightbackground This option indicates the highlight color when the widget is not under the focus
highlightcolor This option indicates the highlight color when the widget is under the focus
This option is used to control the behavior of the scroll jump. If this option is set to 1, then the
jump
callback is called at the time when the user releases the mouse button.
This option can be set to either horizontal or vertical depending upon the orientation of the
orient
scrollbar.
troughcolor This option is used to set the color for the trough
By default, you can tab the focus through this widget. If you don't want this behavior you can
takefocus
set this option to 0.
This option is mainly used to tell the duration up to which the button is to be pressed before
repeatdelay
the slider starts moving in that direction repeatedly. its default value is 300 ms
This method is used to connect the scrollbar to any other widget. That
set(first, last) is yscrollcommand or xscrollcommand of the other widget to this method.
Tkinter Scrollbar Widget Example
mainloop()
Output:
• As you can see in the above code, we have created a Listbox widget with numbers
as list items in it.
• Then we have created a Scrollbar widget and have used the yscrollcommand
option of the Listbox widget to set the Scrollbar widget with it. We have used the
Scrollbar widget's set function here.
Python Tkinter Text Widget
• The Tkinter Text widget in Python. If you want a text-editor in your desktop application then you can use
the Tkinter Text widget.
The text widget is used to provide a multiline textbox (input box) because in Tkinter single-
line textbox is provided using Entry widget.
You can use various styles and attributes with the Text widget.
You can also use marks and tabs in the Text widget to locate the specific sections of the
text.
Media files like images and links can also be inserted in the Text Widget.
There are some variety of applications where you need multiline text like sending
messages or taking long inputs from users, or to show editable long format text content in
application, etc. use cases are fulfilled by this widget.
Thus in order to show textual information, we will use the Text widget.
Method Description
index(index) This method is used to get the specified index.
This method returns true or false on the basis that
see(index)
if the string is visible or not at the specified index.
This method is used to insert a string at the
insert(index,string)
specified index.
This method returns the characters in the specified
get(startindex,endindex)
range
This method deletes the characters in the specified
delete(startindex,endindex)
range
Methods for Tag Handling
• Mainly tags are used to configure different areas of the text widget separately. Tag is basically
the name given to separate areas of the text. Some Methods for handling tags are given below:
Method Description
Method Description
This method is mainly used to get the index of the
index(mark)
mark specified.
This method is used to get all the names of the
mark_names()
mark in the range in the text widget.
To get the gravity of the given mark this method
mark_gravity(mark, gravity)
will be used.
This method is used to inform the new position of
mark_set(mark, index)
the given mark.
• Tkinter Spinbox widget in Python with its syntax and few examples. The Spinbox widget in Tkinter in
Python is used to select a value from the specified given range of values.
• It is different from the Tkinter Scale widget (Scale widget being more stylish) in terms of style, but more or
less, fulfils the same purpose.
• For example, when you want to have a dropdown of numerical values like year of birth (from 1950 to 2020)
or a dropdown for user to choose their age, we can use the Tkinter Spinbox widget.
This widget is an alternative to Entry widget, when we want user to enter a numeric value within a
specific range.
This widget is used only in the case where users need to chose from a given range of choices.
This option is used to indicate the associated function with the widget which is called
command
every time the state of the widget is changed.
With the help of this option, your mouse pointer type can be changed to the cursor type
cursor
that is assigned to this option.
activebackground This option indicates the background color of the widget when it is under the focus
disabledbackground This option is used to indicate the background color of the widget when it is disabled.
disabledforeground This option is used to indicate the foreground color of the widget when it is disabled.
format This option is mainly used for the format string. There is no default value of this option.
This option specifies the alignment of multiple lines in the label. The default value is LEFT.
justify
Other values are RIGHT and CENTER.
relief This option indicates the type of border. The default value of this option is SUNKEN.
This option is used to represent the state of the widget. The default value of this option
state
is NORMAL. Other values are "DISABLED", "read-only", etc.
validate This option is used to control how to validate the value of the widget
textvariable It is a control variable that is used to control the text of the widget
Name of the Option Description
This option represents the maximum limit of the widget value. The other value is
to
specified by the from_ option
This option is mainly used to control the autorepeat button. The value here is in
repeatdelay
milliseconds.
This option is similar to repeatdelay option. The value here is also given in
repeatinterval
milliseconds.
This option is associated with the function callback that is used for the validation of the
validatecommand
content of the widget.
This option is mainly used with the set() method of the scrollbar widget to make
xscrollcommand
this widget horizontally scrollable
wrap This option is mainly used to wrap-up the up and down button of the Spinbox
width This option indicates the width of the widget.
vcmd This option is similar to validatecommand.
values This option represents the tuple which contains the values for the widget
Tkinter Spinbox Widget Methods:
win.mainloop()
Python Tkinter Menu Widget
• The Tkinter Menu widget is used to create different types of menus in Python Application.
The following types of menus can be created using the Tkinter Menu widget: pop-up, pull-down, and
top level.
Top-level menus are those menus that are displayed just under the title bar of the root or any other
top-level windows. For example, all the websites have a top navigation menu just below the URL bar
in the browser.
Menus are commonly used to provide convenient access to options like opening any file, quitting
any task, and manipulating data in an application.
w = menu(master, option=value)
master --- denotes the parent window.
Options --- change the look of the Menu.
Tkinter Menu Widget Options:
Name of the Option Description
This option is used to indicate the background color of the widget when the widget is under the
activebackground
focus.
activeforeground This option indicates the font color of text of the widget when the widget has the focus.
This option is used to indicate the width of the border of the widget when it is under the
activeborderwidth
mouse(when it is active). The default value of this option is 1 pixel.
bd This option is used to indicate the border width of the widget
cursor This option indicates the cursor when the mouse hovers the menu.
Disabledforeground This option indicates the text color of the widget when the widget is disabled
Name of the Option Description
font This option is used to indicate the font type of the text of widget
relief This option is used to specify the border type. Its default value is RAISED.
postcommand This option can be set to any of the function which is called when the mouse hovers the menu.
The choices in the menu start from position 1 by default. But If we set the tearoff=1,
tearoff
then choices will start taking place from 0th position.
This option indicates the color used to display the checkbutton or radiobutton when they are
selectcolor
selected.
title This option is set to the title of the window if you want to change the title of the window.
Tkinter Menu Widget Methods:
add_seperator() This method is used to add the separator line to the menu items.
add(type, options) This method is used to add the specific menu item to the menu.
Name of method Description
delete(startindex, endindex) This method is used to delete the menu items that exist in the specified range.
This method is used to configure a menu item that is identified by the given
entryconfig(index, options)
index.
index(item) This method is used to get the index of the specified menu item.
This method is used to invoke the associated operation with the choice given at
invoke(index)
the specified index.
type(index) This method is used to get the type of choice specified by the index.
Tkinter Menu Widget Example
from tkinter import *
root = Tk()
def hello():
print("hello!")
Output:
menubar = Menu(root)
menubar.add_command(label="Hello StudyTonight!", command=hello)
menubar.add_command(label="Quit!", command=root.quit)
root.config(menu=menubar)
root.mainloop()
Python Tkinter Menubutton Widget
• the Tkinter Menubutton widget in Python which is used to create a dropdown menu which can be
clicked by the user to see the
This widget is used to provide various types of menus in the Python Application.
It is important to note that every Menubutton in an application is associated with
a Menu widget and that in return can display the choices for that
menubutton whenever the user clicks on it.
The Tkinter Menubutton widget provides the user with an option to select
the appropriate choice that exists within the application.
w = menubutton(master, option=value)
master --- denotes the parent window.
Options --- change the look of the Menubutton.
Tkinter Menubutton Widget Options:
Option name Description
This option indicates the background color of the menubutton at the time when the mouse hovers the
activebackground
menubutton.
Bd This option is used to represent the width of the border in pixels. The default value is 2 pixels.
Bitmap This option will be set to the graphical content which is to be displayed to the widget.
Cursor This option indicates the cursor when the mouse hovers the menubutton.
Activeforeground This option mainly represents the font color of the widget at the time when the widget is under the focus