Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Create a GUI for Weather Forecast using openweathermap API in Python
Next article icon

Prerequisites :Introduction to tkinter |Sentiment Analysis using Vader


Python offers multiple options for developing a GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. Python with Tkinter outputs the fastest and easiest way to create GUI applications. In this article, we will learn how to create a Sentiment Detector GUI application using Tkinter, with a step-by-step guide.
 

To create a tkinter :

  • Importing the module – tkinter
  • Create the main window (container)
  • Add any number of widgets to the main window.
  • Apply the event Trigger on the widgets.

The GUI would look like below:


Let’s create a GUI based Sentiment Detector application that can show the sentiments with respect to the given sentences, given by the user.
 

Below is the implementation : 

Python3
# import SentimentIntensityAnalyzer class# from vaderSentiment.vaderSentiment module.fromvaderSentiment.vaderSentimentimportSentimentIntensityAnalyzer# import all methods and classes from the tkinterfromtkinterimport*# Function for clearing the# contents of all entry boxes# And text area.defclearAll():# deleting the content from the entry boxnegativeField.delete(0,END)neutralField.delete(0,END)positiveField.delete(0,END)overallField.delete(0,END)# whole content of text area  is deletedtextArea.delete(1.0,END)# function to print sentiments# of the sentence.defdetect_sentiment():# get a whole input content from text boxsentence=textArea.get("1.0","end")# Create a SentimentIntensityAnalyzer object.sid_obj=SentimentIntensityAnalyzer()# polarity_scores method of SentimentIntensityAnalyzer# object gives a sentiment dictionary.# which contains pos, neg, neu, and compound scores.sentiment_dict=sid_obj.polarity_scores(sentence)string=str(sentiment_dict['neg']*100)+"% Negative"negativeField.insert(10,string)string=str(sentiment_dict['neu']*100)+"% Neutral"neutralField.insert(10,string)string=str(sentiment_dict['pos']*100)+"% Positive"positiveField.insert(10,string)# decide sentiment as positive, negative and neutralifsentiment_dict['compound']>=0.05:string="Positive"elifsentiment_dict['compound']<=-0.05:string="Negative"else:string="Neutral"overallField.insert(10,string)# Driver Codeif__name__=="__main__":# Create a GUI windowgui=Tk()# Set the background colour of GUI windowgui.config(background="light green")# set the name of tkinter GUI windowgui.title("Sentiment Detector")# Set the configuration of GUI windowgui.geometry("250x400")# create a label : Enter Your TaskenterText=Label(gui,text="Enter Your Sentence",bg="light green")# create a text area for the root# with lunida 13 font# text area is for writing the contenttextArea=Text(gui,height=5,width=25,font="lucida 13")# create a Submit Button and place into the root window# when user press the button, the command or# function affiliated to that button is executedcheck=Button(gui,text="Check Sentiment",fg="Black",bg="Red",command=detect_sentiment)# Create a negative : labelnegative=Label(gui,text="sentence was rated as: ",bg="light green")# Create a neutral : labelneutral=Label(gui,text="sentence was rated as: ",bg="light green")# Create a positive : labelpositive=Label(gui,text="sentence was rated as: ",bg="light green")# Create a overall : labeloverall=Label(gui,text="Sentence Overall Rated As: ",bg="light green")# create a text entry boxnegativeField=Entry(gui)# create a text entry boxneutralField=Entry(gui)# create a text entry boxpositiveField=Entry(gui)# create a text entry boxoverallField=Entry(gui)# create a Clear Button and place into the root window# when user press the button, the command or# function affiliated to that button is executed .clear=Button(gui,text="Clear",fg="Black",bg="Red",command=clearAll)# create a Exit Button and place into the root window# when user press the button, the command or# function affiliated to that button is executed .Exit=Button(gui,text="Exit",fg="Black",bg="Red",command=exit)# grid method is used for placing# the widgets at respective positions# in table like structure.enterText.grid(row=0,column=2)textArea.grid(row=1,column=2,padx=10,sticky=W)check.grid(row=2,column=2)negative.grid(row=3,column=2)neutral.grid(row=5,column=2)positive.grid(row=7,column=2)overall.grid(row=9,column=2)negativeField.grid(row=4,column=2)neutralField.grid(row=6,column=2)positiveField.grid(row=8,column=2)overallField.grid(row=10,column=2)clear.grid(row=11,column=2)Exit.grid(row=12,column=2)# start the GUIgui.mainloop()

Output : 


Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp