Python Core



Admission Enquiry Form


GUI Programming using tkinter: in Python

#tkinter is the standarad module that provides the classes for GUI in Python
#Just import the tkinter module in your python file and start using
#Before writing the GUI Programs in Python, we should have some knowledge of 'GUI environment
As you know GUI Stands for Graphical User Interface in which user interacts with computer
with the help of graphics means pictures or img. So nowadays we are using GUI to
interacts with computer e.g Windows 10 etc. In GUI environment we use various types of
components to interact like Window,buttons,textboxes,labels,frames,panels,listboxes,checkboxes,
radiobuttons,scrollbars,spinners
etc. These components are known as widgets in Python GUI
Programming or in tkinter

Example is as follows:

import tkinter as tk #importing tkinter module
window=tk.Tk() #creating window or main window
window.title("compuhelp") # setting title of window
window.geometry("300x200") #setting size of window

window.mainloop() #it displays the window




Code for Adding Button to the window:

import tkinter as tk
import tkinter.messagebox
window=tk.Tk()
window.title("Compuhelp")
window.geometry("300x200")
btn1=tk.Button(window,text="Button1")
btn2=tk.Button(window,text="Button2")
btn1.pack()
btn2.pack()
def message():
#It will display information message
    ans=tkinter.messagebox.showinfo("Greetings","Button Pressed")

# it will display message with yes no button
    ans=tkinter.messagebox.askyesno("query","Do you want to close ?")
    if ans == True:
        window.title("yes pressed" + str(ans))
    else:
        window.title("no Pressed" + str(ans))
btnsum=tk.Button(window,activebackground="red",activeforeground="white",text="Sum",
width="25",command=message)
#in the above code message method will be invoked while we click sum button

btnsum.place(x=50,y=200)
window.mainloop()



Output




Code for Adding Label and Textbox(Entry):

import tkinter as tk #importing tkinter module
window=tk.Tk() #creating window or main window
window.title("compuhelp") # setting title of window
window.geometry("300x200") #setting size of window
label1=tk.Label(window,text="Enter Text")#creating label
label1.place(x=10,y=20)
txt=tk.Entry(window,width=20) # creating textbox
txt.place(x=100,y=20)
window.mainloop() #it displays the window



Output



Example of CheckBoxes and RadioButtons:

import tkinter as tk #importing tkinter module
window=tk.Tk() #creating window or main window
window.title("compuhelp") # setting title of window
window.geometry("300x200") #setting size of window
chk1=tk.Checkbutton(window,text="Python")
chk2=tk.Checkbutton(window,text="Javascript")
gender= tk.IntVar()

rd_male=tk.Radiobutton(window,text="Male",variable=gender,value='M')
rd_female=tk.Radiobutton(window,text="Female",variable=gender,value='F')
chk1.pack()
chk2.pack()
rd_male.pack()
rd_female.pack()
window.mainloop()



Output



Example of ListBox:

import tkinter as tk #importing tkinter module
window=tk.Tk() #creating window or main window
window.title("compuhelp") # setting title of window
window.geometry("300x200") #setting size of window
mylist=tk.Listbox(window)
mylist.insert(1,"Python")
mylist.insert(2,"HTML")
mylist.insert(3,"CSS")
mylist.insert(4,"JavaScript")
mylist.pack()
window.mainloop()



Output



Example of Scale.It provides the slider to select the value from:

import tkinter as tk #importing tkinter module
window=tk.Tk() #creating window or main window
window.title("compuhelp") # setting title of window
window.geometry("300x200") #setting size of window
sc=tk.Scale(window,from_=1,to=20)
sc.pack()
sc1=tk.Scale(window,from_=50,to=150,orient=tk.HORIZONTAL)
window.mainloop()



Output



Example of Vertical and Horizontal Scale:

import tkinter as tk #importing tkinter module
window=tk.Tk() #creating window or main window
window.title("compuhelp") # setting title of window
window.geometry("300x200") #setting size of window
sc=tk.Scale(window,from_=1,to=20)
sc.pack()
sc1=tk.Scale(window,from_=50,to=150,orient=tk.HORIZONTAL)
sc1.pack()
window.mainloop()



Output