Adding a Working quit Button in Tkinter window|How to add quit button in Tkinter|How to close a tkinter window in Python





Introduction

In this Page we are going to learn how can we add a Working quit button in tkinter main window also known as root window.
In other words we are going to demonstrate how to close a tkinter window.
This blog also targets on How you can tkinter and python on Android.

About Tkinter
Tkinter gives us the way in through which we can build a GUI in Python.It can be said as Python interface to the Tk GUI toolkit,which comes with Python.Tkinter is the standard library in Python. When Python and Tkinter are combined then they provide a fast and easy way to create GUI applications. Tkinter provides a powerful object-oriented interface to the Tk GUI toolkit.

Basic steps to start your tkinter-python program are:-
  • Import Tkinter module.
  • Create main window of GUI generally known as Root window.
  • Add widgets you want.
  • Enter the main event loop using root.mainloop() to take action against each operation defined by you.
Widgets
Tkinter provides user to create different objects in their window which are known as Widgets.Example of such Widgets are; buttonslabels and text boxes used in a GUI application.

Button
The Button widget is used to add button in a Tkinter Window.These buttons can display text on them or images to convey its function to the user. You can give a command by defining a function and adding to the widget using the appropriate code. A button Widget can be customized in easy way by adjusting the size,borders,color,font,Relief structure.etc.

Relief style that we are going to use:-
Generally "raised" relief style is used for buttons,as it provides the exact 3-D effect for a button .In this project we have also used the same relief style.If you don't know about relief styles of widgets The following link may help you out.


NOTE:- IF YOU WANT TO COPY COMMAND FOR QUIT BUTTON THEN COPY ONLY THE #QUIT BUTTON PART! OTHERWISE IT WON'T WORK
OR
Watch the youtube tutorial attached to this blog at the end.

Source code:-

from tkinter import *

root=Tk()

root.geometry("680x1200")


#Labels


SPC1=Label(root,text="")

SPC1.grid(column=1,row=1)

Label1=Label(root,text=" USERNAME",

relief="ridge")

Label1.grid(column=1,row=2,padx=60)


SPC2=Label(root,text="")

SPC2.grid(column=1,row=3)

Label2=Label(root,text="Password",relief="ridge")

Label2.grid(column=1,row=4,padx=60)

#Entry WIDGETS

Entry1=Entry(root, width=17,justify=CENTER)

Entry1.grid(column=2,row=2)

Entry2=Entry(root,width=17,

justify=CENTER)

Entry2.grid(column=2,row=4)


#QUIT BUTTON

quit=Button(root,text="Quit",width=30,relief="raised",borderwidth=10,command=root.destroy)

quit.grid(column=1,row=8,columnspan=25)


SPC3=Label(root,text="")

SPC3.grid(column=1,row=5)

SPC4=Label(root,text="")

SPC4.grid(column=1,row=6)

SPC5=Label(root,text="")

SPC5.grid(column=1,row=7)


root.mainloop()

#----------------------CODE ENDED-----------------------

Output screen:-
  Click for full resolution image☝
 

About the code:-
The above code is used for making a window(GUI) of  a basic login screen.
It have a Quit button in the south(down) part of the window which is the Main topic of this blog.The quit button uses command of  root.destroy() to trigger the exit function.

root.destroy()

This is used to close all the windows which are displayed along with the root window.Refer the code for better understanding.

About Youtube series
All the execution of the above code was done on Android device.The playlist is based on MAKING GUI ON ANDROID Device.
For more information Visit Our Youtube channel.

     For Tutorial Watch My YouTube Video:-



"Hope it helped"
Have anything in mind? Let me 
know in the comment section👇


Next Post Previous Post
1 Comments
  • RAIN PAGE NEWS
    RAIN PAGE NEWS January 16, 2022 at 8:40 PM

    Thanks

Add Comment
comment url