Introduction to Python GUI using tkinter














































Introduction to Python GUI using tkinter




DESCRIPTION:



Tkinter is standard Python GUI library which gives object-oriented interface to the Tk GUI toolkit.
GUI applications can be created at a faster pace and with ease using tkinter.


Importing tkinter:

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 is 'tkinter'.


import tkinter as tk


Basic functions in tkinter:

Label:

This widget implements a display box where text or images can be displayed.


import tkinter as tk

def build():
#main window that contains the widgets.
root = tk.Tk()
#sets the title of window.
root.title(
"Label Demo")
#sets the dimensions of window(width x height).
root.geometry(
"250x100")

#create a label widget in main window with provided text.
label_1 = tk.Label(root,text =
"Hello World!!",fg = "blue", bg = "white")
label_1.pack()

#mainloop() is an infinite loop used to run the application,
#wait for an event to occur and process the event till the window is not closed.
root.mainloop()

if __name__ == "__main__":
build()


OUTPUT:


                --------------------------------------------------------------------------------------------------------------------------------------------------------------


Button:

The Button widget is used to add buttons to the current frame. A text or image can be used to display the purpose of button.

A function(passed as a parameter to 'command') is invoked when the button is clicked.


import tkinter as tk

def build():
#main window that contains the widgets.
root = tk.Tk()
#sets the title of window.
root.title(
"Button Demo")
#sets the dimensions of window(width x height).
root.geometry(
"250x100")

#create a button widget in main window.
button_1 = tk.Button(root,text =
"Click",fg = "blue", bg = "white",command = lambda:print("Button clicked"))
button_1.pack()

#mainloop() is an infinite loop used to run the application,
#wait for an event to occur and process the event till the window is not closed.
root.mainloop()

if __name__ == "__main__":
build()


OUTPUT:



                    ---------------------------------------------------------------------------------------------------------------------------------------------------------


Pack:

This geometry manager organizes widgets in blocks before placing them in the parent widget.


import tkinter as tk

def build():
#main window that contains the widgets.
root = tk.Tk()
#sets the title of window.
root.title(
"Pack Demo")
#sets the dimensions of window(width x height).
root.geometry(
"250x100")

#create a button and a label main window.
button_1 = tk.Button(root,text =
"First button",fg = "blue", bg = "white",command = lambda:print("Button clicked"))
label_1 = tk.Label(root,text =
"First label",fg = "red", bg = "white")

#packs the button widget onto the main window.
button_1.pack()
#packs the label widget onto the main window and below(default) the previous widget.
label_1.pack()

#mainloop() is an infinite loop used to run the application,
#wait for an event to occur and process the event till the window is not closed.
root.mainloop()

if __name__ == "__main__":
build()


OUTPUT:


                     -----------------------------------------------------------------------------------------------------------------------------------------------------------------


Frame:

The Frame widget is used for grouping, organizing and arranging the position of other widgets. It uses rectangular
areas on the screen to organize the layout and to provide padding of these widgets.


import tkinter as tk

def build():
#main window that contains the widgets.
root = tk.Tk()
#sets the title of window.
root.title(
"Frame Demo")
#sets the dimensions of window(width x height).
root.geometry(
"250x100")

#create 2 frames.
frame_1 = tk.Frame(root)
frame_2 = tk.Frame(root)
#align the frame to left.
frame_1.pack(side = tk.LEFT)
frame_2.pack(side = tk.LEFT)

#create a button and a label, one in each frame.
button_1 = tk.Button(frame_1,text =
"First button",fg = "blue", bg = "white",command = lambda:print("Button clicked"))
label_1 = tk.Label(frame_2,text =
"First label",fg = "red", bg = "white")

#packs the button widget onto the main window.
button_1.pack()
label_1.pack()

#mainloop() is an infinite loop used to run the application,
#wait for an event to occur and process the event till the window is not closed.
root.mainloop()

if __name__ == "__main__":
build()


OUTPUT:


                       ---------------------------------------------------------------------------------------------------------------------------------------------------------------


Grid:

This geometry manager organizes widgets in a table-like structure in the parent widget.


import tkinter as tk

def build():
#main window that contains the widgets.
root = tk.Tk()
#sets the title of window.
root.title(
"Grid Demo")
#sets the dimensions of window(width x height).
root.geometry(
"250x100")

#create 2 buttons and 2 labels.
button_1 = tk.Button(root,text =
"First button",fg = "red", bg = "white",command = lambda:print("Button clicked"))
button_2 = tk.Button(root,text =
"Second button",fg = "blue", bg = "white",command = lambda:print("Button clicked"))
label_1 = tk.Label(root,text =
"First label",fg = "red", bg = "white")
label_2 = tk.Label(root,text =
"Second label",fg = "green", bg = "white")

