Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (1 vote)
6K views

Python Tkinter Tutorial

Python Tkinter provides the Tkinter module for creating graphical user interfaces (GUIs) in Python. Tkinter includes common widgets like buttons, labels, entries, etc. that can be added to a main window. The geometry of widgets can be organized using pack(), grid(), or place() methods which control widget positioning and layout. Common tasks like creating a window, adding widgets, and entering the main event loop are demonstrated through examples.

Uploaded by

Uddayan Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
6K views

Python Tkinter Tutorial

Python Tkinter provides the Tkinter module for creating graphical user interfaces (GUIs) in Python. Tkinter includes common widgets like buttons, labels, entries, etc. that can be added to a main window. The geometry of widgets can be organized using pack(), grid(), or place() methods which control widget positioning and layout. Common tasks like creating a window, adding widgets, and entering the main event loop are demonstrated through examples.

Uploaded by

Uddayan Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

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.

1. import the Tkinter module.

2. Create the main application window.

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.

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.
values.

5 Frame It can be defined as a container to which, another widget can be added

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.

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 provi
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 wind

14 Text It is different from Entry because it provides a multi-line text field to th


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.

16 PanedWindow It is like a container widget that contains horizontal or vertical panes.

17 LabelFrame A LabelFrame is a container widget that acts as the container

18 MessageBox This module is used to display the message-box in the desktop based a

Python Tkinter Geometry


The Tkinter geometry specifies the method by using which, the widgets are represented
on display. The python Tkinter provides the following geometry methods.

1. The pack() method

2. The grid() method

3. The place() method

Let's discuss each one of them in detail.

Python Tkinter pack() method


The pack() widget is used to organize widget in the block. The positions widgets added
to the python application using the pack() method can be controlled by using the
various options specified in the method call.
However, the controls are less and widgets are generally added in the less organized
manner.

The syntax to use the pack() is given below.

syntax

1. widget.pack(options)

A list of possible options that can be passed in pack() is given below.

o expand: If the expand is set to true, the widget expands to fill any space.

o Fill: By default, the fill is set to NONE. However, we can set it to X or Y to


determine whether the widget contains any extra 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:

Python Tkinter place() method


The place() geometry manager organizes the widgets to the specific x and y
coordinates.

Syntax

1. widget.place(options)

A list of possible options is given below.

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.

o height, width: It refers to the height and width in pixels.

o relheight, relwidth: It is represented as the float between 0.0 and 1.0


indicating the fraction of the parent's height and width.
o relx, rely: It is represented as the float between 0.0 and 1.0 that is the offset in
the horizontal and vertical direction.

o x, y: It refers to the horizontal and vertical offset in the pixels.

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:

Python 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.

The syntax to use the button widget is given below.

Syntax

1. W = Button(parent, options)

A list of possible options is given below.

SN Option Description
1 activebackground It represents the background of the button when
the mouse hover the button.

2 activeforeground It represents the font color of the button when the


mouse hover the button.

3 Bd It represents the border width in pixels.

4 Bg It represents the background color of the button.

5 Command It is set to the function call which is scheduled


when the function is called.

6 Fg Foreground color of the button.

7 Font The font of the button text.

8 Height The height of the button. The height is


represented in the number of text lines for the
textual lines or the number of pixels for the
images.

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

1. #python application to create a simple button


2.
3. from tkinter import *
4.
5.
6. top = Tk()
7.
8. top.geometry("200x100")
9.
10. b = Button(top,text = "Simple")
11.
12. b.pack()
13.
14. top.mainaloop()

Output:
Example

1. from tkinter import *


2.
3. top = Tk()
4.
5. top.geometry("200x100")
6.
7. def fun():
8. messagebox.showinfo("Hello", "Red Button clicked")
9.
10.
11. b1 = Button(top,text = "Red",command = fun,activeforeground = "red",activebackgrou
nd = "pink",pady=10)
12.
13. b2 = Button(top, text = "Blue",activeforeground = "blue",activebackground = "pink",pa
dy=10)
14.
15. b3 = Button(top, text = "Green",activeforeground = "green",activebackground = "pink"
,pady = 10)
16.
17. b4 = Button(top, text = "Yellow",activeforeground = "yellow",activebackground = "pink
",pady = 10)
18.
19. b1.pack(side = LEFT)
20.
21. b2.pack(side = RIGHT)
22.
23. b3.pack(side = TOP)
24.
25. b4.pack(side = BOTTOM)
26.
27. top.mainloop()

Output:

Python 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. The syntax to use the
canvas is given below.

Syntax

1. w = canvas(parent, options)

A list of possible options is given below.


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 SUNKE

8 scrollregion It represents the coordinates specified as the tuple containing the a

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 mult

11 xscrollcommand If the canvas is scrollable, this attribute should be the .set() metho

12 yscrollincrement Works like xscrollincrement, but governs vertical movement.

13 yscrollcommand If the canvas is scrollable, this attribute should be the .set() metho

Example

1. from tkinter import *


2.
3. top = Tk()
4.
5. top.geometry("200x200")
6.
7. #creating a simple canvas
8. c = Canvas(top,bg = "pink",height = "200")
9.
10.
11. c.pack()
12.
13. top.mainloop()

Output:

Example: Creating an arc

1. from tkinter import *


2.
3. top = Tk()
4.
5. top.geometry("200x200")
6.
7. #creating a simple canvas
8. c = Canvas(top,bg = "pink",height = "200",width = 200)
9.
10. arc = c.create_arc((5,10,150,200),start = 0,extent = 150, fill= "white")
11.
12. c.pack()
13.
14. top.mainloop()
Output:

You might also like