DEVARSH CONTRACTOR
Retry🔁 Tkinter The-GUI-Toolkit
Introduction

In the fastly growing world, software applications have played an important role in accelerating the speed of the world. Apps are made faster, optimized, user-friendly, and reliable for the user-end. Accelerating the development growth of software application has made it easy withwhich itself provides an user-friendly environment for the developers which initiated and highlighted the encouragement towards Python development.

is a key feature in software applications for the user-friendly experience; With the growing community in Python, it too supports the GUI package. Tkinter is an in-Built GUI package provided along with python which can be used for GUI development in Python.

Getting Started with Tkinter, let's move towards history and working of tkinter. Tkinter is a Python interface and used as a package in Python. It uses /libraries. The tk has old classic components/widgets. Tkinter had introduced them in 1991. The newer themed ttk widgets added in 2007 with Tk 8.5. The newer Tk themed widgets replace many (but not all) classic widgets. ttk stands for Tk themed.


Installing

Installing Tkinter is like nothing to do. If you have installed theversion on your machine then you are good to go for GUI development with Tkinter. As it is pre-installed with the standard Python Library.




Simple Hello World

For creating a Simple Hello World Program, will firstly need to import Tkinter module to our program.
import * means import everything from the module.







Layouts

Tkinter Layouts are the method in which the components are align or arrange in the GUI program.
There are three basic layout handlers in Tkinter.


Pack

The pack method layout is a basic and easy to use method which Tkinter provides.
root.pack(args, ...)

Pack uses geometery alignments such as: fill, expand, side, ipadx, ipady, padx, and pady.
Fill it takes three values 'x', 'y' and 'both'.
'x' - fills the available space in the x-axis,
'y' - fills the available space in the y-axis,
'both' - fills the available space in both xy-axis
Expand it provides additional space to widget.
side it specifices the alignment of the widget in 'left', 'rigth', 'top', 'bottom' direction. Default value is 'top'.
ipadx it sets padding on x-axis.
ipady it sets padding on y-axis.


Grid

The grid method layout is an intermediate level and uses table column and row method to align the components.
root.grid(args, ...)

column it takes column value to place the component.
row it takes row value to place the component.
sticky it specifies which side the widget should stick to and how to distribute any extra space within the cell that is not taken up by the widget at its original size.

ValuesLocation
NNorth or Top-Center
SSouth or Bottom-Center
EEast or Right-Center
WWest or Left-Center
NWNorth-West or Top-Left
NENorth-East or Top-Right
SESouth-East or Bottom-Right
SWSouth-West or Bottom-Left
NSCentered horizontally.
EWCentered vertically.
padx it takes value for external padding on x-axis.
pady it takes value for external padding on y-axis.
ipadx it takes value for internal padding on x-axis.
ipady it takes value for internal padding on x-axis.
By default internal and external padding value is 0.


Place

The place method layout is an advanced method of Tkinter alignment system. Moreover it is precise as it is uses coordinate method to align the components.
root.place(args, ...)

The place geometry comes with both absolute and relative positioning options. Absolute positioning is specified by the 'x' and 'y' options. Relative positions is specified by the 'relx' and 'rely' options.
The absolute width and height of the component in pixels along with relative width and height using the relwidth and relheight options.
The value of relwidth and relheight are float values between 0.0 and 1.0.




Responsive

Command Binding

Command Binding is a method in which we bind callback function with the button to make it Responsive.
When the button is click, the callback will be invoked automatically to handle the event.
def callbackFunction():
  # your code

Button.(root,
  text = 'Display some text',
  command = callbackFunction
)

  # or we can use lambda keyword for functions with arguments.
def callbackFunction(args):
  # your code

Button.(root,
  text = 'Display some text',
  command = lambda: callbackFunction(args)
)

There are some limitation for Command Binding. The command option is not available with all widgets. It is limited to the Button and some other widgets.
Solving the issue with Command Binging, Tkinter add new feature Event Binding.


Event Binding

In event binding components are set for events and on counter of any event the binded function will get called to execute.
def callbackFunction(event):
  # your code

varName = Button.(root,
    text = 'Display some text'
)
varName.bind('',callbackFunction)



Window Components

Add window title using title() method:
window.title('NEW_TITLE')
Get window title title() method:
varName = window.title()

Set window scale and size using geometry() method:
window.geometry(width x height ± x ± y)
Get window scale and size using geometry() method:
varName = window.geometry()

Grant resizable window access using resizable() method:
window.resizable([true|false])
# you can use 1 for true and 0 for false

Maximum resizable limit and minimum resizable limit using maxsize() and minsize() methods:
window.maxsize(width, height)
window.minsize(width, height)


Set screen order for window using attributes() lift() lower() methods:
window.attributes('-topmost', 1)
This will set screen order always top.
We can also set transparency for the window using attributes()
window.attributes('alpha', 0.2)

Change Icon or Logo of the Window using iconbitmap() method:
window.iconbitmap('../path/image.ico')
It supports .ico format for Icons.