使用Pyqt和Twitters API

时间:2017-04-16 21:05:31

标签: python python-3.x user-interface twitter pyqt

我正在尝试为我的Twitter应用程序构建一个自动跟踪人员的GUI。我有它主要在控制台工作,但我试图将其转换为GUI,并遇到一些问题。主要问题是每当我按下按钮时我想要运行的脚本时,它会在我运行应用程序时立即运行,而不会显示GUI;或GUI显示,但按钮上的功能不执行任何操作。我一直在寻找好几天,但我不知道问题是什么。

#! /usr/bin/python
import time
import tweepy
from keys import keys
import requests_cache
from random import randint
from PyQt4 import QtGui
from PyQt4 import QtCore
from subprocess import call
import sys
from AddToDB import *



SCREEN_NAME = 'JonahHensley'
CONSUMER_KEY = keys['consumer_key']
CONSUMER_SECRET = keys['consumer_secret']
ACCESS_TOKEN = keys['access_token']
ACCESS_TOKEN_SECRET = keys['access_token_secret']

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
api = tweepy.API(auth)

followers = api.followers_ids(SCREEN_NAME)

class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()
        self.initUI()

    def initUI(self):

        btn = QtGui.QPushButton('Yes', self)
        btn.clicked.connect(follower.follow(self))

        btn.resize(180, 40)
        btn.move(20, 35)

        qbtn = QtGui.QPushButton('No', self)
        qbtn.clicked.connect(QtCore.QCoreApplication.instance().quit)
        qbtn.resize(180, 40)
        qbtn.move(20, 80)

        self.setWindowTitle('Test')
        self.show()



class follower(QtGui.QWidget):
    def follow(self):
        while True:
            try:
              for f in followers:
                  UserFollow = input("Follow users from:  ")
                  print('Getting followers from ' + UserFollow)
                  api.wait_on_rate_limit = True
                  api.wait_on_rate_limit_notify = True
                  requests_cache.install_cache(cache_name='twitter_cache', backend='sqlite', expire_after=180)
                  print("followed {0}".format(api.get_user(f).screen_name))

                  api.create_friendship(f)
                  randnum = randint(10, 25)
                  randnumdisplay = print("Delaying next follow for " + str(randnum) + " seconds.")

                  print(str(randnumdisplay))
                  screen_names = [api.get_user(f).screen_name]
                  time.sleep(randnum)

            except tweepy.TweepError:
                print("Waiting till next interval...")

                time.sleep(60 * 15)

                continue
            except StopIteration:


def main():

    app = QtGui.QApplication(sys.argv)
    gui = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()             break


def main():
    app = QtGui.QApplication(sys.argv)
    gui = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

0 个答案:

没有答案