Python Tkinter Tutorial
Python Tkinter Tutorial
Tkinter tutorial provides basic and advanced concepts of Python Tkinter. Our Tkinter
tutorial is designed for beginners and professionals.
Python provides the standard library Tkinter for creating the graphical user interface for
desktop based applications.
Developing desktop based applications with python Tkinter is not a complex task. An
empty Tkinter top-level window can be created by using the following steps.
3. Add the widgets like labels, buttons, frames, etc. to the window.
4. Call the main event loop so that the actions can take place on the user's
computer screen.
Example
1. # !/usr/bin/python3
2. from tkinter import *
3. #creating the application main window.
4. top = Tk()
5. #Entering the event main loop
6. top.mainloop()
Output:
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 applica
2 Canvas The canvas widget is used to draw the canvas on the window.
4 Entry The entry widget is used to display the single-line text field to the user.
values.
6 Label A label is a text used to display some message or information about the
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.
11 Radiobutton The Radiobutton is different from a checkbutton. Here, the user is provi
can select only one option among them.
13 Scrollbar It provides the scrollbar to the user so that the user can scroll the wind
18 MessageBox This module is used to display the message-box in the desktop based a
syntax
1. widget.pack(options)
o expand: If the expand is set to true, the widget expands to fill any space.
o size: it represents the side of the parent to which the widget is to be placed on
the window.
Example
1. # !/usr/bin/python3
2. from tkinter import *
3. parent = Tk()
4. redbutton = Button(parent, text = "Red", fg = "red")
5. redbutton.pack( side = LEFT)
6. greenbutton = Button(parent, text = "Black", fg = "black")
7. greenbutton.pack( side = RIGHT )
8. bluebutton = Button(parent, text = "Blue", fg = "blue")
9. bluebutton.pack( side = TOP )
10. blackbutton = Button(parent, text = "Green", fg = "red")
11. blackbutton.pack( side = BOTTOM)
12. parent.mainloop()
Output:
Python Tkinter grid() method
The grid() geometry manager organizes the widgets in the tabular form. We can specify
the rows and columns as the options in the method call. We can also specify the column
span (width) or rowspan(height) of a widget.
This is a more organized way to place the widgets to the python application. The syntax
to use the grid() is given below.
Syntax
1. widget.grid(options)
A list of possible options that can be passed inside the grid() method is given below.
o Column
The column number in which the widget is to be placed. The leftmost column is
represented by 0.
o Columnspan
The width of the widget. It represents the number of columns up to which, the
column is expanded.
o ipadx, ipady
It represents the number of pixels to pad the widget inside the widget's border.
o padx, pady
It represents the number of pixels to pad the widget outside the widget's border.
o row
The row number in which the widget is to be placed. The topmost row is
represented by 0.
o rowspan
The height of the widget, i.e. the number of the row up to which the widget is
expanded.
o 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
1. # !/usr/bin/python3
2. from tkinter import *
3. parent = Tk()
4. name = Label(parent,text = "Name").grid(row = 0, column = 0)
5. e1 = Entry(parent).grid(row = 0, column = 1)
6. password = Label(parent,text = "Password").grid(row = 1, column = 0)
7. e2 = Entry(parent).grid(row = 1, column = 1)
8. submit = Button(parent, text = "Submit").grid(row = 4, column = 0)
9. parent.mainloop()
Output:
Syntax
1. widget.place(options)
o Anchor: It represents the exact position of the widget within the container. The
default value (direction) is NW (the upper left corner)
o bordermode: The default value of the border type is INSIDE that refers to
ignore the parent's inside the border. The other option is OUTSIDE.
Example
1. # !/usr/bin/python3
2. from tkinter import *
3. top = Tk()
4. top.geometry("400x250")
5. name = Label(top, text = "Name").place(x = 30,y = 50)
6. email = Label(top, text = "Email").place(x = 30, y = 90)
7. password = Label(top, text = "Password").place(x = 30, y = 130)
8. e1 = Entry(top).place(x = 80, y = 50)
9. e2 = Entry(top).place(x = 80, y = 90)
10. e3 = Entry(top).place(x = 95, y = 130)
11. top.mainloop()
Output:
We can also associate a method or function with a button which is called when the
button is pressed.
Syntax
1. W = Button(parent, options)
SN Option Description
1 activebackground It represents the background of the button when
the mouse hover the button.
10 Highlightcolor The color of the highlight when the button has the
focus.
Example
Output:
Example
Output:
Syntax
1. w = canvas(parent, options)
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.
7 relief It represents the type of the border. The possible values are SUNKE
10 xscrollincrement If it is set to a positive value. The canvas is placed only to the mult
11 xscrollcommand If the canvas is scrollable, this attribute should be the .set() metho
13 yscrollcommand If the canvas is scrollable, this attribute should be the .set() metho
Example
Output: