8

I am trying to show my twitter timeline on Tkinter window using tweepy. This is the code

import tweepy
import tkinter

consumer_key = 'xxxxxxxxxxxxxx'
consumer_sec ='xxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
acc_token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
acc_token_sec = 'xxxxxxxxxxxxxxxxxxxxxx'
auth = tweepy.OAuthHandler(consumer_key,consumer_sec)
auth.set_access_token(acc_token,acc_token_sec)

api = tweepy.API(auth)

tweets = api.home_timeline()

tkwindow = tkinter.Tk()

for tweet in tweets:
    i = 1
    label = tkinter.Label(tkwindow, text=tweet.author.name + " " + str(tweet.created_at) + "\n" + str(tweet.text))
    if i == 5:
        break
tkwindow.mainloop()

But I am having following error

_tkinter.TclError: character U+1f449 is above the range (U+0000-U+FFFF) allowed by Tcl

I understand that tkinter can't show some special icons which appear in real tweets, But actually, I don't want to show those, I just want to show simple text of the tweet,

So How I can avoid this error and show only text of the tweets

beginner
  • 2,366
  • 4
  • 29
  • 53

1 Answers1

6

The easiest way to do this would be to strip out the extra characters. This could be done with the following code at the start of the for loop:

char_list = [tweet[j] for j in range(len(tweet)) if ord(tweet[j]) in range(65536)]
tweet=''
for j in char_list:
    tweet=tweet+j
  • Hi, Thanks for your answer, But it is not working, I am new to python, So maybe I used it the wrong way. could you show, where should I write these lines in my code? – beginner Sep 12 '17 at 10:18
  • Sorry, I forgot to add the range() function. It should work now. – Temsia Carrolla Sep 12 '17 at 10:22