Sentiment Detector GUI using Tkinter in Python

Python provides a variety of choices for GUI (Graphical User Interface) development. Tkinter is the approach used the most frequently among all GUI approaches. The fastest and simplest approach to construct GUI apps is with Python and Tkinter. This article will provide a step-by-step tutorial on how to develop a Sentiment Detector GUI application using Tkinter. The basic idea behind the creation of this application is to detect whether a given sentence is positive, negative or neutral in nature. It is a very simple application that can also be of great use in various ways like to detect a person's mood by detecting the nature of the sentence that particular person is using to communicate. The process of how can such an application be programmed using Python will be discussed ahead in the article.

Creating a tkinter :

  • tkinter's module will be imported
  • Making the primary window (container)
  • Add as many widgets as we like to the main window.
  • Putting the widgets under the event trigger.

Now let us build a GUI-based sentiment detector programme that can display sentiments in relation to user-provided phrases.

Walking through the code :

Firstly, we will import all the required modules into our program, that is vaderSentiment and tkinter, from the standard Python library.

Then, we will create a clrAll() method which will clear out all the text, present in the ngtvField, neutrlField, postvField and ovrallField entry fields. And even the textArea will also be cleared under this method along with the use of the delete() method.

Then, we will create another method dtct_sentiment() in order to detect the sentiments of the entered sentence in the text field. Firstly, we will create a textField for taking in the data. Then, we will use the polarity_scores() method from the SentimentIntensityAnalyzer module for getting a dictionary of the sentiments present. The dtct_sentiment() method, will further helps us to be able to get the percentage of sentence that is negative, positive and neutral. Ultimately, on the basis of these three parameters, each parameter will be presented with some individual score Then, the percentage value of all these sentiments will display the overall sentiment of the sentence using the if-else loops.

Then, we will jump on to the main part of our code. Firstly, in the main code, we will create a new GUI. Then, we will set the background colour of the GUI window to "light green" using the config() method. Then, we will give a suitable title to our GUI window using the title() method. And lastly, we will give required dimensions to our window using the geometry() method.

Next, we will start creating the various entry boxes and labels to enter the text. First, we will construct a label asking to enter your text and along with that we will then create an entry box to enter the main sentence that will be analysed to detect the sentiments. Then, we will create a check button to confirm the sentence and send it ahead for detection. Then, we will create a label for the detection of the sentence as negative along with an entry box for the same to show the analysed result of that particular field. Then, we will create a label for the detection of the sentence as neutral along with an entry box for the same to show the analysed result of that particular field in the entry box. Then, we will create a label for the detection of the sentence as positive along with an entry box for the same to show the analysed result of that particular field in the entry box. Then, we will create a label for the detection of the sentence as overall along with an entry box for the same to show the final result of the sentence entered in the entry box.

Then, we will create a few buttons for our GUI. Firstly, we will create a clear button which will help us to clear up all the present content in the entry boxes. Then, we will create an exit button, which when clicked will be responsible for exiting the GUI. All the text areas i.e. the entry boxes, labels and buttons will all then be placed at their required positions using the grid() method.

Ultimately, we will use the mainloop() method in order to execute the entire program that we created.

Complete Code :

Output :

Sentiment Detector GUI using Tkinter in Python




Latest Courses