Let’s Play with Numpy with the help of Tkinter (GUI)

In this blog, we will learn about the NumPy module using Python( Tkinter module).

Let us first understand what NumPy Module is?

NumPy word rises from the word “Numeric Python” or “Numerical Python”.NumPy is a library for the Python programming language, adding support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays.NumPy is available in open-source, so you can install and use it in your Python Project.

(For more information on NumPy Module, kindly refer to this documentation)

Now let us start a project using Python GUI for NumPy Module:

Code for the project:

For the code of this project click on the link below:

https://github.com/Platforuma/medium_blog/blob/178ccb2a29402f2f1840b2e367c57d8504d648c6/tkinter_numpy.py

Now let us deep-dive into a better understanding of the code line by line.

Define code working:-

from Tkinter import*

import NumPy as np

Here we are using some Modules and importing in our Project for use in our project and Build Python GUI Project of the Numpy Module.

So firstly we import the Tkinter Module. Tkinter is the name of the GUI library in Python. Creating a GUI application using Tkinter is an easy task, so we used it in our project Tkinter Module.

Import * means everything from the library. Import * is allowing a Python file to access the script from another Python file.

(For more information on Tkinter Module, kindly refer to this documentation)

Here in this line of the code we import the NumPy Module and give the object name is np. NumPy is an open-source library for Python Programming. NumPy used for working with arrays.

(For more information on NumPy Module, kindly refer to this documentation)

root = Tk()

root.title(“Numpy Module”)

root.geometry(“700x400”)

Tk class is used to create a root window. The root is the name of a root window. Here Tk function provides a window of GUI as well as provides so many features like setting the title, set the geometry of the GUI window.

(For more information on Tk Method, kindly refer to this documentation)

In these steps, we are giving the title of the GUI root window. The title is defined about the project in one line what we do in the project.

(For more information on Title Method, kindly refer to this documentation)

Tkinter provides many methods, one of them is the geometry() method. This method is used to set the dimensions of the Tkinter window and it is used to set the position of the main window on the user desktop.

(For more information on Geometry, kindly refer to this documentation)

def find():

global sc1 , sc2

sc1.set(“”)

sc2.set(“”)

if entry.get():

if entry_1.get():

if entry_3.get():

r = int(entry.get())

c = int(entry_1.get())

n = (entry_3.get().split())

matrix = np.array(n).reshape(r, c)

a = matrix.size

b = matrix.shape

sc1.set(a)

sc2.set(b)

Now come to the next line of the code here in this line we define the Function with the help of the def keyword and the function name is found. In our previous Python GUI Project, we better understand how the function works.

(For more information on Function Method, kindly refer to this documentation)

Here we define global variables in the program sc1 or sc2. Here sc1 or sc2 is the global variable. In a previous project, we understand how global variables work.

(For more information on Global variable, kindly refer to this documentation)

Here in this line of the code, we define the sc1 variable value is null with the help of the set function. Set Function is allowed to set the new value of any object and variable where we apply the set function.

Now again in this line of the code, we define the sc2 variable value is null with the help of the set function. Set Function is allowed to set the new value of any object and variable where we apply the set function.

(For more information on Set Function, kindly refer to this documentation)

In this line, we are defining the if condition when getting the entry object value, and then apply the condition. In programming, if is the conditional statement where we are providing some condition in our project. We already worked on the if condition in our previous project.

(For more information on Conditional Statement, kindly refer to this documentation)

Now again in this line, we are defining the if condition when getting the entry_1 object value, and then apply the condition. In programming, if is the conditional statement where we are providing some condition in our project and go to the next line of the code.

Now again in this line, we are defining the if condition when getting the entry_3 object value, and then apply the condition. In programming, if is the conditional statement where we are providing some condition in our project.

Here in this line of the code, we define the r variable for holding the value of the entry object. Here r we denote the row so we use the only r for the row input.

(For more information on Variable Methods, kindly refer to this documentation)

Here in this line of the code, we define the c variable for holding the value of the entry_1 object. Here c us denote the column so we use the only c for the column input.

(For more information on Array Methods, kindly refer to this documentation)

Here in this line of the code, we define the n variable for holding the value of the entry_3 object. Here we apply a split function on the entry_3 object value and store it in the n variable.

Here we again define the matrix variable. The matrix variable holds the reshape value that can apply to the row and column value and the reshape function used from the NumPy Module.

(For more information on Reshape Methods, kindly refer to this documentation)

Now again we define the b variable. Here the b variable stores the value of the size of the matrix.

In this line of the code, we define the set function on the sc1 object and the value taken from the matrix variable.

Again In this line of the code, we define the set function on the sc2 object and the value taken from the b variable.

sc1=StringVar(‘’)

sc2=StringVar(‘’)

