mercredi 29 octobre 2014

twitter api and tkinter - python 2.7.8


Vote count:

0




I am streaming tweets in a GUI program. Each tweet is appended to a TEXT widget of Tkinter. The code is as follows :



from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
import time
import json
from threading import Thread
from Tkinter import *


ckey='xxx'
csecret='xxx'
atoken='xxx'
asecret='xxx'

tweetQ=[]

tweet_no=0
stream_created=0

def processT(tweet):
global tweet_no
global tweet_box
tweet_no+=1
if 'text' in tweet:
text = tweet['text'].encode('ascii', 'ignore')
date = tweet['created_at']
tweet_box.insert(END,str(tweet_no)+"\t"+str(date)+"\t"+str(text)+"\n")
tweet_box.update_idletasks()
tweet_box.yview(END)

class listener(StreamListener):
def on_data(self,data):
try:
tweet=json.loads(data)
t=Thread(target=processT,args=(tweet,))
t.start()
t.join()
return True

except BaseException ,e:
print "Failed on_data, ",str(e)

def on_error(self,status):
print status
time.sleep(3)

def actFromTo(i):
if i==0:
option_real.select()
option_time.deselect()
else:
option_real.deselect()
option_time.select()



def main():
global stream_created
try:
auth = OAuthHandler(ckey,csecret)
auth.set_access_token(atoken,asecret)
if stream_created==0:
stream_created=1
twitterStream=Stream(auth,listener())
twitterStream.filter(locations=[-122.75,36.8,-121.75,37.8,-74,40,-73,41])
else:
delete(twitterStream)


except BaseException ,e:
stream_created=0
print "Failed main, ",str(e)



root= Tk()
root.title("Twitter Monitor")

left_f = Frame(root)
right_f = Frame(root)


v = StringVar()
v.set(["selection"])
option_real = Radiobutton(left_f,variable=v,text="Realtime",value=1,command=lambda:actFromTo(0))
option_real.grid(row=0,column=0,sticky=(W))
option_real.select()
option_time=Radiobutton(left_f,variable=v,text="Time period",value=2,command=lambda:actFromTo(1))
option_time.grid(row=1,column=0,sticky=(W))
frm = Label(left_f,text="FROM > ")
frm.grid(row=2,column=1,sticky=(E))
frm_f = Entry(left_f,width=10,state=DISABLED)
frm_f.grid(row=2,column=2,sticky=(E))

to = Label(left_f,text="TO > ")
to.grid(row=2,column=3,sticky=(E))
to_f = Entry(left_f,width=10,state=DISABLED)
to_f.grid(row=2,column=4,sticky=(E))


tweet_box = Text(right_f,width=80)

scrollbar = Scrollbar(right_f, orient="vertical")
scrollbar.config(command=tweet_box.yview)

tweet_box.config(yscrollcommand=scrollbar.set)
scrollbar.pack(side='right')
tweet_box.pack(side='right')

launch = Button(left_f,text="<< START >>",command=main)
launch.grid(row=3,column=0,sticky=(E))


left_f.pack(side=LEFT,padx=20,pady=20)
right_f.pack(side=RIGHT,padx=20,pady=20)
root.mainloop()


I have a start button which initiates the process of streaming. The program is working fine but as soon as I touch the GUI window or press start button again or use the scrollbar , the program goes to NOT RESPONDING state.


I am using Windows 7 32 bit and Python 2.7.8. What can be the possible reasons?. I even tried creating a separate thread for executing the process but problem persists...





asked 2 mins ago







twitter api and tkinter - python 2.7.8

Aucun commentaire:

Enregistrer un commentaire