使用tkinter逐行显示txt文件

时间:2018-03-23 15:25:14

标签: tkinter python-3.6 tweepy

您好我正在编写一个程序,使用tweepys流类从Twitter中提取推文。我试图在使用tkinter的GUI中显示它,我在标签中显示多于一条推文时遇到问题。我是编程的新手,所以任何帮助将不胜感激。感谢。

import json
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
import time
from tkinter import*
consumer_key = ''
consumer_secret = ''
access_token = ''
access_secret = ''

class listener(StreamListener):
    def __init__(self,time_limit=30):
        self.start_time = time.time()
        self.limit = time_limit
        self.saveFile = open('TwitterStream.json','a')
        self.newFile = open('FilteredStream.txt','a')
        super(StreamListener, self).__init__()


    def on_data(self, data):
        if (time.time() - self.start_time) < self.limit:
            filtered = json.loads(data)
            print (filtered['user']['screen_name'], filtered['text'].encode('ascii', 'ignore'),('\n'))
            filteredFile =(filtered['user']['screen_name'], filtered['text'].encode('ascii', 'ignore'),('\n'))

            root = Tk()
            theLabel  = Label(root, text = (filtered['user']['screen_name'], filtered['text'].encode('ascii', 'ignore'),('\n')))
            theLabel.pack()
            frame = Frame(root, width = 1000, height= 50)
            frame.pack()
            def close_window():
                root.destroy()
            frame = Frame(root, width = 400, height = 400)
            frame.pack()
            button = Button(frame, text="next", command=close_window)
            button.pack()
            root.mainloop()

            self.saveFile.write(data)
            self.saveFile.write('\n')
            self.newFile.write(str(filteredFile))
            self.newFile.write('\n')
            return True
        else:
            self.saveFile.close()
            self.newFile.close()
            return False
    def on_error(self, status):
        print(status)

auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
twitterStream = Stream(auth, listener())
twitterStream.filter(languages=["en"], track=["Russia"])

0 个答案:

没有答案