Search code examples
python-3.xuser-interfacetkintersentiment-analysistext-widget

How to get multiple text entries from GUI and use those in a main python script?


I have a python file that extracts tweets, get their geo-coordinates and sentiment and finally plots those tweets/sentiment as colored circles on a map.

The following inputs (text entries) are needed to get it to work: An example user-input is also shown next to each input prompt:

 Enter the maximum number of tweets: *100*

 Do you want to search by topic? type: y or n: *y*

 Enter topic: *MoSalah*

 Enter visulaization/projection type:  
 1. Mercator 
 2. orthographic 
 3. melloweide 
 >>  *Mercator* 

 Zoom in to a conteninent of choice:  
 1. world 
 2. africa 
 3. asia 
 4. north america 
 5. south america 
 6. europe 
 7. usa 
 >>  *world*

 Enter symbol shape: 
 1. square 
 2. circle 
 >> *circle*

Now in order to make the user experience more exciting, I want to build a simple GUI that asks the user for all those inputs and store them into their respective variables, but I don't know how to create one and more importantly link the GUI to the python code running behind it.

Do I have to have separate retrieval function for each one of the required inputs shown above? For example, is this how the retrieval function for max no. of tweets should look like using a tkinter GUI :

from tkinter import * 
root = Tk()

root.geometry('200x100')

# Retrieve to get input form user and store it in a variable

# Retrieve maximum number of tweets

def retrieveMaxTweets():
    maxTweets = textBox.get()

    return maxTweets 

textBox = Text(root, height = 2, width = 10)
textBox.pack()

buttonComment = Button(root, height=1, width=10, text='Enter max no. of tweets', command = lambda: retrieveMaxTweets())

buttonComment.pack()

mainloop()

And then in the part of the code where I initially asked for the limit, I do this:

limit = retrieveMaxTweets()

instead of this:

limit = int(input(" Enter the maximum number of tweets: "))

Solution

  • You could store the results of the different GUI 'questions' in to a dictionary ready for the other parts of the code to use. That way you would only have one function that 'collects/validates/stores' the responses.

    For example

    import tkinter as tk
    
    class App(tk.Frame):
        def __init__(self,master=None,**kw):
            #Create a blank dictionary
            self.answers = {}
            tk.Frame.__init__(self,master=master,**kw)
    
            tk.Label(self,text="Maximum number of Tweets").grid(row=0,column=0)
            self.question1 = tk.Entry(self)
            self.question1.grid(row=0,column=1)
    
            tk.Label(self,text="Topic").grid(row=1,column=0)
            self.question2 = tk.Entry(self)
            self.question2.grid(row=1,column=1)
    
            tk.Button(self,text="Go",command = self.collectAnswers).grid(row=2,column=1)
    
    
        def collectAnswers(self):
            self.answers['MaxTweets'] = self.question1.get()
            self.answers['Topic'] = self.question2.get()
            functionThatUsesAnswers(self.answers)
    
    def functionThatUsesAnswers(answers):
        print("Maximum Number of Tweets ", answers['MaxTweets'])
        print("Topic ", answers['Topic'])
    
    
    if __name__ == '__main__':
        root = tk.Tk()
        App(root).grid()
        root.mainloop()
    

    When the button is pressed, each of the 'answers' are added to a dictionary which is then passed to the function that does the main part of your code.