Introduction to Tkinter module in Python
In this article, we will learn about Tkinter module in Python. This article will help you to understand with Tkinter library in python and gives you a brief idea about the Graphical User Interfaces (GUI) applications in python.
Tkinter module in Python
First, we have to import Tkinter module by simply and at the same time we create the main window. Basically in this window we have to do all the operations or we can say we use all the functions of Tkinter module. The main window is created by using Tk() function of Tkinter module at the same time we have to close that window by command window.mainloop() as:
import tkinter window=tkinter.Tk() window.mainloop()
output is simply a Tkinter window that we have created.
Functions of Tkinter module
- tkinter.Label(window,text=” “).pack() : This method is used to give some label or name to our window.It takes two argument one is on which that you want to put and what you want to put.
- tkinter.Frame(window, width, height): This is used as containers in the Tkinter module. It takes 3 arguments window and width or height of frame that the user wants.
- Entry(window): This method or widget is used to create input fields in the GUI or entry buttons in our created frame.
- checkbutton(window,text=” “): This method is used to create the check buttons in your application. It takes two arguments one is the window on which you want to put that button second is text that you want to apply on this button.
- mainloop(): This method is used when you are ready to run your application.
- canvas(): This function is used to draw complex pictures on the frame.
You may take a look at this: Tkinter pack(), grid() Method In Python
The code with all functions that we have used above is:
from tkinter import * m=Tk() m.title("NUMBER GUESSING GAME") lable=Label(m,text="CodeSpeedy") lable.pack() frame=Frame(m,width=300,height=300) button1=Button(frame,text="enter") button2=Button(frame,text="number 1") button3=Button(frame,text="number 2") button4=Button(frame,text="number 3") button4.pack(side=LEFT) button3.pack(side=LEFT) button2.pack(side=LEFT) button1.pack(side=LEFT) frame.pack() bottomframe=Frame(m,width=300,height=300) lable2=Label(bottomframe,text="JITENDRA KUMAR") button5=Button(bottomframe,text="Exit") button5.pack(side=RIGHT) bottomframe.pack(side=BOTTOM) mainloop()
I am not giving any output here as I want you to go try and run on your machine.
You can also see: