Search code examples
pythonapiuser-interfacetwitter

Python GUI for twitter API


import tweepy
import csv
import json
import datetime
import pandas as pd
from Tkinter import *



def twitter(e1):
 consumer_key = 'BPJYMwo4Z6ueJ'
 consumer_secret = 'zB0SfmOtWWAm4T1N6akLD0ut0TKB2Hc6Yxmry'
 access_token = '380414482-Z1ZZgJz4V8Y5epzDgrehB'
 access_token_secret = 'x1QINIBHeW9DJc7vG1SqI9p3d13H'
 auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
 auth.set_access_token(access_token, access_token_secret)
 api = tweepy.API(auth, wait_on_rate_limit=True)
 #####United Airlines
 # Open/Create a file to append data
 csvFile = open('Twitter' + (datetime.datetime.today().strftime('%d%m%Y%H%M')) + '.json', 'a')
 # Use csv Writer
 # csvWriter = csv.writer(csvFile)
 print("fetching started........")
 for tweet in tweepy.Cursor(api.search, q=e1, count=1, lang="en", since="2018-07-26").items():
    csvFile.write(json.dumps(tweet._json))
    csvFile.write("\n")
    print("fetching end........")
    csvFile.close()

if __name__ == '__main__':
        master = Tk()
        Label(master, text="Hashtag").grid(row=0)
        e1 = Entry(master)
        e1.grid(row=0, column=1)
        Button(master, text='Quit', command=lambda: twitter(e1)).grid(row=3, column=0, sticky=W, pady=4)
        master.mainloop()

The code runs but when i add text in gui the fetching starts and get stuck. I tried everything i could but no luck. Iunderstand the problem is with the twitter(e1) but i dont have any clue how to solve it.


Solution

  • You need to extract the text from the Entry, instead of passing the entire object:

    command=lambda: twitter(e1.get().strip())