essentially I am working on a 'draft' of a subscriber counter app in python. I use the YouTube Data API to get the data from YouTube and then loop that piece of code to update the subscriber count. But since the code for my GUI is after the loop it never starts since the loop is infinite and never ends. I tried putting the GUI part before the code to get the sub count but none of the variables are defined so errors are returned. So basically my question is how do I reorganize this so that it works and the sub count gets updated in the GUI. I have heard about people using the threading module but I do not have much experience with this.
import urllib.request
import json
from tkinter import*
name ="pewdiepie"
key = "AIzaSyDAOUFomRB1lxdb_fvSKKaG-FSZDRoVt_s"
i = 1
while i<99999999:
data = urllib.request.urlopen("https://www.googleapis.com/youtube/v3/channels?part=statistics&forUsername="+name+"&key="+key).read()
subs = json.loads(data)["items"][0]["statistics"]["subscriberCount"]
subc =("{:,d}".format(int(subs)))
print(subc)
i = i + 1
root = Tk()
root.geometry("900x600")
root.title("Sub Counter")
label1 = Label(text="Sub Count:", font=("Comic Sans MS", 45), fg="Brown").place(x=10, y=20)
label2 = Label(text=subc, font=("Comic Sans MS", 45), fg="Red").place(x=10, y=130)
root.mainloop()