Python Tkinter Gui
Python Tkinter Gui
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:
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
2 activeforeground It represents the font color of the button when the mouse
hover the button.
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.
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:
Syntax
1. w = canvas(parent, options)
A list of possible options is given below.
SN Option Description
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 SUNK
8 scrollregion It represents the coordinates specified as the tuple containing the
11 xscrollcommand If the canvas is scrollable, this attribute should be the .set() meth
13 yscrollcommand If the canvas is scrollable, this attribute should be the .set() meth
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:
Output:
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
1. w = checkbutton(master, options)
SN Option Description
7 cursor The mouse pointer will be changed to the cursor name when it is
12 highlightcolor The color of the focus highlight when the checkbutton is under fo
14 justify This specifies the justification of the text if the text contains mult
25 variable It represents the associated variable that tracks the state of the c
27 wraplength If this option is set to an integer number, the text will be broken
Methods
The methods that can be called with the Checkbuttons are described in the following
table.
SN Method Description
2 flash() The checkbutton is flashed between the active and normal colo
3 invoke() This will invoke the method associated with the checkbutton.
Example
1. from tkinter import *
2.
3. top = Tk()
4.
5. top.geometry("200x200")
6.
7. checkvar1 = IntVar()
8.
9. checkvar2 = IntVar()
10.
11.checkvar3 = IntVar()
12.
13.chkbtn1 = Checkbutton(top, text = "C", variable = checkvar1, onvalue = 1, offvalue =
0, height = 2, width = 10)
14.
15.chkbtn2 = Checkbutton(top, text = "C++", variable = checkvar2, onvalue = 1, offvalue
= 0, height = 2, width = 10)
16.
17.chkbtn3 = Checkbutton(top, text = "Java", variable = checkvar3, onvalue = 1, offvalue
= 0, height = 2, width = 10)
18.
19.chkbtn1.pack()
20.
21.chkbtn2.pack()
22.
23.chkbtn3.pack()
24.
25.top.mainloop()
Output:
Syntax
1. w = Entry (parent, options)
3 cursor The mouse pointer will be changed to the cursor type set to the
4 exportselection The text written inside the entry box will be automatically copie
set the exportselection to 0 to not copy this.
8 highlightcolor It represents the color to use for the traversal highlight rectang
when it has the input focus.
14 insertwidth It represents the value indicating the total width of the insertio
the forms acceptable to Tk_GetPixels.
15 justify It specifies how the text is organized if the text contains multip
16 relief It specifies the type of the border. Its default value is FLAT.
18 selectborderwidth The width of the border to display around the selected task.
20 show It is used to show the entry text of some other type instead of
is typed using stars (*).
21 textvariable It is set to the instance of the StringVar to retrieve the text fro
Example
1. # !/usr/bin/python3
2.
3. from tkinter import *
4.
5. top = Tk()
6.
7. top.geometry("400x250")
8.
9. name = Label(top, text = "Name").place(x = 30,y = 50)
10.
11.email = Label(top, text = "Email").place(x = 30, y = 90)
12.
13.password = Label(top, text = "Password").place(x = 30, y = 130)
14.
15.sbmitbtn = Button(top, text = "Submit",activebackground = "pink", activeforeground =
"blue").place(x = 30, y = 170)
16.
17.e1 = Entry(top).place(x = 80, y = 50)
18.
19.
20.e2 = Entry(top).place(x = 80, y = 90)
21.
22.
23.e3 = Entry(top).place(x = 95, y = 130)
24.
25.top.mainloop()
Output:
SN Method Description
1 delete(first, last = none) It is used to delete the specified characters inside the w
However, the controls are less and widgets are generally added in the less organized
manner.
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:
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)
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 relx, rely: It is represented as the float between 0.0 and 1.0 that is the offset in
the horizontal and vertical direction.
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: