What is Tkinter?
Tkinter is an inbuilt Python module used to create simple GUI application. It is the most commonly used module for GUI apps in Python. It has many geometry managers which make it more attractive.
It is by default come with python so no need to worry about it.
In this, I am using python 3.7 version. So that before starting it first fix your version issue.
You can download Python latest version, directly from the Python official website.
Steps which you can follow before start writing code
1 - import tkinter module first
from tkinter import *
import tkinter as tk or import tkinter
2 - In the second step initialize window manager using,
root = tk.TK() or root = TK()
3 - Then set window title
Tkinter has many GUI widgets, which used to create attractive GUI, in this blog we will learn some important geometry manager like grid(), pack(), etc., which used to maintain the position of widgets.
Before start first we understand basic Tkinter code format:
Before starting to create Tkinter GUI first have knowledge all Tkinter widgets, here we provided list of all Tkinter widgets
Button: Button widget is used to place the buttons in the Tkinter
Canvas: Canvas is used to draw shapes in your GUI
Check button: The check button is used to create the check buttons in your application. You can select more than one option at a time.
Entry: Entry widget is used to create input fields in the GUI.
Frame: Frame is used as containers in the Tkinter.
Label: Label is used to create a single line widgets like text, images, etc..,
Menu: Menu is used to create menus in the GUI.
from tkinter import *
root = Tk()
### Add window title and set size here ###
### Add widgets here ###
Example to set Tkinter Geometry
Here we will discuss some examples which clearly understandable.
Before start it first we know all geometry methods:
These are the topmost important methods which used to set all Tkinter GUI but here some point which is necessary to know.
We can't use pack() with grid() geometry so before starting it is decide which is important to create GUI.
In my overview, if any previous conditions are not given then you choose grid(), because you can easily set x and y dimension:
grid(row = 10, column = 50)
And pack() can we define using
pack(side = Option)
where the option is LEFT, RIGHT. TOP, BOTTOM
You can use pack() with the place(x= 10, y=20), you can use both GUI in the same both are work.
This is the better way to create a Tkinter GUI.
Example to create a GUI using grid()
In this example, we use some mathematical calculations like a sub, add, multiplication, division like a simple calculator, and output shown in the label below the button.
Here we attached screenshots of output here for your better understanding regarding this.
Here we attached the code which is completely done using grid().
You can also try itself pack() with place using this.
Set Label entry using StringVar()
First, define it: in the constructor class
self.result = StringVar()
Then use it in methods to print results in the label and updated by the new value.
Pass variable to label:
self.result_lebel = Label(self.master,textvariable=self.result, bg = "skyblue")
Then print the result using methods: for example
num1 = int(self.entry_1.get())
num2 = int(self.entry_2.get())
result = num1 + num2
self.result.set("The Sum is " + str(result))
The Second Syntax we will use frame it is very important to divide the window into more than two parts
Creating Tkinter Gui using a frame. Here we use some basic syntax to create the GUI using frame. It uses when we needed to divide the window into many parts as per your choice.
We can use a simple frame as:
First set frame using a master or main window, then use as per given below syntax of code:
If you like Codersarts blog and looking for Programming Assignment Help Service, Database Development Service, Web development service, Mobile App Development, Project help, Hire Software Developer, Programming tutors help and suggestion you can send mail at firstname.lastname@example.org.
Please write your suggestion in the comment section below if you find anything incorrect in this blog post