检查变化的数字是否低于或高于上次
Check Wether A Changing Number is Lower or Higher Than it Last Was
我正在开发一个取笑 Logan Paul 的 python 程序,以练习我的 python 技能。基本上我的想法是监控 Logan 的订阅者数量,如果他失去订阅者,就会取笑他。到目前为止,我已经创建了一个(错误的)GUI 来显示他的子计数。我将如何去监控它并知道他是失去了还是获得了潜艇?作为我的概念证明,我希望它在控制台中执行类似 print "Lost" 或 "Gained" 的操作。我认为要做到这一点,我必须使用 io
模块将之前的数字存储在内存中,但我认为这不是最好的方法。
到目前为止,这是我的代码,Comic Sans 是为了效果:
import urllib.request
import json
from tkinter import*
channelid = "UCG8rbF3g2AMX70yOd8vqIZg"
key = "AIzaSyDAOUFomRB1lxdb_fvSKKaG-FSZDRoVt_s"
def func(label2):
data = urllib.request.urlopen("https://www.googleapis.com/youtube/v3/channels?part=statistics&id="+channelid+"&key="+key).read()
subs = json.loads(data)["items"][0]["statistics"]["subscriberCount"]
subc =("{:,d}".format(int(subs)))
label2.config(text=subc)
label2.update()
root.after(10, lambda:func(label2))
root = Tk()
root.geometry("900x600")
root.title("yeetmeister")
label1 = Label(text="Logan Paul's Sub Count:", font=("Comic Sans MS", 45), fg="Brown")
label2 = Label(font=("Comic Sans MS", 45), fg="Red")
label1.place(x=10, y=20)
label2.place(x=10, y=130)
func(label2)
root.mainloop()
只要让它成为你记住的变量,这样当你得到新的子计数时你就可以比较它们
prev_subs = 0
def func():
current_subs = subs #from api
if prev_subs:
if prev_subs > current_subs:
#went down
elif prev_subs < current_subs:
#went up
prev_subs = current_subs
while true: # just to continually refresh the data
func(label2)
您需要一个变量来保存上一次检查中的订阅者并与新检查中的订阅者进行比较。因此,您必须按如下方式修改您的程序:
import urllib.request
import json
from tkinter import*
channelid = "UCG8rbF3g2AMX70yOd8vqIZg"
key = "AIzaSyDAOUFomRB1lxdb_fvSKKaG-FSZDRoVt_s"
prevSubs = 0 # Variable to hold the subs from previous check
firstTime = True # Flag variable to avoid checking the first time due to lack of previous subscribers.
def func(label2,prevSubs, firstTime): # Change the signature here.
data = urllib.request.urlopen("https://www.googleapis.com/youtube/v3/channels?part=statistics&id="+channelid+"&key="+key).read()
subs = json.loads(data)["items"][0]["statistics"]["subscriberCount"]
subc =("{:,d}".format(int(subs)))
subs = int(subc.replace(',',''))
# Comparisons to print the appropriate message
if firstTime:
print("Started monitoring...")
firstTime = False
pass
else:
if subs > prevSubs:
print("Gained")
elif subs < prevSubs:
print("Lost")
else:
pass
prevSubs = subs # Update previous subs.
label2.config(text=subc)
label2.update()
root.after(10, lambda:func(label2,prevSubs,firstTime)) # Add argument to 'func'.
root = Tk()
root.geometry("900x600")
root.title("yeetmeister")
label1 = Label(text="Logan Paul's Sub Count:", font=("Comic Sans MS", 45), fg="Brown")
label2 = Label(font=("Comic Sans MS", 45), fg="Red")
label1.place(x=10, y=20)
label2.place(x=10, y=130)
func(label2,prevSubs,firstTime) # Add new argument to 'func'.
root.mainloop()
可以添加全局变量:
import urllib.request
import json
from tkinter import*
channelid = "UCG8rbF3g2AMX70yOd8vqIZg"
key = "AIzaSyDAOUFomRB1lxdb_fvSKKaG-FSZDRoVt_s"
score = 0
def func(label2, label3):
global score
r = urllib.request.urlopen("https://www.googleapis.com/youtube/v3/channels?part=statistics&id="+channelid+"&key="+key).read()
# data = r.json()
subs = json.loads(r.decode('utf-8'))["items"][0]["statistics"]["subscriberCount"]
newscore = int(subs)
if(score < newscore):
label3.config(text="UP" + " prev[ " + str(score) + "]")
label3.config(fg="Green")
else:
if(score == newscore):
label3.config(text="SAME" + " prev[ " + str(score) + "]")
label3.config(fg="Blue")
else:
label3.config(text="DOWN" + " prev[ " + str(score) + "]")
label3.config(fg="Red")
score = newscore
subc =("{:,d}".format(newscore))
label2.config(text=subc)
label2.update()
root.after(10, lambda:func(label2, label3))
root = Tk()
root.geometry("900x600")
root.title("yeetmeister")
label1 = Label(text="Logan Paul's Sub Count:", font=("Comic Sans MS", 45), fg="Brown")
label2 = Label(font=("Comic Sans MS", 45), fg="Red")
label3 = Label(font=("Comic Sans MS", 45), fg="Blue")
label1.place(x=10, y=20)
label2.place(x=10, y=130)
label3.place(x=340, y=130)
label3.config(text="NO CHANGE")
func(label2, label3)
root.mainloop()
我正在开发一个取笑 Logan Paul 的 python 程序,以练习我的 python 技能。基本上我的想法是监控 Logan 的订阅者数量,如果他失去订阅者,就会取笑他。到目前为止,我已经创建了一个(错误的)GUI 来显示他的子计数。我将如何去监控它并知道他是失去了还是获得了潜艇?作为我的概念证明,我希望它在控制台中执行类似 print "Lost" 或 "Gained" 的操作。我认为要做到这一点,我必须使用 io
模块将之前的数字存储在内存中,但我认为这不是最好的方法。
到目前为止,这是我的代码,Comic Sans 是为了效果:
import urllib.request
import json
from tkinter import*
channelid = "UCG8rbF3g2AMX70yOd8vqIZg"
key = "AIzaSyDAOUFomRB1lxdb_fvSKKaG-FSZDRoVt_s"
def func(label2):
data = urllib.request.urlopen("https://www.googleapis.com/youtube/v3/channels?part=statistics&id="+channelid+"&key="+key).read()
subs = json.loads(data)["items"][0]["statistics"]["subscriberCount"]
subc =("{:,d}".format(int(subs)))
label2.config(text=subc)
label2.update()
root.after(10, lambda:func(label2))
root = Tk()
root.geometry("900x600")
root.title("yeetmeister")
label1 = Label(text="Logan Paul's Sub Count:", font=("Comic Sans MS", 45), fg="Brown")
label2 = Label(font=("Comic Sans MS", 45), fg="Red")
label1.place(x=10, y=20)
label2.place(x=10, y=130)
func(label2)
root.mainloop()
只要让它成为你记住的变量,这样当你得到新的子计数时你就可以比较它们
prev_subs = 0
def func():
current_subs = subs #from api
if prev_subs:
if prev_subs > current_subs:
#went down
elif prev_subs < current_subs:
#went up
prev_subs = current_subs
while true: # just to continually refresh the data
func(label2)
您需要一个变量来保存上一次检查中的订阅者并与新检查中的订阅者进行比较。因此,您必须按如下方式修改您的程序:
import urllib.request
import json
from tkinter import*
channelid = "UCG8rbF3g2AMX70yOd8vqIZg"
key = "AIzaSyDAOUFomRB1lxdb_fvSKKaG-FSZDRoVt_s"
prevSubs = 0 # Variable to hold the subs from previous check
firstTime = True # Flag variable to avoid checking the first time due to lack of previous subscribers.
def func(label2,prevSubs, firstTime): # Change the signature here.
data = urllib.request.urlopen("https://www.googleapis.com/youtube/v3/channels?part=statistics&id="+channelid+"&key="+key).read()
subs = json.loads(data)["items"][0]["statistics"]["subscriberCount"]
subc =("{:,d}".format(int(subs)))
subs = int(subc.replace(',',''))
# Comparisons to print the appropriate message
if firstTime:
print("Started monitoring...")
firstTime = False
pass
else:
if subs > prevSubs:
print("Gained")
elif subs < prevSubs:
print("Lost")
else:
pass
prevSubs = subs # Update previous subs.
label2.config(text=subc)
label2.update()
root.after(10, lambda:func(label2,prevSubs,firstTime)) # Add argument to 'func'.
root = Tk()
root.geometry("900x600")
root.title("yeetmeister")
label1 = Label(text="Logan Paul's Sub Count:", font=("Comic Sans MS", 45), fg="Brown")
label2 = Label(font=("Comic Sans MS", 45), fg="Red")
label1.place(x=10, y=20)
label2.place(x=10, y=130)
func(label2,prevSubs,firstTime) # Add new argument to 'func'.
root.mainloop()
可以添加全局变量:
import urllib.request
import json
from tkinter import*
channelid = "UCG8rbF3g2AMX70yOd8vqIZg"
key = "AIzaSyDAOUFomRB1lxdb_fvSKKaG-FSZDRoVt_s"
score = 0
def func(label2, label3):
global score
r = urllib.request.urlopen("https://www.googleapis.com/youtube/v3/channels?part=statistics&id="+channelid+"&key="+key).read()
# data = r.json()
subs = json.loads(r.decode('utf-8'))["items"][0]["statistics"]["subscriberCount"]
newscore = int(subs)
if(score < newscore):
label3.config(text="UP" + " prev[ " + str(score) + "]")
label3.config(fg="Green")
else:
if(score == newscore):
label3.config(text="SAME" + " prev[ " + str(score) + "]")
label3.config(fg="Blue")
else:
label3.config(text="DOWN" + " prev[ " + str(score) + "]")
label3.config(fg="Red")
score = newscore
subc =("{:,d}".format(newscore))
label2.config(text=subc)
label2.update()
root.after(10, lambda:func(label2, label3))
root = Tk()
root.geometry("900x600")
root.title("yeetmeister")
label1 = Label(text="Logan Paul's Sub Count:", font=("Comic Sans MS", 45), fg="Brown")
label2 = Label(font=("Comic Sans MS", 45), fg="Red")
label3 = Label(font=("Comic Sans MS", 45), fg="Blue")
label1.place(x=10, y=20)
label2.place(x=10, y=130)
label3.place(x=340, y=130)
label3.config(text="NO CHANGE")
func(label2, label3)
root.mainloop()