Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
794 views
in Technique[技术] by (71.8m points)

Python Tkinter window crashes after function starts

im working on a Python project that follows people on Instagram. The code works fine, it can unfollow (Entfolgen) and follow (Folgen). However as soon as one of the mentioned buttons is pressed, the Tkinter window crashes. The bot does not have an issue with that, it just prevents me from implementing an output to the window. Heres the full code:

from tkinter import *
from time import sleep
from InstagramAPI import InstagramAPI
import pandas as pd
import sys


users_list = []
following_users = []
follower_users = []
Amount = 0

root = Tk()

root.geometry('400x200')

Benutzername = Entry(root, width= 20,)
Benutzername.insert(0, "Email")
Benutzername.pack()

Passwort = Entry(root, width= 20,show="*")
Passwort.insert(0, "Password",)
Passwort.pack()

Quellaccount = Entry(root, width= 20,)
Quellaccount.insert(0, "Source")
Quellaccount.pack()

global entfolgenvar
entfolgenvar = 0

def Entfolgen():
    global entfolgenvar
    entfolgenvar = 1
    Folgen()

def Folgen():
    global entry1
    global entry2
    global entry3
    global bot
    global InstaBot
    entry1 = Benutzername.get()
    entry2 = Passwort.get()
    entry3 = Quellaccount.get()
    bot =  InstaBot()

    # To follow users run the function below
    # change the username ('instagram') to your target username
    if entfolgenvar == 0:
        bot.get_likes_list(entry3)
    else:
        bot.unfollow_users()

    # To unfollow users uncomment and run the function below
    # bot.unfollow_users()

class InstaBot:

    def __init__(self):
        self.api = InstagramAPI(entry1, entry2)

    def get_likes_list(self,username):
        global api
        api = self.api
        api.login()
        api.searchUsername(entry3) #Gets most recent post from user
        result = api.LastJson
        username_id = result['user']['pk']
        user_posts = api.getUserFeed(username_id)
        result = api.LastJson
        media_id = result['items'][0]['id']

        api.getMediaLikers(media_id)
        users = api.LastJson['users']
        for user in users:
            users_list.append({'pk':user['pk'], 'username':user['username']})
        bot.follow_users(users_list)



    def follow_users(self,users_list):
        api = self.api
        api.login()
        api.getSelfUsersFollowing()
        result = api.LastJson
        for user in result['users']:
            following_users.append(user['pk'])
        for user in users_list:
            if not user['pk'] in following_users:
                print('Following @' + user['username'])
                api.follow(user['pk'])
                # set this really long to avoid from suspension
                sleep(30)
            else:
                print('Already following @' + user['username'])
                sleep(15)

    def unfollow_users(self):
        api = self.api
        api.login()
        api.getSelfUserFollowers()
        result = api.LastJson
        for user in result['users']:
            follower_users.append({'pk':user['pk'], 'username':user['username']})

        api.getSelfUsersFollowing()
        result = api.LastJson
        for user in result['users']:
            following_users.append({'pk':user['pk'],'username':user['username']})
        for user in following_users:
            if not user['pk'] in follower_users:
                print('Unfollowing @' + user['username'])
                api.unfollow(user['pk'])
                sleep(20)
                # set this really long to avoid from suspension


Button(root, text="Folgen", command=Folgen).pack()
Button(root, text="Entfolgen", command=Entfolgen).pack()



root.mainloop()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...