Ch - 1 Advance Python
Ch - 1 Advance Python
Sem - 6
1 Python Tkinter
Topics Covered
Introduction:
• Python offers multiple options for developing GUI (Graphical User Interface).
• Out of all the GUI methods, Tkinter is the most commonly used method.
• It is a standard Python interface to the Tk GUI toolkit shipped with Python.
• Python with Tkinter is the fastest and easiest way to create the GUI applications.
Creating a GUI using Tkinter is an easy task.
• Most of you write a code and run it in a command-line terminal or an IDE (Integrated
Development Environment), and the code produces an output based on what you expect
out of it either on the terminal or on the IDE itself.
• However, what if you want your system to have a fancy looking user-interface or
maybe your application (use-case) requires you to have a GUI.
What is GUI?
• GUI is nothing but a desktop app that provides you with an interface that helps you to
interact with the computers and enriches your experience of giving a command
(command-line input) to your code.
• They are used to perform different tasks in desktops, laptops, and other electronic
devices, etc.
Some of the applications where the power of GUI is utilized are:
• Creating a Calculator which would have a user-interface and functionalities that persists
in a calculator.
• Text-Editors, IDE's for coding are on a GUI app.
• Sudoku, Chess, Solitaire, etc.., are games that you can play are GUI apps.
• Chrome, Firefox, Microsoft Edge, etc. used to surf the internet is a GUI app.
• Another interesting use-case would be - A GUI for controlling a Drone from your
laptop, and the GUI would probably have buttons to maneuver the Drone along with a
screen that would show the camera feed captured by the Drone in a real-time.
Page 1 of 2
Subject: ADVANCE PYTHON Chapter - 1 B.C.A. Sem - 6
Let's see some of the frameworks that Python provides to develop a GUI:
• PyQT : is one of the favored cross-platform Python bindings implementing the Qt
library for the Qt application development framework. currently; PyQT is available for
almost all operating systems like Unix/Linux, Windows, Mac OS X.
• Kivy is for the creation of new user interfaces and is an OpenGL ES 2 accelerated
framework. Much like PyQt, Kivy also supports almost all platforms like Windows,
Mac OSX, Linux, Android, iOS. It is an open-source framework and comes with over
20 pre-loaded widgets in its toolkit.
• Jython is a Python port for Java, which gives Python scripts seamless access to Java
class libraries on the local machine.
• WxPython, initially known as WxWindows (now as a WxWidgets library), is an open-
source abstract-level wrapper for cross-platform GUI library. It is implemented as a
Python expansion module. With WxPython, you, as a developer, can create native
applications for Windows, Mac OS, and UNIX.
• And finally, the framework that is the discussion in our whole chapter is Tkinter!
• Tkinter commonly comes bundled with Python, using Tk and is Python's standard GUI
framework. It is famous for its simplicity and graphical user interface. It is open-source
and available under the Python License.
Note: Tkinter comes pre-installed with Python3, and you need not bother about
installing it.
• Let's break down the above flow diagram and understand what each component is
handling!
• First, you import the key component, i.e., the Tkinter module.
Page 2 of 2
Subject: ADVANCE PYTHON Chapter - 1 B.C.A. Sem - 6
• As a next step, you initialize the window manager with the tkinter.Tk () method and
assign it to a variable.
• This method creates a blank window with close, maximize, and minimize buttons on
the top as a usual GUI should have.
• Then as an optional step, you will rename the title of the window as you like
with window.title (title_of_the_window).
• Next, you make use of a widget called Label, which is used to insert some text into the
window.
• Then, you make use of Tkinter's geometry management attribute called pack () to
display the widget in size it requires.
• Finally, as the last step, you use the mainloop () method to display the window until you
manually close it. It runs an infinite loop in the backend.
• Creating a GUI application using Tkinter is an easy task.
• All you need to do is perform the following steps −
1. Import the Tkinter module.
2. Create the GUI application main window (Container).
3. Add one or more of the above-mentioned widgets to the GUI application.
4. Enter the main event loop to take action against each event triggered by the user.
• Importing Tkinter is same as importing any other module in the Python code.
Note: that the name of the module in Python 2.x is ‘Tkinter’ and in Python 3.x it is
‘tkinter’.
Import library as import tkinter
• There are two main methods used which the user needs to remember while creating the
Python application with GUI.
1. Tk(screenName=None, baseName=None, className=’Tk’, useTk=1):
• To create a main window, tkinter offers a method ‘Tk(screenName=None,
baseName=None, className=’Tk’, useTk=1)’.
• To change the name of the window, you can change the className to the desired one.
• The basic code used to create the main window of the application is:
m=tkinter.Tk() where m is the name of the main window object
2. Mainloop ():
• There is a method known by the name mainloop () is used when your application is
ready to run.
• Mainloop () is an infinite loop used to run the application, wait for an event to occur
and process the event as long as the window is not closed.
m.mainloop ()
Example:
import tkinter
m = tkinter.Tk()
'''
Widgets are added here
'''
m.mainloop ()
Page 3 of 3
Subject: ADVANCE PYTHON Chapter - 1 B.C.A. Sem - 6
Tkinter widgets:
• There are various widgets like button, canvas, checkbutton, entry, etc. that are used to
build the python GUI applications.
SN Widget Description
1 Button The Button is used to add various kinds of buttons to the
python application.
2 Canvas The canvas widget is used to draw the canvas on the
window.
3 Checkbutton The Checkbutton is used to display the CheckButton on the
window.
4 Entry The entry widget is used to display the single-line text field
to the user. It is commonly used to accept user values.
5 Frame It can be defined as a container to which, another widget
can be added and organized.
6 Label A label is a text used to display some message or
information about the other widgets.
7 ListBox The ListBox widget is used to display a list of options to
the user.
8 Menubutton The Menubutton is used to display the menu items to the
user.
9 Menu It is used to add menu items to the user.
10 Message The Message widget is used to display the message-box to
the user.
11 Radiobutton The Radiobutton is different from a checkbutton. Here, the
user is provided with various options and the user can
select only one option among them.
12 Scale It is used to provide the slider to the user.
13 Scrollbar It provides the scrollbar to the user so that the user can
scroll the window up and down.
14 Text It is different from Entry because it provides a multi-line
text field to the user so that the user can write the text and
edit the text inside it.
14 Toplevel It is used to create a separate window container.
15 Spinbox It is an entry widget used to select from options of values.
Page 4 of 4
Subject: ADVANCE PYTHON Chapter - 1 B.C.A. Sem - 6
Syntax:
widget.pack(options)
Example:
from tkinter import *
parent = Tk()
redbutton = Button(parent, text = "Red", fg = "red")
redbutton.pack( side = LEFT)
blackbutton = Button(parent, text = "Black", fg = "black")
blackbutton.pack( side = RIGHT )
bluebutton = Button(parent, text = "Blue", fg = "blue")
bluebutton.pack( side = TOP )
greenbutton = Button(parent, text = "Green", fg = "green")
greenbutton.pack( side = BOTTOM)
parent.mainloop()
Page 5 of 5
Subject: ADVANCE PYTHON Chapter - 1 B.C.A. Sem - 6
Output:
• A list of possible options that can be passed inside the grid() method is given below.
1. Column:
• The column number in which the widget is to be placed.
• The leftmost column is represented by 0.
2. Columnspan:
• The width of the widget.
• It represents the number of columns up to which, the column is expanded.
3. ipadx,ipady:
• It represents the number of pixels to pad the widget inside the widget's border.
4. padx,pady:
• It represents the number of pixels to pad the widget outside the widget's border.
5. row:
• The row number in which the widget is to be placed. The topmost row is represented by
0.
6. rowspan:
• The height of the widget, i.e. the number of the row up to which the widget is
expanded.
7. Sticky:
• If the cell is larger than a widget, then sticky is used to specify the position of the
widget inside the cell.
• It may be the concatenation of the sticky letters representing the position of the widget.
• It may be N, E, W, S, NE, NW, NS, EW, ES.
Example:
from tkinter import *
parent = Tk()
name = Label(parent,text = "Name").grid(row = 0, column = 0)
e1 = Entry(parent).grid(row = 0, column = 1)
password = Label(parent,text = "Password").grid(row = 1, column = 0)
Page 6 of 6
Subject: ADVANCE PYTHON Chapter - 1 B.C.A. Sem - 6
e2 = Entry(parent).grid(row = 1, column = 1)
submit = Button(parent, text = "Submit").grid(row = 4, column = 0)
parent.mainloop()
Output:
Example:
from tkinter import *
top = Tk()
top.geometry("400x250")
name = Label(top, text = "Name").place(x = 30,y = 50)
email = Label(top, text = "Email").place(x = 30, y = 90)
password = Label(top, text = "Password").place(x = 30, y = 130)
e1 = Entry(top).place(x = 80, y = 50)
Page 7 of 7
Subject: ADVANCE PYTHON Chapter - 1 B.C.A. Sem - 6
Output:
1. Tkinter Button:
• The button widget is used to add various types of buttons to the python application.
• Python allows us to configure the look of the button according to our requirements.
• Various options can be set or reset depending upon the requirements.
• We can also associate a method or function with a button which is called when the
button is pressed.
Syntax:
w = Button(parent, options)
Page 8 of 8
Subject: ADVANCE PYTHON Chapter - 1 B.C.A. Sem - 6
10 Highlightcolor The color of the highlight when the button has the focus.
11 Image It is set to the image displayed on the button.
12 justify It illustrates the way by which the multiple text lines are
represented. It is set to LEFT for left justification,
RIGHT for the right justification, and CENTER for the
center.
13 Padx Additional padding to the button in the horizontal
direction.
14 pady Additional padding to the button in the vertical
direction.
15 Relief It represents the type of the border. It can be SUNKEN,
RAISED, GROOVE, and RIDGE.
17 State This option is set to DISABLED to make the button
unresponsive. The ACTIVE represents the active state
of the button.
18 Underline Set this option to make the button text underlined.
19 Width The width of the button. It exists as a number of letters
for textual buttons or pixels for image buttons.
20 Wraplength If the value is set to a positive number, the text lines will
be wrapped to fit within this length.
Example:
#python application to create a simple button
Output:
Example:
from tkinter import *
top = Tk()
top.geometry("200x100")
def fun():
messagebox.showinfo("Hello", "Red Button clicked")
Page 9 of 9
Subject: ADVANCE PYTHON Chapter - 1 B.C.A. Sem - 6
b1.pack(side = LEFT)
b2.pack(side = RIGHT)
b3.pack(side = TOP)
b4.pack(side = BOTTOM)
top.mainloop()
Output:
2. Tkinter Canvas
• The canvas widget is used to add the structured graphics to the python application.
• It is used to draw the graph and plots to the python application.
Syntax:
w = canvas (parent, options)
Page 10 of 10
Subject: ADVANCE PYTHON Chapter - 1 B.C.A. Sem - 6
SN Option Description
1 bd The represents the border width. The default width is 2.
2 bg It represents the background color of the canvas.
3 confine It is set to make the canvas unscrollable outside the
scroll region.
4 cursor The cursor is used as the arrow, circle, dot, etc. on the
canvas.
5 height It represents the size of the canvas in the vertical
direction.
6 highlightcolor It represents the highlight color when the widget is
focused.
7 relief It represents the type of the border. The possible values
are SUNKEN, RAISED, GROOVE, and RIDGE.
8 scrollregion It represents the coordinates specified as the tuple
containing the area of the canvas.
9 width It represents the width of the canvas.
10 xscrollincrement If it is set to a positive value. The canvas is placed only
to the multiple of this value.
11 xscrollcommand If the canvas is scrollable, this attribute should be the
.set() method of the horizontal scrollbar.
12 yscrollincrement Works like xscrollincrement, but governs vertical
movement.
13 yscrollcommand If the canvas is scrollable, this attribute should be the
.set() method of the vertical scrollbar.
Example:
from tkinter import *
top = Tk()
top.geometry("200x200")
#creating a simple canvas
c = Canvas(top,bg = "pink",height = "200")
c.pack()
top.mainloop()
Output:
Page 11 of 11
Subject: ADVANCE PYTHON Chapter - 1 B.C.A. Sem - 6
Output:
Prime Ministers of India | List of Prime Minister of India (1947-2020)
3. Tkinter Checkbutton:
• The Checkbutton is used to track the user's choices provided to the application.
• In other words, we can say that Checkbutton is used to implement the on/off selections.
• The Checkbutton can contain the text or images.
• The Checkbutton is mostly used to provide many choices to the user among which, the
user needs to choose the one.
• It generally implements many of many selections.
Syntax:
w = checkbutton(master, options)
SN Option Description
1 activebackground It represents the background color when the checkbutton
is under the cursor.
2 activeforeground It represents the foreground color of the checkbutton
when the checkbutton is under the cursor.
3 bg The background color of the button.
4 bitmap It displays an image (monochrome) on the button.
5 bd The size of the border around the corner.
6 command It is associated with a function to be called when the
Page 12 of 12
Subject: ADVANCE PYTHON Chapter - 1 B.C.A. Sem - 6
Methods:
• The methods that can be called with the Checkbuttons are described in the following
table.
Page 13 of 13
Subject: ADVANCE PYTHON Chapter - 1 B.C.A. Sem - 6
SN Method Description
1 deselect() It is called to turn off the checkbutton.
2 flash() The checkbutton is flashed between the active and normal colors.
3 invoke() This will invoke the method associated with the checkbutton.
4 select() It is called to turn on the checkbutton.
5 toggle() It is used to toggle between the different Checkbuttons.
Example:
from tkinter import *
top = Tk()
top.geometry("200x200")
checkvar1 = IntVar()
checkvar2 = IntVar()
checkvar3 = IntVar()
chkbtn1.pack()
chkbtn2.pack()
chkbtn3.pack()
top.mainloop()
Output:
Page 14 of 14
Subject: ADVANCE PYTHON Chapter - 1 B.C.A. Sem - 6
4. Tkinter Entry:
• The Entry widget is used to provide the single line text-box to the user to accept a value
from the user.
• We can use the Entry widget to accept the text strings from the user.
• It can only be used for one line of text from the user.
• For multiple lines of text, we must use the text widget.
Syntax:
w = Entry (parent, options)
Page 15 of 15
Subject: ADVANCE PYTHON Chapter - 1 B.C.A. Sem - 6
multiple lines.
16 relief It specifies the type of the border. Its default value is
FLAT.
17 selectbackground The background color of the selected text.
18 selectborderwidth The width of the border to display around the selected
task.
19 selectforeground The font color of the selected task.
20 show It is used to show the entry text of some other type instead
of the string. For example, the password is typed using
stars (*).
21 textvariable It is set to the instance of the StringVar to retrieve the text
from the entry.
22 width The width of the displayed text or image.
23 xscrollcommand The entry widget can be linked to the horizontal scrollbar
if we want the user to enter more text then the actual width
of the widget.
Example:
from tkinter import *
top = Tk()
top.geometry("400x250")
name = Label(top, text = "Name").place(x = 30,y = 50)
email = Label(top, text = "Email").place(x = 30, y = 90)
password = Label(top, text = "Password").place(x = 30, y = 130)
submitbtn = Button(top, text = "Submit",activebackground = "pink", activeforeground = "
blue").place(x = 30, y = 170)
Output:
Hello Java Program for Beginners
Page 16 of 16
Subject: ADVANCE PYTHON Chapter - 1 B.C.A. Sem - 6
SN Method Description
1 delete(first, last = none) It is used to delete the specified characters inside
the widget.
2 get() It is used to get the text written inside the
widget.
3 icursor(index) It is used to change the insertion cursor position.
We can specify the index of the character before
which, the cursor to be placed.
4 index(index) It is used to place the cursor to the left of the
character written at the specified index.
5 insert(index,s) It is used to insert the specified string before the
character placed at the specified index.
6 select_adjust(index) It includes the selection of the character present
at the specified index.
7 select_clear() It clears the selection if some selection has been
done.
8 select_form(index) It sets the anchor index position to the character
specified by the index.
9 select_present() It returns true if some text in the Entry is
selected otherwise returns false.
10 select_range(start,end) It selects the characters to exist between the
specified range.
11 select_to(index) It selects all the characters from the beginning to
the specified index.
12 xview(index) It is used to link the entry widget to a horizontal
scrollbar.
13 xview_scroll(number,what) It is used to make the entry scrollable
horizontally.
root = tk.Tk()
root.geometry('400x200+100+200')
root.title('Calculator')
number1 = tk.StringVar()
number2 = tk.StringVar()
Page 17 of 17
Subject: ADVANCE PYTHON Chapter - 1 B.C.A. Sem - 6
root.mainloop()
Output:
5. Tkinter Frame:
• Python Tkinter Frame widget is used to organize the group of widgets.
• It acts like a container which can be used to hold the other widgets.
• The rectangular areas of the screen are used to organize the widgets to the python
application.
Syntax:
w = Frame(parent, options)
• A list of possible options is given below.
SN Option Description
1 bd It represents the border width.
2 bg The background color of the widget.
3 cursor The mouse pointer is changed to the cursor type set
to different values like an arrow, dot, etc.
4 height The height of the frame.
5 highlightbackground The color of the background color when it is under
focus.
6 highlightcolor The text color when the widget is under focus.
7 highlightthickness It specifies the thickness around the border when the
Page 18 of 18
Subject: ADVANCE PYTHON Chapter - 1 B.C.A. Sem - 6
Example:
from tkinter import *
top = Tk()
top.geometry("140x100")
frame = Frame(top)
frame.pack()
leftframe = Frame(top)
leftframe.pack(side = LEFT)
rightframe = Frame(top)
rightframe.pack(side = RIGHT)
top.mainloop()
Output:
Difference between JDK, JRE, and JVM
Page 19 of 19
Subject: ADVANCE PYTHON Chapter - 1 B.C.A. Sem - 6
6. Tkinter Label:
• The Label is used to specify the container box where we can place the text or images.
• This widget is used to provide the message to the user about other widgets used in the
python application.
• There are the various options which can be specified to configure the text or the part of
the text shown in the Label.
Syntax:
w = Label (master, options)
Page 20 of 20
Subject: ADVANCE PYTHON Chapter - 1 B.C.A. Sem - 6
Example:
from tkinter import *
top = Tk()
top.geometry("400x250")
#creating label
uname = Label(top, text = "Username").place(x = 30,y = 50)
#creating label
password = Label(top, text = "Password").place(x = 30, y = 90)
Output:
7. Tkinter Listbox:
• The Listbox widget is used to display the list items to the user.
• We can place only text items in the Listbox and all text items contain the same font and
color.
• The user can choose one or more items from the list depending upon the configuration.
Syntax:
w = Listbox(parent, options)
Page 21 of 21
Subject: ADVANCE PYTHON Chapter - 1 B.C.A. Sem - 6
SN Option Description
1 bg The background color of the widget.
2 bd It represents the size of the border. Default value is 2
pixel.
3 cursor The mouse pointer will look like the cursor type like
dot, arrow, etc.
4 font The font type of the Listbox items.
5 fg The color of the text.
6 height It represents the count of the lines shown in the
Listbox. The default value is 10.
7 highlightcolor The color of the Listbox items when the widget is
under focus.
8 highlightthickness The thickness of the highlight.
9 relief The type of the border. The default is SUNKEN.
10 selectbackground The background color that is used to display the
selected text.
11 selectmode It is used to determine the number of items that can be
selected from the list. It can set to BROWSE,
SINGLE, MULTIPLE, EXTENDED.
12 width It represents the width of the widget in characters.
13 xscrollcommand It is used to let the user scroll the Listbox horizontally.
14 yscrollcommand It is used to let the user scroll the Listbox vertically.
Methods:
• There are the following methods associated with the Listbox.
SN Method Description
1 activate(index) It is used to select the lines at the specified index.
2 curselection() It returns a tuple containing the line numbers of the selected
element or elements, counting from 0. If nothing is selected,
returns an empty tuple.
3 delete(first, last = None) It is used to delete the lines which exist in the given range.
4 get(first, last = None) It is used to get the list items that exist in the given range.
5 index(i) It is used to place the line with the specified index at the top
of the widget.
6 insert(index, *elements) It is used to insert the new lines with the specified number
of elements before the specified index.
7 nearest(y) It returns the index of the nearest line to the y coordinate of
the Listbox widget.
8 see(index) It is used to adjust the position of the listbox to make the
lines specified by the index visible.
9 size() It returns the number of lines that are present in the Listbox
widget.
10 xview() This is used to make the widget horizontally scrollable.
11 xview_moveto(fraction) It is used to make the listbox horizontally scrollable by the
fraction of width of the longest line present in the listbox.
Page 22 of 22
Subject: ADVANCE PYTHON Chapter - 1 B.C.A. Sem - 6
Example 1:
from tkinter import *
top = Tk()
top.geometry("200x250")
lbl = Label(top,text = "A list of favourite countries...")
listbox = Listbox(top)
listbox.insert(1,"India")
listbox.insert(2, "USA")
listbox.insert(3, "Japan")
listbox.insert(4, "Austrelia")
lbl.pack()
listbox.pack()
top.mainloop()
Output:
Page 23 of 23
Subject: ADVANCE PYTHON Chapter - 1 B.C.A. Sem - 6
listbox = Listbox(top)
listbox.insert(1,"India")
listbox.insert(2, "USA")
listbox.insert(3, "Japan")
listbox.insert(4, "Austrelia")
#this button will delete the selected item from the list
btn = Button(top, text = "delete", command = lambda listbox=listbox: listbox.delete(ANC
HOR))
lbl.pack()
listbox.pack()
btn.pack()
top.mainloop()
Output:
Page 24 of 24
Subject: ADVANCE PYTHON Chapter - 1 B.C.A. Sem - 6
8. Tkinter Menubutton:
• The Menubutton widget can be defined as the drop-down menu that is shown to the
user all the time.
• It is used to provide the user a option to select the appropriate choice exist within the
application.
• The Menubutton is used to implement various types of menus in the python application.
• A Menu is associated with the Menubutton that can display the choices of the
Menubutton when clicked by the user.
Syntax:
w = Menubutton(Top, options)
SN Option Description
1 activebackground The background color of the widget when the widget is under
focus.
2 activeforeground The font color of the widget text when the widget is under
focus.
3 anchor It specifies the exact position of the widget content when the
widget is assigned more space than needed.
4 bg It specifies the background color of the widget.
5 bitmap It is set to the graphical content which is to be displayed to
the widget.
6 bd It represents the size of the border. The default value is 2
pixels.
7 cursor The mouse pointer will be changed to the cursor type
specified when the widget is under the focus. The possible
value of the cursor type is arrow, or dot etc.
8 direction It direction can be specified so that menu can be displayed to
the specified direction of the button. Use LEFT, RIGHT, or
ABOVE to place the widget accordingly.
9 disabledforeground The text color of the widget when the widget is disabled.
10 fg The normal foreground color of the widget.
11 height The vertical dimension of the Menubutton. It is specified as
the number of lines.
12 highlightcolor The highlight color shown to the widget under focus.
13 image The image displayed on the widget.
14 justify This specified the exact position of the text under the widget
when the text is unable to fill the width of the widget. We can
use the LEFT for the left justification, RIGHT for the right
justification, CENTER for the centre justification.
15 menu It represents the menu specified with the Menubutton.
16 padx The horizontal padding of the widget.
17 pady The vertical padding of the widget.
18 relief This option specifies the type of the border. The default value
is RAISED.
Page 25 of 25
Subject: ADVANCE PYTHON Chapter - 1 B.C.A. Sem - 6
Example:
from tkinter import *
top = Tk()
top.geometry("200x250")
menubutton = Menubutton(top, text = "Language", relief = FLAT)
menubutton.grid()
menubutton.menu = Menu(menubutton)
menubutton["menu"]=menubutton.menu
menubutton.menu.add_checkbutton(label = "Hindi", variable=IntVar())
menubutton.menu.add_checkbutton(label = "English", variable = IntVar())
menubutton.pack()
top.mainloop()
Output:
Page 26 of 26
Subject: ADVANCE PYTHON Chapter - 1 B.C.A. Sem - 6
9. Tkinter Menu:
• The Menu widget is used to create various types of menus (top level, pull down, and
pop up) in the python application.
• The top-level menus are the one which is displayed just under the title bar of the parent
window.
• We need to create a new instance of the Menu widget and add various commands to it
by using the add() method.
Syntax:
w = Menu(top, options)
SN Option Description
1 activebackground The background color of the widget when the widget is
under the focus.
2 activeborderwidth The width of the border of the widget when it is under the
mouse. The default is 1 pixel.
3 activeforeground The font color of the widget when the widget has the focus.
4 bg The background color of the widget.
5 bd The border width of the widget.
6 cursor The mouse pointer is changed to the cursor type when it
hovers the widget. The cursor type can be set to arrow or
dot.
7 disabledforeground The font color of the widget when it is disabled.
8 font The font type of the text of the widget.
9 fg The foreground color of the widget.
10 postcommand The postcommand can be set to any of the function which is
called when the mourse hovers the menu.
11 relief The type of the border of the widget. The default type is
RAISED.
12 image It is used to display an image on the menu.
13 selectcolor The color used to display the checkbutton or radiobutton
when they are selected.
14 tearoff By default, the choices in the menu start taking place from
position 1. If we set the tearoff = 1, then it will start taking
place from 0th position.
15 title Set this option to the title of the window if you want to
change the title of the window.
Methods
• The Menu widget contains the following methods.
SN Option Description
1 add_command(options) It is used to add the Menu items to the menu.
2 add_radiobutton(options) This method adds the radiobutton to the menu.
3 add_checkbutton(options) This method is used to add the checkbuttons to the
menu.
Page 27 of 27
Subject: ADVANCE PYTHON Chapter - 1 B.C.A. Sem - 6
Example 1:
from tkinter import *
top = Tk()
def hello():
print("hello!")
Page 28 of 28
Subject: ADVANCE PYTHON Chapter - 1 B.C.A. Sem - 6
Output:
• Clicking the hello Menubutton will print the hello on the console while clicking the
Quit Menubutton will make an exit from the python application.
Example 2:
from tkinter import Toplevel, Button, Tk, Menu
top = Tk()
menubar = Menu(top)
file = Menu(menubar, tearoff=0)
file.add_command(label="New")
file.add_command(label="Open")
file.add_command(label="Save")
file.add_command(label="Save as...")
file.add_command(label="Close")
file.add_separator()
file.add_command(label="Exit", command=top.quit)
menubar.add_cascade(label="File", menu=file)
edit = Menu(menubar, tearoff=0)
edit.add_command(label="Undo")
edit.add_separator()
edit.add_command(label="Cut")
edit.add_command(label="Copy")
edit.add_command(label="Paste")
edit.add_command(label="Delete")
edit.add_command(label="Select All")
Page 29 of 29
Subject: ADVANCE PYTHON Chapter - 1 B.C.A. Sem - 6
menubar.add_cascade(label="Edit", menu=edit)
help = Menu(menubar, tearoff=0)
help.add_command(label="About")
menubar.add_cascade(label="Help", menu=help)
top.config(menu=menubar)
top.mainloop()
Output:
SN Option Description
1 anchor It is used to decide the exact position of the text within the space
provided to the widget if the widget contains more space than the
need of the text. The default is CENTER.
2 bg The background color of the widget.
3 bitmap It is used to display the graphics on the widget. It can be set to any
graphical or image object.
4 bd It represents the size of the border in the pixel. The default size is 2
Page 30 of 30
Subject: ADVANCE PYTHON Chapter - 1 B.C.A. Sem - 6
pixel.
5 cursor The mouse pointer is changed to the specified cursor type. The
cursor type can be an arrow, dot, etc.
6 font The font type of the widget text.
7 fg The font color of the widget text.
8 height The vertical dimension of the message.
9 image We can set this option to a static image to show that onto the widget.
10 justify This option is used to specify the alignment of multiple line of code
with respect to each other. The possible values can be LEFT (left
alignment), CENTER (default), and RIGHT (right alignment).
11 padx The horizontal padding of the widget.
12 pady The vertical padding of the widget.
13 relief It represents the type of the border. The default type is FLAT.
14 text We can set this option to the string so that the widget can represent
the specified text.
15 textvariable This is used to control the text represented by the widget. The
textvariable can be set to the text that is shown in the widget.
16 underline The default value of this option is -1 that represents no underline.
We can set this option to an existing number to specify that nth letter
of the string will be underlined.
17 width It specifies the horizontal dimension of the widget in the number of
characters (not pixel).
18 wraplength We can wrap the text to the number of lines by setting this option to
the desired number so that each line contains only that number of
characters.
Example:
from tkinter import *
top = Tk()
top.geometry("100x100")
var = StringVar()
msg = Message( top, text = "Welcome to Javatpoint")
msg.pack()
top.mainloop()
Output:
Page 31 of 31
Subject: ADVANCE PYTHON Chapter - 1 B.C.A. Sem - 6
Syntax:
w = Radiobutton(top, options)
SN Option Description
1 activebackground The background color of the widget when it has the focus.
2 activeforeground The font color of the widget text when it has the focus.
3 anchor It represents the exact position of the text within the widget
if the widget contains more space than the requirement of
the text. The default value is CENTER.
4 bg The background color of the widget.
5 bitmap It is used to display the graphics on the widget. It can be set
to any graphical or image object.
6 borderwidth It represents the size of the border.
7 command This option is set to the procedure which must be called
every-time when the state of the radiobutton is changed.
8 cursor The mouse pointer is changed to the specified cursor type.
It can be set to the arrow, dot, etc.
9 font It represents the font type of the widget text.
10 fg The normal foreground color of the widget text.
11 height The vertical dimension of the widget. It is specified as the
number of lines (not pixel).
12 highlightcolor It represents the color of the focus highlight when the
widget has the focus.
13 highlightbackground The color of the focus highlight when the widget is not
having the focus.
14 image It can be set to an image object if we want to display an
image on the radiobutton instead the text.
15 justify It represents the justification of the multi-line text. It can be
set to CENTER(default), LEFT, or RIGHT.
16 padx The horizontal padding of the widget.
17 pady The vertical padding of the widget.
18 relief The type of the border. The default value is FLAT.
19 selectcolor The color of the radio button when it is selected.
20 selectimage The image to be displayed on the radiobutton when it is
selected.
21 state It represents the state of the radio button. The default state
of the Radiobutton is NORMAL. However, we can set this
Page 32 of 32
Subject: ADVANCE PYTHON Chapter - 1 B.C.A. Sem - 6
Methods:
• The radiobutton widget provides the following methods.
SN Method Description
1 deselect() It is used to turn of the radiobutton.
2 flash() It is used to flash the radiobutton between its active and normal
colors few times.
3 invoke() It is used to call any procedure associated when the state of a
Radiobutton is changed.
4 select() It is used to select the radiobutton.
Example:
from tkinter import *
def selection():
selection = "You selected the option " + str(radio.get())
label.config(text = selection)
top = Tk()
top.geometry("300x150")
radio = IntVar()
lbl = Label(text = "Favourite programming language:")
lbl.pack()
R1 = Radiobutton(top, text="C", variable=radio, value=1,
command=selection)
R1.pack( anchor = W )
Page 33 of 33
Subject: ADVANCE PYTHON Chapter - 1 B.C.A. Sem - 6
label = Label(top)
label.pack()
top.mainloop()
Output:
SN Option Description
1 activebackground The background color of the widget when it has the focus.
2 bg The background color of the widget.
3 bd The border size of the widget. The default is 2 pixel.
4 command It is set to the procedure which is called each time when
we move the slider. If the slider is moved rapidly, the
callback is done when it settles.
5 cursor The mouse pointer is changed to the cursor type assigned
to this option. It can be an arrow, dot, etc.
6 digits If the control variable used to control the scale data is of
string type, this option is used to specify the number of
digits when the numeric scale is converted to a string.
7 font The font type of the widget text.
Page 34 of 34
Subject: ADVANCE PYTHON Chapter - 1 B.C.A. Sem - 6
Methods:
SN Method Description
1 get() It is used to get the current value of the scale.
2 set(value) It is used to set the value of the scale.
Page 35 of 35
Subject: ADVANCE PYTHON Chapter - 1 B.C.A. Sem - 6
Example:
from tkinter import *
def select():
sel = "Value = " + str(v.get())
label.config(text = sel)
top = Tk()
top.geometry("200x100")
v = DoubleVar()
scale = Scale( top, variable = v, from_ = 1, to = 50, orient = HORIZONTAL)
scale.pack(anchor=CENTER)
label = Label(top)
label.pack()
top.mainloop()
Output:
SN Option Description
1 activebackground The background color of the widget when it has the
focus.
2 bg The background color of the widget.
3 bd The border width of the widget.
4 command It can be set to the procedure associated with the list
which can be called each time when the scrollbar is
moved.
Page 36 of 36
Subject: ADVANCE PYTHON Chapter - 1 B.C.A. Sem - 6
Methods:
• The widget provides the following methods.
18.9M
90HTML Tutorial
SN Method Description
1 get() It returns the two numbers a and b which represents the current
position of the scrollbar.
2 set(first, It is used to connect the scrollbar to the other widget w. The
last) yscrollcommand or xscrollcommand of the other widget to this
method.
Example:
from tkinter import *
top = Tk()
sb = Scrollbar(top)
sb.pack(side = RIGHT, fill = Y)
mainloop()
Page 37 of 37
Subject: ADVANCE PYTHON Chapter - 1 B.C.A. Sem - 6
Output:
• A list of possible options that can be used with the Text widget is given below.
SN Option Description
1 bg The background color of the widget.
2 bd It represents the border width of the widget.
3 cursor The mouse pointer is changed to the specified cursor
type, i.e. arrow, dot, etc.
4 exportselection The selected text is exported to the selection in the
window manager. We can set this to 0 if we don't want
the text to be exported.
5 font The font type of the text.
6 fg The text color of the widget.
7 height The vertical dimension of the widget in lines.
8 highlightbackground The highlightcolor when the widget doesn't has the
focus.
9 highlightthickness The thickness of the focus highlight. The default value is
1.
10 highlighcolor The color of the focus highlight when the widget has the
focus.
Page 38 of 38
Subject: ADVANCE PYTHON Chapter - 1 B.C.A. Sem - 6
Methods:
• We can use the following methods with the Text widget.
SN Method Description
1 delete(startindex, This method is used to delete the characters of the
endindex) specified range.
2 get(startindex, It returns the characters present in the specified range.
endindex)
3 index(index) It is used to get the absolute index of the specified index.
4 insert(index, string) It is used to insert the specified string at the given index.
5 see(index) It returns a boolean value true or false depending upon
whether the text at the specified index is visible or not.
Page 39 of 39
Subject: ADVANCE PYTHON Chapter - 1 B.C.A. Sem - 6
SN Method Description
1 index(mark) It is used to get the index of the specified mark.
2 mark_gravity(mark, It is used to get the gravity of the given mark.
gravity)
3 mark_names() It is used to get all the marks present in the Text
widget.
4 mark_set(mark, index) It is used to inform a new position of the given mark.
5 mark_unset(mark) It is used to remove the given mark from the text.
SN Method Description
1 tag_add(tagname, startindex, This method is used to tag the string present in
endindex) the specified range.
2 tag_config This method is used to configure the tag
properties.
3 tag_delete(tagname) This method is used to delete a given tag.
4 tag_remove(tagname, This method is used to remove a tag from the
startindex, endindex) specified range.
Example
from tkinter import *
top = Tk()
text = Text(top)
text.insert(INSERT, "Name.....")
text.insert(END, "Salary.....")
text.pack()
top.mainloop()
Page 40 of 40
Subject: ADVANCE PYTHON Chapter - 1 B.C.A. Sem - 6
Output:
Syntax:
w = Toplevel(options)
SN Options Description
1 bg It represents the background color of the window.
2 bd It represents the border size of the window.
3 cursor The mouse pointer is changed to the cursor type set to the arrow, dot,
etc. when the mouse is in the window.
4 class_ The text selected in the text widget is exported to be selected to the
window manager. We can set this to 0 to make this behavior false.
5 font The font type of the text inserted into the widget.
6 fg The foreground color of the widget.
7 height It represents the height of the window.
8 relief It represents the type of the window.
9 width It represents the width of the window,
Page 41 of 41
Subject: ADVANCE PYTHON Chapter - 1 B.C.A. Sem - 6
Methods:
• The methods associated with the Toplevel widget is given in the following list.
SN Method Description
1 deiconify() This method is used to display the window.
2 frame() It is used to show a system dependent window identifier.
3 group(window) It is used to add this window to the specified window
group.
4 iconify() It is used to convert the toplevel window into an icon.
5 protocol(name, It is used to mention a function which will be called for
function) the specific protocol.
6 state() It is used to get the current state of the window. Possible
values are normal, iconic, withdrawn, and icon.
7 transient([master]) It is used to convert this window to a transient window
(temporary).
8 withdraw() It is used to delete the window but doesn't destroy it.
9 maxsize(width, It is used to declare the maximum size for the window.
height)
10 minsize(width, It is used to declare the minimum size for the window.
height)
11 positionfrom(who) It is used to define the position controller.
12 resizable(width, It is used to control whether the window can be resizable
height) or not.
13 sizefrom(who) It is used to define the size controller.
14 title(string) It is used to define the title for the window.
Example:
from tkinter import *
root = Tk()
root.geometry("200x200")
def open():
top = Toplevel(root)
top.mainloop()
btn = Button(root, text = "open", command = open)
btn.place(x=75,y=50)
root.mainloop()
Output:
Page 42 of 42
Subject: ADVANCE PYTHON Chapter - 1 B.C.A. Sem - 6
SN Option Description
1 activebackground The background color of the widget when it has the focus.
2 bg The background color of the widget.
3 bd The border width of the widget.
4 command The associated callback with the widget which is called
each time the state of the widget is called.
5 cursor The mouse pointer is changed to the cursor type assigned
to this option.
6 disabledbackground The background color of the widget when it is disabled.
7 disabledforeground The foreground color of the widget when it is disabled.
8 fg The normal foreground color of the widget.
9 font The font type of the widget content.
10 format This option is used for the format string. It has no default
value.
11 from_ It is used to show the starting range of the widget.
12 justify It is used to specify the justification of the multi-line
widget content. The default is LEFT.
13 relief It is used to specify the type of the border. The default is
SUNKEN.
14 repeatdelay This option is used to control the button auto repeat. The
value is given in milliseconds.
15 repeatinterval It is similar to repeatdelay. The value is given in
milliseconds.
16 state It represents the state of the widget. The default is
NORMAL. The possible values are NORMAL,
DISABLED, or "readonly".
17 textvariable It is like a control variable which is used to control the
behaviour of the widget text.
18 to It specify the maximum limit of the widget value. The
other is specified by the from_ option.
19 validate This option controls how the widget value is validated.
20 validatecommand It is associated to the function callback which is used for
the validation of the widget content.
21 values It represents the tuple containing the values for this widget.
22 vcmd It is same as validation command.
23 width It represents the width of the widget.
24 wrap This option wraps up the up and down button the Spinbox.
Page 43 of 43
Subject: ADVANCE PYTHON Chapter - 1 B.C.A. Sem - 6
Methods:
• There are the following methods associated with the widget.
SN Option Description
1 delete(startindex, This method is used to delete the characters present at the
endindex) specified range.
2 get(startindex, It is used to get the characters present in the specified
endindex) range.
3 identify(x, y) It is used to identify the widget's element within the
specified range.
4 index(index) It is used to get the absolute value of the given index.
5 insert(index, string) This method is used to insert the string at the specified
index.
6 invoke(element) It is used to invoke the callback associated with the
widget.
Example:
from tkinter import *
top = Tk()
top.geometry("200x200")
spin = Spinbox(top, from_= 0, to = 25)
spin.pack()
top.mainloop()
Output:
Page 44 of 44
Subject: ADVANCE PYTHON Chapter - 1 B.C.A. Sem - 6
Syntax:
w= PanedWindow(master, options)
SN Option Description
1 bg It represents the background color of the widget when it doesn't
have the focus.
2 bd It represents the 3D border size of the widget. The default option
specifies that the trough contains no border whereas the
arrowheads and slider contain the 2-pixel border size.
3 borderwidth It represents the border width of the widget. The default is 2
pixel.
4 cursor The mouse pointer is changed to the specified cursor type when it
is over the window.
5 handlepad This option represents the distance between the handle and the
end of the sash. For the horizontal orientation, it is the distance
between the top of the sash and the handle. The default is 8
pixels.
6 handlesize It represents the size of the handle. The default size is 8 pixels.
However, the handle will always be a square.
7 height It represents the height of the widget. If we do not specify the
height, it will be calculated by the height of the child window.
8 orient The orient will be set to HORIZONTAL if we want to place the
child windows side by side. It can be set to VERTICAL if we
want to place the child windows from top to bottom.
9 relief It represents the type of the border. The default is FLAT.
10 sashpad It represents the padding to be done around each sash. The
default is 0.
11 sashrelief It represents the type of the border around each of the sash. The
default is FLAT.
12 sashwidth It represents the width of the sash. The default is 2 pixels.
13 showhandle It is set to True to display the handles. The default value is false.
14 Width It represents the width of the widget. If we don't specify the width
of the widget, it will be calculated by the size of the child
widgets.
Exception Handling in Java
Methods:
• There are the following methods that are associated with the PanedWindow.
SN Method Description
1 add(child, options) It is used to add a window to the parent window.
2 get(startindex, endindex) This method is used to get the text present at the specified
range.
Page 45 of 45
Subject: ADVANCE PYTHON Chapter - 1 B.C.A. Sem - 6
Example:
from tkinter import *
def add():
a = int(e1.get())
b = int(e2.get())
leftdata = str(a+b)
left.insert(1,leftdata)
w1 = PanedWindow()
w1.pack(fill = BOTH, expand = 1)
left = Entry(w1, bd = 5)
w1.add(left)
e1 = Entry(w2)
e2 = Entry(w2)
w2.add(e1)
w2.add(e2)
mainloop()
Output:
Page 46 of 46
Subject: ADVANCE PYTHON Chapter - 1 B.C.A. Sem - 6
Syntax:
w = LabelFrame(top, options)
SN Option Description
1 bg The background color of the widget.
2 bd It represents the size of the border shown around the indicator.
The default is 2 pixels.
3 Class The default value of the class is LabelFrame.
4 colormap This option is used to specify which colomap is to be used for
this widget. By colormap, we mean the 256 colors that are used
to form the graphics. With this option, we can reuse the
colormap of another window on this widget.
5 container If this is set to true, the LabelFrame becomes the container
widget. The default value is false.
6 cursor It can be set to a cursor type, i.e. arrow, dot, etc. the mouse
pointer is changed to the cursor type when it is over the widget.
7 fg It represents the foreground color of the widget.
8 font It represents the font type of the widget text.
9 height It represents the height of the widget.
10 labelAnchor It represents the exact position of the text within the widget. The
default is NW(north-west)
11 labelwidget It represents the widget to be used for the label. The frame uses
the text for the label if no value specified.
12 highlightbackground The color of the focus highlight border when the widget doesn't
have the focus.
13 highlightcolor The color of the focus highlight when the widget has the focus.
14 highlightthickness The width of the focus highlight border.
15 padx The horizontal padding of the widget.
16 pady The vertical padding of the widget.
17 relief It represents the border style. The default value is GROOVE.
18 text It represents the string containing the label text.
19 width It represents the width of the frame.
Example:
from tkinter import *
top = Tk()
top.geometry("300x200")
Page 47 of 47
Subject: ADVANCE PYTHON Chapter - 1 B.C.A. Sem - 6
top.mainloop()
Output:
Parameters:
1. function_name: It represents an appropriate message box function.
2. title: It is a string which is shown as a title of a message box.
3. message: It is the string to be displayed as a message on the message box.
4. options: There are various options which can be used to configure the message dialog
box.
• The two options that can be used are default and parent.
1. default:
• The default option is used to mention the types of the default button, i.e. ABORT,
RETRY, or IGNORE in the message box.
2. parent:
• The parent option specifies the parent window on top of which, the message box is to
be displayed.
• There is one of the following functions used to show the appropriate message boxes.
Page 48 of 48
Subject: ADVANCE PYTHON Chapter - 1 B.C.A. Sem - 6
• All the functions are used with the same syntax but have the specific functionalities.
1. showinfo():
• The showinfo() messagebox is used where we need to show some relevant information
to the user.
Example:
from tkinter import *
top = Tk()
top.geometry("100x100")
messagebox.showinfo("information","Information")
top.mainloop()
Output:
2. showwarning():
• This method is used to display the warning to the user.
Example:
from tkinter import *
top = Tk()
top.geometry("100x100")
messagebox.showwarning("warning","Warning")
top.mainloop()
Page 49 of 49
Subject: ADVANCE PYTHON Chapter - 1 B.C.A. Sem - 6
Output:
3. showerror():
• This method is used to display the error message to the user.
Example:
from tkinter import *
from tkinter import messagebox
top = Tk()
top.geometry("100x100")
messagebox.showerror("error","Error")
top.mainloop()
Output:
4. askquestion():
• This method is used to ask some question to the user which can be answered in yes or
no.
Example:
from tkinter import *
from tkinter import messagebox
top = Tk()
top.geometry("100x100")
messagebox.askquestion("Confirm","Are you sure?")
top.mainloop()
Page 50 of 50
Subject: ADVANCE PYTHON Chapter - 1 B.C.A. Sem - 6
Output:
5. askokcancel()
• This method is used to confirm the user's action regarding some application activity.
Example:
from tkinter import *
from tkinter import messagebox
top = Tk()
top.geometry("100x100")
messagebox.askokcancel("Redirect","Redirecting you to www.javatpoint.com")
top.mainloop()
Output:
6. askyesno():
• This method is used to ask the user about some action to which, the user can answer in
yes or no.
Example:
from tkinter import *
from tkinter import messagebox
top = Tk()
top.geometry("100x100")
messagebox.askyesno("Application","Got It?")
Page 51 of 51
Subject: ADVANCE PYTHON Chapter - 1 B.C.A. Sem - 6
top.mainloop()
Output:
7. askretrycancel():
• This method is used to ask the user about doing a particular task again or not.
Example:
from tkinter import *
from tkinter import messagebox
top = Tk()
top.geometry("100x100")
messagebox.askretrycancel("Application","try again?")
top.mainloop()
Output:
Page 52 of 52