#arrange the widgets in the grid with specified row and column numbers.
button_1.grid(row =
0,column = 0)
label_1.grid(row =
0,column = 1)
button_2.grid(row =
1,column = 0)
label_2.grid(row =
1,column = 1)

#mainloop() is an infinite loop used to run the application,
#wait for an event to occur and process the event till the window is not closed.
root.mainloop()

if __name__ == "__main__":
build()


OUTPUT:


                ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------


Listbox and Insert:

The Listbox widget is used to display a list of items from which a user can select number of items.

 

Insert is used to insert one or more new lines into the listbox before the line specified by index(first argument).

Use tk.END as the index if you want to add a new line to the end of listbox.


import tkinter as tk

def build():
#main window that contains the widgets.
root = tk.Tk()
#sets the title of window.
root.title(
"Listbox Demo")
#sets the dimensions of window(width x height).
root.geometry(
"250x100")

#create a listbox widget in the main window.
list_box = tk.Listbox(root)
#insert items into the listbox .
list_box.insert(
1, "I")
list_box.insert(
2, "II")
list_box.insert(
3, "Brotherhood")
list_box.insert(
4, "Revelations")
list_box.insert(
5, "III")
list_box.insert(
6, "Liberation")

list_box.pack()

#mainloop() is an infinite loop used to run the application,
#wait for an event to occur and process the event till the window is not closed.
root.mainloop()

if __name__ == "__main__":
build()


OUTPUT:

     Listbox with third field selected. 


              ----------------------------------------------------------------------------------------------------------------------------------------------------------------


Entry:

The Entry widget is used to accept single-line text strings from a user.

The get() method an be used to fetch the content entered by user in the widget.


The example shown uses a button to fetch the information entered in the entry widget.


import tkinter as tk

def build():
#main window that contains the widgets.
root = tk.Tk()
#sets the title of window.
root.title(
"Entry Demo")
#sets the dimensions of window(width x height).
root.geometry(
"250x100")

label_1 = tk.Label(root, text=
"Name")
entry_1 = tk.Entry(root, bd =
5)
button_1 = tk.Button(root,text =
"Click",command = lambda:print(entry_1.get()))

label_1.grid(row =
0,column = 0)
entry_1.grid(row =
0,column = 1)
button_1.grid(row =
1,column = 0)

#mainloop() is an infinite loop used to run the application,
#wait for an event to occur and process the event till the window is not closed.
root.mainloop()

if __name__ == "__main__":
build()



OUTPUT:



           ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Menu and config:

This widget allows us to create different types of menus that can be used by the application.


import tkinter as tk
import sys

def build():
#main window that contains the widgets.
root = tk.Tk()
#sets the title of window.
root.title(
"Menu Demo")
#sets the dimensions of window(width x height).
root.geometry(
"250x100")

#create a menu widget in main window.
#tearoff allows you to detach menus for the main window creating floating menus. Setting it to 0 turns off this feature.
menu_bar = tk.Menu(root,tearoff=
0)


file_menu = tk.Menu(root,tearoff=
0)
#Create a new hierarchical menu by associating a given menu to a parent menu.
menu_bar.add_cascade(label=
"File",menu=file_menu)

edit_menu = tk.Menu(root,tearoff=
0)
menu_bar.add_cascade(label=
"Edit",menu=edit_menu)

#add a new menu item to the menu.
file_menu.add_command(label=
"New",command = None)
file_menu.add_command(label=
"Open",command = None)
#add a separator line to the menu.
file_menu.add_separator()
file_menu.add_command(label=
"Save",command = None)
file_menu.add_command(label=
"Save As",command = None)
file_menu.add_separator()
file_menu.add_command(label=
"Exit",command=sys.exit)

edit_menu.add_command(label=
"Undo",command = None)
edit_menu.add_command(label=
"Redo",command = None)
edit_menu.add_separator()
edit_menu.add_command(label=
"Cut",command = None)
edit_menu.add_command(label=
"Copy",command = None)
edit_menu.add_command(label=
"Paste",command = None)
edit_menu.add_separator()
edit_menu.add_command(label=
"Find",command = None)
edit_menu.add_command(label=
"Replace",command = None)

#config() is used to access an object's attributes after its initialization,
#using which you can set the attribute, and modify it during runtime.
root.config(menu=menu_bar)
#mainloop() is an infinite loop used to run the application,
#wait for an event to occur and process the event till the window is not closed.
root.mainloop()

if __name__ == "__main__":
build()


OUTPUT:




More Articles of Akshay Koushik H:

Name Views Likes
Introduction to Python GUI using tkinter 372 11
Simple and Scientific Calculator using Tkinter 2200 25

Comments