Professional Documents
Culture Documents
Unit - V Packages & Gui
Unit - V Packages & Gui
Python packages:
Illustrative programs:
GUI Programming:
Tkinter introduction,
Tkinter and Python Programming,
Tk Widgets,
Tkinter examples.
Python programming with IDE.
Illustrative programs:
1. Built-in modules:
Python boasts an extensive collection of built-in modules designed to simplify tasks and
enhance code readability. These modules offer a diverse range of functionality and are readily
accessible without the requirement of installing extra packages. With these built-in modules,
Python provides a comprehensive set of tools and capabilities right out of the box, allowing
developers to accomplish various tasks conveniently and without the hassle of additional
installations.
A list of a few most frequently used built-in python modules is given below
math: This module is very useful to perform complex mathematical functions such as
trigonometric functions and logarithmic functions.
date: The date module can be used to work with date and time such as time, date, and
datetime.
os: Provides a way to interact with the underlying operating system, such as reading or
writing files, executing shell commands, and working with directories.
sys: Provides access to some variables used or maintained by the Python interpreter, such as
the command-line arguments passed to the script, the Python version, and the location of the
Python executable.
Code
For example, let‟s take the datetime module, which has a submodule called date. When
datetime is imported, it‟ll result in an error, as shown below:
import datetime
date.today()
Output
Traceback (most recent call last):
File "main.py", line 2, in <module>
date.today()
NameError: name 'date' is not defined
The correct way to use the date module is shown below:
Output
2024-04-15
User-defined python modules are the modules, which are created by the user to simplify their
project. These modules can contain functions, classes, variables, and other code that you can
reuse across multiple scripts.
Any Python file, whose name is the module‟s name with the .py extension, is a module. A
package is a directory of Python modules that contains an additional __init__.py file, which
distinguishes a package from a directory that is supposed to contain multiple Python scripts.
Packages can be nested to multiple depths if each corresponding directory contains its own
__init__.py file.
When you import a module or a package, the object created by Python is always of type
module.
When you import a package, only the methods and the classes in the __init__.py file of that
package are directly visible.
In Python, the module is a Python script with a .py extension and contains objects such as
classes, functions, etc. Packages in Python extend the concept of the modular approach
further. The package is a folder containing one or more module files; additionally, a special
file "__init__.py" file may be empty but may contain the package list.
Let us create a Python package with the name mypackage. Follow the steps given below −
Create an outer folder to hold the contents of mypackage. Let its name
be packagedemo.
Inside it, create another folder mypackage. This will be the Python package we are
going to construct. Two Python modules areafunctions.py and mathfunctions.py will
be created inside mypackage.
Create an empty "__.init__.py" file inside mypackage folder.
Inside the outer folder, we shall later store a Python script example.py to test our
package.
def average(x,y):
val = (x+y)/2
return val
def power(x,y):
val = x**y
return val
def circle(r):
import math
area = math.pi*math.pow(r,2)
return area
Let us now test the myexample package with the help of a Python script above this package
folder. Refer to the folder structure above.
#example.py
from mypackage.areafunctions import rectangle
print ("Area :", rectangle(10,20))
You can put selected functions or any other resources from the package in the "__init__.py"
file. Let us put the following code in it.
from .areafunctions import circle
from .mathsfunctions import sum, power
To import the available functions from this package, save the following script as
testpackage.py, above the package folder as before.
Output
Area of circle: 78.53981633974483
10 raised to 2: 100
# x axis values
x = [1,2,3]
# corresponding y axis values
y = [2,4,1]
Exploring Different Types of Plots, Best Practices, and Tips for Effective Data
Visualization
Matplotlib is a plotting library for the Python programming language and its numerical
mathematics extension NumPy. It provides an object-oriented API for embedding plots into
applications using general-purpose GUI toolkits like Tkinter, wxPython, Qt, or GTK.
Matplotlib is a powerful tool for data visualization in data science and can be used to create a
wide variety of plots, including line plots, scatter plots, bar plots, histograms, 3D plots, and
more. Some of the key features of matplotlib include support for customizable plot styles and
color maps, interactive plot manipulation, and a variety of export options for creating
publication-quality figures.
Line Plot:
A line plot is a way to display data along a number line. It is useful to show trends over time
or to compare multiple sets of data. It is created using the plot function in matplotlib, which
takes in the x and y data as arguments. In the example I gave, the x data is an array of 100
evenly spaced points between 0 and 10 and the y data is the sine of x values.
Scatter Plot:
A scatter plot is used to show the relationship between two variables. It is created using
the scatter function in matplotlib, which takes in the x and y data as arguments. In the
example I gave, x and y are arrays of random values generated using
the random.normal function from numpy. It shows the correlation or distribution of data
points.
A bar plot is used to compare the values of different categories. It is created using
the bar function in matplotlib, which takes in the x and y data as arguments. In the example I
gave, x data is an array of categorical values ('A','B','C','D') and y data is an array of values.
Box Plot:
A box plot is used to show the distribution and outliers of a set of data. It is created using
the boxplot function in seaborn, which takes in the data and the variables to plot as
arguments. In the example I gave, the data is an array of random values generated using
the random.normal function from numpy.
import seaborn as sns
A heatmap is used to visualize large data with multiple variables. It is created using
the heatmap function in seaborn, which takes in the data as an argument. In the example I
gave, the data is a 2-D array of random values generated using the random.normal function
from numpy. The color of the cells represents the value of each element in the matrix.
Violin Plot:
Violin Plots are similar to box plots, but also display the probability density of the data at
different values. They can be created using the violinplot function in seaborn
Swarm Plot :
A swarm plot is used to show the distribution of a single categorical variable. It is created
using the swarmplot function in seaborn, which takes in the data and the variables to plot as
arguments. In the example I gave, the x data is an array of random values generated using the
random.normal function from numpy and y data is an array of categorical values(0,1)
A pie chart is used to show the proportion of different categories in a single variable. It is
created using the pie function in matplotlib, which takes in the data and the labels as
arguments. In the example I gave, the data is an array of values representing the size of each
category and the labels are the names of each category. Additionally, you can use the autopct
parameter to add the numerical value of each slice on the chart.
A stacked bar plot is used to show the breakdown of one variable by another. It is created
using the bar function in matplotlib and bottom attribute of bar function. In the example I
gave, Two sets of data are plotted as separate bars, one on top of the other, to show the
breakdown of one variable by another. The legend is used to distinguish between the two sets
of data.
N=5
menMeans = (20, 35, 30, 35, 27)
womenMeans = (25, 32, 34, 20, 25)
menStd = (2, 3, 4, 1, 2)
womenStd = (3, 5, 2, 3, 3)
ind = np.arange(N) # the x locations for the groups
width = 0.35 # the width of the bars: can also be len(x) sequence
plt.ylabel('Scores')
plt.title('Scores by group and gender')
plt.xticks(ind, ('G1', 'G2', 'G3', 'G4', 'G5'))
plt.yticks(np.arange(0, 81, 10))
plt.legend((p1[0], p2[0]), ('Men', 'Women'))
plt.show()
In conclusion, Matplotlib and Seaborn are powerful libraries for data visualization in data
science. They provide a wide range of options for creating different types of plots, from
simple line plots to more complex heatmaps and violin plots. Each type of plot has its own
strengths and can be used to effectively communicate different types of information.
When creating plots, it's important to consider the context of your data and the audience for
your plots. Choosing the right type of plot depends on the nature of your data and what you
want to communicate with your plot. Additionally, you should also pay attention to the details
of the plot, like labels, scales, and colors, to make sure your plot is easy to read and
understand.
Lastly, always keeping in mind the data you have and what are the important information you
want to show, this will make sure that you choose the right type of plot and customize it to
convey the correct information in a
Python Numpy
Arrays in Numpy
Array in Numpy is a table of elements (usually numbers), all of the same type, indexed by a
tuple of positive integers. In Numpy, number of dimensions of the array is called rank of the
array.A tuple of integers giving the size of the array along each dimension is known as shape of
the array. An array class in Numpy is called as ndarray. Elements in Numpy arrays are
accessed by using square brackets and can be initialized by using nested Python Lists.
Note: Type of array can be explicitly defined while creating the array.
NumPy Arrays provides the ndim attribute that returns an integer that tells us how many
dimensions the array have.
Example
import numpy as np
a = np.array(42)
b = np.array([1, 2, 3, 4, 5])
c = np.array([[1, 2, 3], [4, 5, 6]])
d = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])
print(a.ndim)
print(b.ndim)
print(c.ndim)
print(d.ndim)
Output:
0
1
2
3
# Printing elements at
# specific Indices
Index_arr = arr[[1, 1, 0, 3],
[3, 2, 1, 0]]
print ("\nElements at indices (1, 3), "
"(1, 2), (0, 1), (3, 0):\n", Index_arr)
Output:
Initial Array:
[[-1. 2. 0. 4. ]
[ 4. -0.5 6. 0. ]
[ 2.6 0. 7. 8. ]
[ 3. -7. 4. 2. ]]
Array with first 2 rows and alternate columns(0 and 2):
[[-1. 0.]
[ 4. 6.]]
Elements at indices (1, 3), (1, 2), (0, 1), (3, 0):
[0. 6. 2. 3.]
Output:
Adding 1 to every element:
[[2 3]
[4 5]]
Subtracting 2 from each element:
[[ 2 1]
[ 0 -1]]
Sum of all array elements: 10
Array sum:
[[5 5]
[5 5]]
Output:
Integer Datatype:
int64
Float Datatype:
float64
Forcing a Datatype:
int64
Output:
Addition of Two Arrays:
[[ 7. 13.]
[ 4. 14.]]
Addition of Array elements:
19.0
Square root of Array1 elements:
[[2. 2.64575131]
[1.41421356 2.44948974]]
Transpose of Array:
[[4. 2.]
[7. 6.]]
arr = np.ones(3)
print(arr)
Output:
[1. 1. 1.]
Explanation: In this example, we created an array of size 3 which is filled with only ones.
5. np.linspace(): This function is used to create an array with a specified number of evenly
spaced values.
Example Code:
import numpy as np
arr = np.linspace(0, 1, 5)
print(arr)
Output:
[0. 0.25 0.5 0.75 1. ]
6. np.random.rand(): This function is used to create an array with random values between 0
and 1.
Example Code:
import numpy as np
arr = np.random.rand(3)
print(arr)
Output:
[0.5488135 0.71518937 0.60276338]
Explanation: In this example, we created an array of size 3 which is filled with random
values which lie between 0 and 1.
7. np.random.randint(): This function is used to create an array with random integer values
between a specified range.
Example Code:
import numpy as np
10. np.mean(): This function is used to find the mean value of an array.
Example Code:
import numpy as np
arr = np.array([1, 2, 3])
mean_value = np.mean(arr)
print(mean_value)
Output:
2.0
11. np.median(): This function is used to find the median value of an array.
Example Code:
import numpy as np
12. np.dot(): This function is used to find the dot product of two arrays.
Example Code:
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
dot_product = np.dot(arr1, arr2)
print(dot_product)
Output:
32
Explanation: In this example, we created two NumPy arrays called array1 and array2, which
contain the values [1, 2, 3] and [4, 5, 6], respectively. We then used np.dot() to calculate the
dot product of these two arrays, which is equal to 14 + 25 + 3*6, or 32. The resulting value is
stored in the variable result and printed to the console.
Python Pandas
The term "Pandas" refers to an open-source library for manipulating high-performance data in
Python. This instructional exercise is intended for the two novices and experts.
It was created in 2008 by Wes McKinney and is used for data analysis in Python. Pandas is an
open-source library that provides high-performance data manipulation in Python. All of the
basic and advanced concepts of Pandas, such as Numpy, data operation, and time series, are
covered in our tutorial.
Pandas Introduction
The name of Pandas is gotten from the word Board Information, and that implies an
Econometrics from Multi-faceted information. It was created in 2008 by Wes McKinney and
is used for data analysis in Python.
Processing, such as restructuring, cleaning, merging, etc., is necessary for data analysis.
Numpy, Scipy, Cython, and Panda are just a few of the fast data processing tools available.
Yet, we incline toward Pandas since working with Pandas is quick, basic and more expressive
than different apparatuses.
Since Pandas is built on top of the Numpy bundle, it is expected that Numpy will work with
Pandas.
Before Pandas, Python was able for information planning, however it just offered restricted
help for information investigation. As a result, Pandas entered the picture and enhanced data
analysis capabilities. Regardless of the source of the data, it can carry out the five crucial
steps that are necessary for processing and analyzing it: load, manipulate, prepare, model, and
analyze.
It has a DataFrame object that is quick and effective, with both standard and
custom indexing.
Utilized for reshaping and turning of the informational indexes.
For aggregations and transformations, group by data.
It is used to align the data and integrate the data that is missing.
Provide Time Series functionality.
Process a variety of data sets in various formats, such as matrix data,
heterogeneous tabular data, and time series.
Manage the data sets' multiple operations, including subsetting, slicing, filtering,
groupBy, reordering, and reshaping.
It incorporates with different libraries like SciPy, and scikit-learn.
Performs quickly, and the Cython can be used to accelerate it even further.
Benefits of Pandas
Clear code: Pandas' clear API lets you concentrate on the most important part of the code. In
this way, it gives clear and brief code to the client.
DataFrame and Series are the two data structures that Pandas provides for processing data.
These data structures are discussed below:
1) Series
A one-dimensional array capable of storing a variety of data types is how it is defined. The
term "index" refers to the row labels of a series. We can without much of a stretch believer
the rundown, tuple, and word reference into series utilizing "series' technique. Multiple
columns cannot be included in a Series. Only one parameter exists:
Before creating a Series, Firstly, we have to import the numpy module and then use array()
function in the program.
import pandas as pd
import numpy as np
info = np.array(['P','a','n','d','a','s'])
a = pd.Series(info)
print(a)
Output
0 P
1 a
2 n
3 d
4 a
5 s
dtype: object
Explanation: In this code, firstly, we have imported the pandas and numpy library with
the pd and np alias. Then, we have taken a variable named "info" that consist of an array of
some values. We have called the info variable through a Series method and defined it in an
"a" variable. The Series has printed by calling the print(a) method.
It is a generally utilized information design of pandas and works with a two-layered exhibit
with named tomahawks (lines and segments). As a standard method for storing data,
DataFrame has two distinct indexes-row index and column index. It has the following
characteristics:
It can be thought of as a series structure dictionary with indexed rows and columns. It is
referred to as "columns" for rows and "index" for columns.
import pandas as pd
# a list of strings
x = ['Python', 'Pandas']
# Calling DataFrame constructor on list
df = pd.DataFrame(x)
print(df)
Output
0
0 Python
1 Pandas
Explanation: In this code, we have characterized a variable named "x" that comprise of
string values. On a list, the values are being printed by calling the DataFrame constructor.
Python Pandas Series
The Pandas Series can be defined as a one-dimensional array that is capable of storing various data
types. We can easily convert the list, tuple, and dictionary into series using "series' method. The
row labels of series are called the index. A Series cannot contain multiple columns. It has the
following parameter:
Creating a Series:
We can easily create an empty series in Pandas which means it will not have any value.
The below example creates an Empty Series type object that has no values and having default
datatype, i.e., float64.
Example
1. import pandas as pd
2. x = pd.Series()
3. print (x)
Output
o Array
o Dict
o Scalar value
Before creating a Series, firstly, we have to import the numpy module and then use array() function
in the program. If the data is ndarray, then the passed index must be of the same length.
If we do not pass an index, then by default index of range(n) is being passed where n defines the
length of an array, i.e., [0,1,2,....range(len(array))-1].
Example
1. import pandas as pd
2. import numpy as np
3. info = np.array(['P','a','n','d','a','s'])
4. a = pd.Series(info)
5. print(a)
Output
0 P
1 a
2 n
3 d
4 a
5 s
dtype: object
We can also create a Series from dict. If the dictionary object is being passed as an input and
the index is not specified, then the dictionary keys are taken in a sorted order to construct the
index.
If index is passed, then values correspond to a particular label in the index will be extracted from
the dictionary.
Output
x 0.0
y 1.0
z 2.0
dtype: float64
If we take the scalar values, then the index must be provided. The scalar value will be repeated for
matching the length of the index.
Output
0 4
1 4
2 4
3 4
dtype: int64
Once you create the Series type object, you can access its indexes, data, and even individual
elements.
The data in the Series can be accessed similar to that in the ndarray.
1. import pandas as pd
2. x = pd.Series([1,2,3],index = ['a','b','c'])
3. #retrieve the first element
4. print (x[0])
Output
1
Series object attributes
The Series attribute is defined as any information related to the Series object such as size, datatype.
etc. Below are some of the attributes that you can use to get the information about the Series object:
Attributes Description
Series.index Defines the index of the Series.
Series.shape It returns a tuple of shape of the data.
Series.dtype It returns the data type of the data.
Series.size It returns the size of the data.
Series.empty It returns True if Series object is empty, otherwise returns false.
Series.hasnans It returns True if there are any NaN values, otherwise returns
false.
Series.nbytes It returns the number of bytes in the data.
Series.ndim It returns the number of dimensions in the data.
Series.itemsize It returns the size of the datatype of item.
We can retrieve the index array and data array of an existing Series object by using the attributes
index and values.
1. import numpy as np
2. import pandas as pd
3. x=pd.Series(data=[2,4,6,8])
4. y=pd.Series(data=[11.2,18.6,22.5], index=['a','b','c'])
5. print(x.index)
6. print(x.values)
7. print(y.index)
8. print(y.values)
Output
You can use attribute dtype with Series object as <objectname> dtype for retrieving the data type of
an individual element of a series object, you can use the itemsize attribute to show the number of
bytes allocated to each data item.
1. import numpy as np
2. import pandas as pd
3. a=pd.Series(data=[1,2,3,4])
4. b=pd.Series(data=[4.9,8.2,5.6],
5. index=['x','y','z'])
6. print(a.dtype)
7. print(a.itemsize)
8. print(b.dtype)
9. print(b.itemsize)
Output
int64
8
float64
8
Retrieving Shape
The shape of the Series object defines total number of elements including missing or empty
values(NaN).
1. import numpy as np
2. import pandas as pd
3. a=pd.Series(data=[1,2,3,4])
4. b=pd.Series(data=[4.9,8.2,5.6],index=['x','y','z'])
5. print(a.shape)
6. print(b.shape)
Output
(4,)
(3,)
1. import numpy as np
2. import pandas as pd
3. a=pd.Series(data=[1,2,3,4])
4. b=pd.Series(data=[4.9,8.2,5.6],
5. index=['x','y','z'])
6. print(a.ndim, b.ndim)
7. print(a.size, b.size)
8. print(a.nbytes, b.nbytes)
Output
11
43
32 24
To check the Series object is empty, you can use the empty attribute. Similarly, to check if a series
object contains some NaN values or not, you can use the hasans attribute.
Example
1. import numpy as np
2. import pandas as pd
3. a=pd.Series(data=[1,2,3,np.NaN])
4. b=pd.Series(data=[4.9,8.2,5.6],index=['x','y','z'])
5. c=pd.Series()
6. print(a.empty,b.empty,c.empty)
7. print(a.hasnans,b.hasnans,c.hasnans)
8. print(len(a),len(b))
9. print(a.count( ),b.count( ))
Output
Series Functions
Functions Description
Pandas Series.map() Map the values from two series that have a common column.
Pandas Series.std() Calculate the standard deviation of the given set of numbers,
DataFrame, column, and rows.
Pandas Series.to_frame() Convert the series object to the dataframe.
Pandas Returns a Series that contain counts of unique values.
Series.value_counts()
Illustrative programs:
Python Code :
import numpy as np
import pandas as pd
np_array = np.array([10, 20, 30, 40, 50])
print("NumPy array:")
print(np_array)
new_series = pd.Series(np_array)
print("Converted Pandas series:")
print(new_series)
Output:
NumPy array:
[10 20 30 40 50]
Converted Pandas series:
0 10
1 20
2 30
3 40
4 50
dtype: int64
Explanation:
np.array([10, 20, 30, 40, 50]): This code creates a NumPy array 'np_array' containing a
sequence of five integers: [10, 20, 30, 40, 50]. new_series = pd.Series(np_array): This line
creates a new Pandas Series object 'new_series' from the NumPy array using the pd.Series()
constructor. The resulting Series object will have the same values as the NumPy array, and
the default index will be assigned to each element starting from 0 and increasing by 1 for each
subsequent element
we will illustrate the process of creating a Pandas DataFrame with the two-dimensional
list. Python is widely recognized for its effectiveness in data analysis, thanks to its robust
ecosystem of data-centric packages. Among these packages, Pandas stands out, streamlining
the import and analysis of data. There are various methods to achieve a Pandas DataFrame,
and in this article, we will focus on creating one using a two-dimensional list.
# import pandas as pd
import pandas as pd
# List1
lst = [['Geek', 25], ['is', 30],
['for', 26], ['Geeksforgeeks', 22]]
# creating df object with columns specified
df = pd.DataFrame(lst, columns =['Tag', 'number'])
print(df )
Output :
Tag number
0 Geek 25
1 is 30
2 for 26
3 Geeksforgeeks 22
import pandas as pd
# Two-dimensional list
data = [['Geek1', 28, 'Analyst'],
['Geek2', 35, 'Manager'],
['Geek3', 29, 'Developer']]
# Column names
columns = ['Name', 'Age', 'Occupation']
Output:
Name Age Occupation
0 Geek1 28 Analyst
1 Geek2 35 Manager
2 Geek3 29 Developer
import pandas as pd
# Two-dimensional list
data = [['Geek1', 26, 'Scientist'],
['Geek2', 31, 'Researcher'],
['Geek3', 24, 'Engineer']]
# Column names
columns = ['Name', 'Age', 'Occupation']
Output:
Name Age Occupation
0 Geek1 26 Scientist
1 Geek2 31 Researcher
2 Geek3 24 Engineer
import pandas as pd
# Two-dimensional list
data = [['Geek1', 'Reacher', 25],
['Geek2', 'Pete', 30],
['Geek3', 'Wilson', 26],
['Geek4', 'Williams', 22]]
# Column names
columns = ['FName', 'LName', 'Age']
# Creating DataFrame with specified data types
df = pd.DataFrame(data, columns=columns, dtype=float)
# Displaying the DataFrame
print(df)
Output :
FName LName Age
0 Geek1 Reacher 25.0
1 Geek2 Pete 30.0
2 Geek3 Wilson 26.0
3 Geek4 Williams 22.0
Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new
heights by tapping into the power of data. Sharpen your skills and become a part of the hottest
trend in the 21st century.
Python Tkinter
Python provides the standard library Tkinter for creating the graphical user interface for
desktop based applications.
Developing desktop based applications with python Tkinter is not a complex task. An empty
Tkinter top-level window can be created by using the following steps.
Example
1. # !/usr/bin/python3
2. from tkinter import *
3. #creating the application main window.
4. top = Tk()
5. #Entering the event main loop
6. top.mainloop()
Output:
Tkinter widgets
There are various widgets like button, canvas, checkbutton, entry, etc. that are used to build
the python GUI applications.
SN Widget Description
1 Button The Button is used to add various kinds of buttons to the python
application.
2 Canvas The canvas widget is used to draw the canvas on the window.
3 Checkbutton The Checkbutton is used to display the CheckButton on the window.
4 Entry The entry widget is used to display the single-line text field to the user.
It is commonly used to accept user values.
5 Frame It can be defined as a container to which, another widget can be added
and organized.
6 Label A label is a text used to display some message or information about the
other widgets.
7 ListBox The ListBox widget is used to display a list of options to the user.
8 Menubutton The Menubutton is used to display the menu items to the user.
9 Menu It is used to add menu items to the user.
10 Message The Message widget is used to display the message-box to the user.
11 Radiobutton The Radiobutton is different from a checkbutton. Here, the user is
provided with various options and the user can select only one option
among them.
12 Scale It is used to provide the slider to the user.
13 Scrollbar It provides the scrollbar to the user so that the user can scroll the
window up and down.
14 Text It is different from Entry because it provides a multi-line text field to
the user so that the user can write the text and edit the text inside it.
14 Toplevel It is used to create a separate window container.
15 Spinbox It is an entry widget used to select from options of values.
16 PanedWindow It is like a container widget that contains horizontal or vertical panes.
17 LabelFrame A LabelFrame is a container widget that acts as the container
18 MessageBox This module is used to display the message-box in the desktop based
applications.
The Tkinter geometry specifies the method by using which, the widgets are represented on
display. The python Tkinter provides the following geometry methods.
The pack() widget is used to organize widget in the block. The positions widgets added to the
python application using the pack() method can be controlled by using the various options
specified in the method call.
However, the controls are less and widgets are generally added in the less organized manner.
syntax
1. widget.pack(options)
A list of possible options that can be passed in pack() is given below.
o expand: If the expand is set to true, the widget expands to fill any space.
o Fill: By default, the fill is set to NONE. However, we can set it to X or Y to determine
whether the widget contains any extra space.
o size: it represents the side of the parent to which the widget is to be placed on the
window.
Example
1. # !/usr/bin/python3
2. from tkinter import *
3. parent = Tk()
4. redbutton = Button(parent, text = "Red", fg = "red")
5. redbutton.pack( side = LEFT)
6. greenbutton = Button(parent, text = "Black", fg = "black")
7. greenbutton.pack( side = RIGHT )
8. bluebutton = Button(parent, text = "Blue", fg = "blue")
9. bluebutton.pack( side = TOP )
10. blackbutton = Button(parent, text = "Green", fg = "red")
11. blackbutton.pack( side = BOTTOM)
12. parent.mainloop()
Output:
The grid() geometry manager organizes the widgets in the tabular form. We can specify the
rows and columns as the options in the method call. We can also specify the column span
(width) or rowspan(height) of a widget.
This is a more organized way to place the widgets to the python application. The syntax to
use the grid() is given below.
Syntax
1. widget.grid(options)
A list of possible options that can be passed inside the grid() method is given below.
o Column
The column number in which the widget is to be placed. The leftmost column is
represented by 0.
o Columnspan
The width of the widget. It represents the number of columns up to which, the column
is expanded.
o ipadx, ipady
o It represents the number of pixels to pad the widget inside the widget's border.
o padx, pady
o It represents the number of pixels to pad the widget outside the widget's border.
o row
The row number in which the widget is to be placed. The topmost row is represented
by 0.
o rowspan
The height of the widget, i.e. the number of the row up to which the widget is
expanded.
o Sticky
If the cell is larger than a widget, then sticky is used to specify the position of the
widget inside the cell. It may be the concatenation of the sticky letters representing the
position of the widget. It may be N, E, W, S, NE, NW, NS, EW, ES.
Example
# !/usr/bin/python3
from tkinter import *
parent = Tk()
name = Label(parent,text = "Name").grid(row = 0, column = 0)
e1 = Entry(parent).grid(row = 0, column = 1)
password = Label(parent,text = "Password").grid(row = 1, column = 0)
e2 = Entry(parent).grid(row = 1, column = 1)
submit = Button(parent, text = "Submit").grid(row = 4, column = 0)
parent.mainloop()
Output:
The place() geometry manager organizes the widgets to the specific x and y coordinates.
Syntax
1. widget.place(options)
A list of possible options is given below.
o Anchor: It represents the exact position of the widget within the container. The
default value (direction) is NW (the upper left corner)
o bordermode: The default value of the border type is INSIDE that refers to ignore the
parent's inside the border. The other option is OUTSIDE.
o height, width: It refers to the height and width in pixels.
o relheight, relwidth: It is represented as the float between 0.0 and 1.0 indicating the
fraction of the parent's height and width.
o relx, rely: It is represented as the float between 0.0 and 1.0 that is the offset in the
horizontal and vertical direction.
o x, y: It refers to the horizontal and vertical offset in the pixels.
Example
# !/usr/bin/python3
from tkinter import *
top = Tk()
top.geometry("400x250")
name = Label(top, text = "Name").place(x = 30,y = 50)
email = Label(top, text = "Email").place(x = 30, y = 90)
password = Label(top, text = "Password").place(x = 30, y = 130)
e1 = Entry(top).place(x = 80, y = 50)
e2 = Entry(top).place(x = 80, y = 90)
e3 = Entry(top).place(x = 95, y = 130)
top.mainloop()
Output:
Create a Date Picker Calendar in Tkinter
Tkinter is a popular Python library for creating and developing applications. It has various
methods and functions that can be used for adding multiple features in an application.
Tkcalendar is one of the tkinter packages that can be used to create GUI-based calendars in
the window and thus, we can perform multiple operations like selecting the data, picking and
scheduling the event through the calendar application and many more.
However, in this article, we will see how we can create a Date Picker calendar using the
Tkcalendar package. Before that, we have to install the package in our local environment
using pip install tkcalendar.
Once installed, we will create an instance of tkcalendar and create a button to get the date.