label = Label(root,text=”Enter the number of row”,font=(‘arial’,20),fg=”green”)

label.place(x=40,y=70)

Here we define the variable sc1. We used the sc1 Variable type is StringVar. The StringVar holds the string value.

In this line of the code, we define the variable sc2. We used the sc2 Variable type is StringVar. The StringVar holds the string value.

(For more information on StringVar, kindly refer to this documentation)

Now come to Designing part of the GUI project. Here we are using some Label, Entry Box and Button in a project. So let us start to understand the working of Label, EntryBox and Button in a project accordingly to the project code line by line.

Here in this line define the label object of the Label Method on the GUI window and write text on the label object and set the font and set FG and bg of the label object.

(For more information on Label Method, kindly refer to this documentation)

Now in this line, we are working on the place method. In the code, we assign the place of the label object using x or y coordinates values.

(For more information on Place Method, kindly refer this documentation)

entry = Entry(root,justify=CENTER)

entry.place(x=350, y=80)

label_1 = Label(root,text=”Enter the number of col”,font=(‘arial’,20),fg=”green”)

label_1.place(x=40,y=130)

entry_1 = Entry(root, justify=CENTER)

entry_1.place(x=350, y=142)

label_3 = Label(root,text=”Enter the entries in a single line (separated by space):”,font=(‘arial’,20),fg=”green”)

label_3.place(x=40,y=180)

entry_3 = Entry(root,justify=CENTER,width=40)

entry_3.place(x=400, y=220)

Now about the Entry Box, we already work on our previous GUI project where Entry Boxes are used.

In this line of code, we define the entry object of the Entry Method on the GUI window and set the justify of the entry object.

Assign the place of the entry object using x or y coordinates values.

(For more information on Entry Box, kindly refer to this documentation)

Here in this line define the label_1 object of the Label Method on the GUI window and write text on the label object and set the font and set FG of the label object.

Now in this line, we again work on the place method. In the code, we assign the place of the label_1 object using x or y coordinates values.

In this line of code, we define the entry_1 object of the Entry Method on the GUI window and set the justify of the entry object.

Assign the place of the entry_1 object using x or y coordinates values.

Here in this line define the label_3 object of the Label Method on the GUI window and write text on the label object and set the font and set FG of the label object.

Now in this line, we again work on the place method. In the code, we assign the place of the label_3 object using x or y coordinates values.

In this line of code, we define the entry_3 object of the Entry Method on the GUI window and set the justify and width of the entry object.

Assign the place of the entry_3 object using x or y coordinates values.

label_2 = Label(root,text=”The size of matrix is :”,font=(‘arial’,20),fg=”green”)

label_2.place(x=30,y=250)

entry_2 = Entry(root,justify=CENTER,textvariable=sc1)

entry_2.place(x=330, y=260,height=25)

label_4 = Label(root,text=”The shape of matrix in row and col :”,font=(‘arial’,20),fg=”green”)

label_4.place(x=30,y=300)

entry_4 = Entry(root,justify=CENTER,textvariable=sc2)

entry_4.place(x=470, y=310,height=25)

button = Button(root,width=10, justify=CENTER ,text=”click”,command=find)

button.place(x=250, y=360)

root.mainloop()

Here in this line define the label_2 object of the Label Method on the GUI window and write text on the label object and set the font and set FG of label object.

In the code, we assign the place of the label_2 object using x or y coordinates values.

In this line of code, we define the entry_2 object of the Entry Method on the GUI window and set the justify and text variable of the entry object.

Assign the place of the entry_2 object using x or y coordinates values.

Here in this line define the label_4 object of the Label Method on the GUI window and write text on the label object and set the font and set FG of the label object.

In the code, we assign the place of the label_4 object using x or y coordinates values.

In this line of code, we define the entry_4 object of the Entry Method on the GUI window and set the justify and text variable of the entry object.

Assign the place of the entry_4 object using x or y coordinates values.

Now come to the main part of the project is Button because the project shows results when we press the button means all functionality is added on the button.

In the previous Python GUI Project, we understand how buttons work.

In this line of code, we define the button object of the Button Method on the GUI window and set text and width and also give the gen command on the button object for adding the functionality on the function when the button is pressed.

Assign the place of the button object using x or y coordinates values.

(For more information on Button Method, kindly refer to this documentation)

Now come to the last line of code of the project is the main loop function that provides an infinite loop. So let’s start to understand how it works.

mainloop () is an infinite loop used to run the application, using this function window is not closed as long as.

(For more information on the main loop Function, kindly refer to this documentation)

Here we complete a full GUI Project using the Python Tkinter module and NumPy module.

Refer to our more Blogs on Python from the link below:

https://medium.com/@platforuma

Author: Priyanka Prasad

Editor: Riya Patidar, Devendra Patidar, Ashutosh Raghuwanshi

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store