Now Reading
Complete Tutorial on Tkinter To Deploy Machine Learning Model

Complete Tutorial on Tkinter To Deploy Machine Learning Model

Rohit Dwivedi

In machine learning, while building a predictive model we follow several different steps. We first do exploratory data analysis to understand the data well and do the required preprocessing. After the data gets ready we do modelling and develop a predictive model. This model is then used to compute prediction on the testing data and the results are evaluated using different error metrics. But what to do next? Do you know how you can use this model and check real-time predictions? It is said you can validate the model performance when you compute prediction in real-time. As we have already seen how we can do model deployment using flask.

In this article, we will be exploring Tkinter – python GUI programming tool. We will explore how we can deploy a machine learning model and check real-time predictions using Tkinter. For this experiment, we will be using the Pima Indians Diabetes Data set that is available on Kaggle. We will first build a classification model that will classify whether a patient is diabetic or not. Then we will make a GUI using Tkinter and will check predictions on new data points. 

What you will learn from this? 

  1. What is Tkinter? How is it used to make GUI? 
  2. How to build machine learning models? 
  3. How to Compute Predictions using the Tkinter GUI in real-time?
  1. What is Tkinter? How is it used to make GUI? 

Tkinter is a library written in Python that is widely used to create GUI applications. It is very easy to build GUI using Tkinter and the process is even faster. Tkinter has several widgets that can be used while developing GUI. These include buttons, radio buttons, checkboxes, etc. We will see how we can make a GUI Tkinter after we build the machine learning model later in the article. 

  1. How to Build Machine Learning Models? 

Now we will build the classification model for classifying the patient as diabetic or not. We will quickly import all the libraries that are required and the data set. Use the below code for the same. 

import pandas as pd

df = pd.read_csv('pima.csv')


Complete Tutorial on Tkinter To Deploy Machine Learning Model

There are a total of 768 rows and 9 columns in the data set. There were no missing values found in the data set. Now we will create independent and dependent variables. After creating these we will split the data into training and testing sets. We will fit the training data over the model and will compute prediction over the testing data. Refer to the below code for the same. 

X = df.drop('class', axis = 1)
y = df[['class']]
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)

There are a total of 514 rows in the training and 254 are present in the testing set. We will now build the classification model. We are using Logistic regression for the same.  Once we have built the model we will feed the training data and will compute predictions for testing data. Use the below code for the same. 

from sklearn.linear_model import LogisticRegression
lr = LogisticRegression()
model =,y_train)
y_pred = lr.predict(X_test)

We have stored the prediction on the testing data in y_pred variable. Let us check the predictions. 


Complete Tutorial on Tkinter To Deploy Machine Learning Model

Now will evaluate the predictions using metrics accuracy score. Check the below code for the same. 


We will now pickle this model that would be used to compute predictions for new data points. There are several other methods also available to pickle the model like Joblib. Refer to the below code for pickling the model. 

import pickle    

Model = pickle.dumps(model)  

  1. How to Compute Predictions Using the Tkinter GUI in Real-Time?

Since we are now done with pickling the file. Now we will create a GUI using Tkinter that will be used to capture new data points. Use the below code for the same. We will first define the library and then will make the GUI. 

import tkinter as tk

from tkinter import ttk

win = tk.Tk()

win.title('Diabetes Predictions') 

See Also
Deep Learning

We have first created a tkinter window and given the title as “Diabetic Predictions”. Now we will create the labels (features). Use the below code for the same. 

#Column 1 
#Column 2
#Column 3
#Column 4
#Column 5
#Column 6
#Column 7
#Column 8

We have now created all the buttons that are mainly the features that will store the new data point values. When we have to compute prediction using any model we need a data frame on which we have to make the prediction. Once a user enters a different set of values we then have to create a data frame of it. Use the below code for the same. 

import pandas as pd
DF = pd.DataFrame()
def action():
    global DB
    import pandas as pd
    DF = pd.DataFrame(columns=['Preg','Plas','Pres','skin','test','mass','pedi','age'])

Now we will create a submit button. Once this button is clicked the above action of data frame creation is done. We will once again convert every value that is inputted by the user to numerics. Use the below code for the same. 

def Output():
    DB["Preg"] = pd.to_numeric(DB["Preg"])
    DB["Plas"] = pd.to_numeric(DB["Plas"])
    DB["Pres"] = pd.to_numeric(DB["Pres"])
    DB["skin"] = pd.to_numeric(DB["skin"])
    DB["test"] = pd.to_numeric(DB["test"])
    DB["mass"] = pd.to_numeric(DB["mass"])
    DB["pedi"] = pd.to_numeric(DB["pedi"])
    DB["age"] = pd.to_numeric(DB["age"])

Now once we are done with this we will make use of the pickle file and compute prediction over this data frame. If the prediction comes out to be 1 then it will revert “Diabetic” and if it’s 0 then it will revert “Non-Diabetic”. Use the below code for the same. 

    if output==1:
    elif output==0:

Now we will create a box that will display the output. (Diabetic/ Non-Diabetic). Once the predict button is clicked the model will predict the class and this prediction will be displayed in this box. 


Now we are ready to execute the GUI. Try running the entire code in one cell to get rid of errors. Once you run the cell a new window will open that will show you the GUI. Now we will enter the random values and check the prediction. 

Complete Tutorial on Tkinter To Deploy Machine Learning Model
Complete Tutorial on Tkinter To Deploy Machine Learning Model

We have entered the values for the features now we will click on submit to create the data frame and right after that we will click on the Predict button to check the prediction. 

Complete Tutorial on Tkinter To Deploy Machine Learning Model
Complete Tutorial on Tkinter To Deploy Machine Learning Model


In this article, we discussed how to make a GUI using Tkinter. We explored by first building a classification model over Pima Diabetic Data then and pickling the model weights. We then designed a GUI and then computed prediction for randomly chosen data. The model that was built only gave 75% accuracy. We did not make any efforts to improve the accuracy since we wanted to learn more about predictions in real-time whereas the approach is to finalize the best performing model and pickling it. 

If you want to know more about how you can deploy a machine learning model using Flask, check this article title as “Hands-On-Guide To Machine Learning Model Deployment Using Flask”. 

What Do You Think?

If you loved this story, do join our Telegram Community.

Also, you can write for us and be one of the 500+ experts who have contributed stories at AIM. Share your nominations here.

Copyright Analytics India Magazine Pvt Ltd

Scroll To Top