16 – GUI Programming in Python

In earlier 1970’s the computer was operated only using command line interface, i.e. one cannot work without commands. To make it more user-friendly the GUI was developed.  GUI stands for Graphical User Interface. GUI basically contains windows, icons, menus, buttons, entry field to interact with Operating System instead of commands. Some examples of daily used GUI’s are

  • Microsoft programs like word, excel, powerpoint, etc
  • Internet Browsers like Chrome, etc
  • In ATM’s
  • In Android Applications etc

In Python, Tkinter is used to develop GUI. Mostly the Tkinter module comes inbuilt with Python. In Python 2.x series it can be imported using 

import Tkinter

In Python 3.x series it can be imported using 

import tkinter

BUTTON EXAMPLE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from Tkinter import *
from PIL import Image,ImageTk
root=Tk()
 
 
w = root.winfo_screenwidth()
h = root.winfo_screenheight()
rootsize = (200,150)
x = w/2 - rootsize[0]/2
y = h/2 - rootsize[1]/2
root.geometry("%dx%d+%d+%d" % (rootsize + (x, y)))
 
def f1():
    print "Welcome to Advanced Python Course from MYBTECHPROJECTS"
 
def f2():
    print "You clicked on MYBTECHPROJECTS Logo"
 
#Button(object,Text,command,image)    
b1=Button(root,text="Click here",command=f1)
b1.place(x=75,y=20)
 
photo=ImageTk.PhotoImage(file="MBPlogo.png")
 
b2=Button(root,text="Click here",command=f2,image=photo)
b2.place(x=75,y=70)
root.mainloop()

OUTPUT

1
2
3
4
>>> 
Welcome to Advanced Python Course from MYBTECHPROJECTS
You clicked on MYBTECHPROJECTS Logo
>>>

LABEL EXAMPLE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from Tkinter import *
root=Tk()
 
w = root.winfo_screenwidth()
h = root.winfo_screenheight()
rootsize = (200,150)
 
x = w/2 - rootsize[0]/2
y = h/2 - rootsize[1]/2
root.geometry("%dx%d+%d+%d" % (rootsize + (x, y)))
 
l1=Label(root,text="Enjoy Learning Python",height=2,width=20,fg="red")
l1.place(x=30,y=55)
 
root.mainloop()

ENTRY EXAMPLE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from Tkinter import *
root=Tk()
 
w = root.winfo_screenwidth()
h = root.winfo_screenheight()
 
rootsize = (200,100)
x = w/2 - rootsize[0]/2
y = h/2 - rootsize[1]/2
 
root.geometry("%dx%d+%d+%d" % (rootsize + (x, y)))
 
Label(root, text="First Name").grid(row=0)
Label(root, text="Last Name").grid(row=1)
 
e1 = Entry(root)
e2 = Entry(root)
e1.insert(10,"MY")
e2.insert(10,"BTECHPROJECTSa")
 
e1.grid(row=0, column=1)
e2.grid(row=1, column=1)
 
def show_entry_fields():
   print("First Name: %s\nLast Name: %s" % (e1.get(), e2.get()))
   e1.delete(0,END)
   e2.delete(0,END)
 
Button(root, text='Quit', command=root.destroy).grid(row=4, column=0, pady=4)
Button(root, text='Show', command=show_entry_fields).grid(row=4, column=1, pady=4)
 
root.mainloop( )

OUTPUT

1
2
3
4
5
6
>>> 
First Name: MY
Last Name: BTECHPROJECTS
First Name: Gowtham
Last Name: S
>>>

RADIO-BUTTON EXAMPLE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from Tkinter import *
root=Tk()
 
w = root.winfo_screenwidth()
h = root.winfo_screenheight()
rootsize = (200,220)
 
x = w/2 - rootsize[0]/2
y = h/2 - rootsize[1]/2
root.geometry("%dx%d+%d+%d" % (rootsize + (x, y)))
 
value=0
 
def getit():
    global value
    value= v.get()
 
def printit():
    global value
    if value==1:
        print "Python Selected"
    elif value==2:
        print "JAVA Selected"
    elif value==3:
        print "C++ Selected"
    elif value==4:
        print "C Selected"
 
v=IntVar()
v.set(1)
 
l1=Label(root,text="Which is best")
l1.config(bg="lightgreen",font=('times',18,'italic'))
l1.pack()
 
r1=Radiobutton(root,text="Python",variable=v,value=1,command=getit)
r1.place(x=60,y=60)/
 
r2=Radiobutton(root,text="JAVA",variable=v,value=2,command=getit)
r2.place(x=60,y=90)
 
r3=Radiobutton(root,text="C++",variable=v,value=3,command=getit)
r3.place(x=60,y=120)
 
r4=Radiobutton(root,text="C",variable=v,value=4,command=getit)
r4.place(x=60,y=150)
 
b1=Button(root,text="Get Selected",command=printit)
b1.place(x=50,y=190)
 
root.mainloop()

OUTPUT

1
2
3
4
5
6
>>> 
C++ Selected
C Selected
Python Selected
JAVA Selected
>>>

CHECK-BOX EXAMPLE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from Tkinter import *
root=Tk()
 
w = root.winfo_screenwidth()
h = root.winfo_screenheight()
rootsize = (200,250)
 
x = w/2 - rootsize[0]/2
y = h/2 - rootsize[1]/2
root.geometry("%dx%d+%d+%d" % (rootsize + (x, y)))
 
value=0
 
def getit():
    global value
    value= v.get()
 
def printit():
    global value
    value= v1.get()
    if value==1:
        print "Python Selected"
    else:
        print "Python Not Selected"
 
    value= v2.get()
    if value==1:
        print "JAVA Selected"
    else: 
        print "JAVA Not Selected"
 
    value= v3.get()
    if value==1:
        print "C++ Selected"
    else: 
        print "C++ Not Selected"
 
    value= v4.get()
    if value==1:
        print "C Selected"
    else:
        print "C Not Selected"
 
v1=IntVar()
v2=IntVar()
v3=IntVar()
v4=IntVar()
 
 
l1=Label(root,text="Which is best")
l1.config(bg="lightgreen",font=('times',18,'italic'))
l1.pack()
 
r1=Checkbutton(root,text="Python",variable=v1)
r1.place(x=60,y=60)
 
r2=Checkbutton(root,text="JAVA",variable=v2)
r2.place(x=60,y=90)
 
r3=Checkbutton(root,text="C++",variable=v3)
r3.place(x=60,y=120)
 
r4=Checkbutton(root,text="C",variable=v4)
r4.place(x=60,y=150)
 
b1=Button(root,text="Get Selected",command=printit)
b1.place(x=50,y=180)
 
e=Button(root,command=root.destroy,text="Exit")
e.place(x=75,y=210)
root.mainloop()

OUTPUT

1
2
3
4
5
6
>>> 
Python Selected
JAVA Selected
C++ Not Selected
C Not Selected
>>>

MESSAGE BOX EXAMPLE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from Tkinter import *
from tkMessageBox import *
 
root=Tk()
 
w = root.winfo_screenwidth()
h = root.winfo_screenheight()
rootsize = (200,150)
x = w/2 - rootsize[0]/2
y = h/2 - rootsize[1]/2
root.geometry("%dx%d+%d+%d" % (rootsize + (x, y)))
 
def answer():
    showerror("Error", "This is an error message")
 
def f1():
    if askyesno('Verify', 'Really quit?'):
        root.destroy()
    else:
        showinfo('No', 'Quit has been cancelled')
 
def f2():
    if askyesno('Verify', 'Do you like MYBTECHPROJECTS?'):
        print "Thank You for the complement"
    else:
        print "It's Okay"
Label(root,text="Message Box Example").place(x=40,y=10)
 
Button(root,text='Answer', command=f2).place(x=60,y=40)
Button(root,text='Show Error', command=answer).place(x=50,y=70)
Button(root,text='Quit', command=f1).place(x=65,y=100)
 
root.mainloop()

OUTPUT

1
2
3
4
>>> 
Thank You for the complement
It's Okay
>>>

 

MENU EXAMPLE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from Tkinter import *
from tkFileDialog   import *
 
def NewFile():
    name = asksaveasfilename(title = "Choose a file.")
    print "File created" ,name
    if name is None: 
        return
    f=open(name,'w')
    f.write("Greetings from MYBTECHPROJECTS")
    f.close()
 
def OpenFile():
    name = askopenfilename(title = "Choose a file.")
    print "File you selected ",name
 
def About():
    print "This is a simple example of a menu"
 
root = Tk()
 
menu = Menu(root)
root.config(menu=menu)
filemenu = Menu(menu)
#Main Menu
menu.add_cascade(label="File", menu=filemenu)
 
#Sub Menu
filemenu.add_command(label="New", command=NewFile)
filemenu.add_command(label="Open...", command=OpenFile)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=root.quit)
 
helpmenu = Menu(menu)
menu.add_cascade(label="Help", menu=helpmenu)
helpmenu.add_command(label="About...", command=About)
 
mainloop()

THANK YOU

 

SHARE THIS!!!

Gowtham S

Gowtham is a programming enthusiast. His field of interest includes Arduino, NodeMCU, Raspberry Pi, and Python. To know more about him visit https://mybtechprojects.tech/about-us/.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.