Tkinter按钮,通过API调用刷新数据



我已经在Tkinter库和函数调用中完成了noob,所以我向您寻求一些帮助:

我尝试创建一个小部件来检查加密货币价格和PNL。

到目前为止,我已经在一个函数中创建了CoinGecko API调用,以返回数据帧和PNL结果。我还用Tkinter创建了GUI。

我当前的脚本如下:

from pycoingecko import CoinGeckoAPI
import pandas as pd
import datetime
from tkinter import *
import time
def get_crypto_data():
data = []
cg = CoinGeckoAPI()
coins = cg.get_price(ids='bitcoin,ethereum,,neo,ripple', vs_currencies='usd', include_24hr_change='true')
df = pd.DataFrame.from_dict(coins, orient='index')
PNL = "something"
data.append(df)
data.append(PNL)
return data
def Click():
get_crypto_data()
def setup_GUI(mask,crypto_data, click):
mask.wm_title("Live Portfolio Data")
date_time = datetime.datetime.now().strftime("%A %d.%m.%Y %H:%M:%S")
l1 = Label(mask,bg='blue', text= "Live Crypto portfolio Data")
l1.grid(row=0, column=0)
l2 = Label(mask,bg='yellow', text= date_time)
l2.grid(row=1, column=0)
l3 = Label(mask,bg='yellow', text=crypto_data[0])
l3.grid(row=2, column=0)
l4 = Label(mask, bg='yellow', text=crypto_data[1])
l4.grid(row=3, column=0)
butt = Button(mask, padx=2, bd=2, fg="black", font=('arial', 20, 'bold'),
text="Button", bg="yellow", command=lambda: Click())
butt.grid(row=5, column=0)
mask.geometry("500x500")
mask.configure(bg='yellow')
return mask
def main():
while True:
mask = Tk()
crypto_data = get_crypto_data()
click = Click()
mask = setup_GUI(mask,crypto_data,click)
mask.update()
mask.mainloop()
if __name__ == '__main__':
main()

我想做的是让Button API调用并刷新数据帧和PNL,并将其显示在我的掩码上。

你有什么办法做到这一点吗?我想这没那么难,但我不知道去哪里找:(

谢谢!

您应该将tkinter.Tk作为子类。我相应地编辑了您的代码:

from pycoingecko import CoinGeckoAPI
import pandas as pd
import datetime
from tkinter import *
import time
class Window(Tk):
def __init__(self):
Tk.__init__(self) # initiate the window

def setup_ui(self):
self.wm_title("Live Portfolio Data")
date_time = datetime.datetime.now().strftime("%A %d.%m.%Y %H:%M:%S")
self.l1 = Label(self, bg="blue", text="Live Crypto portfolio Data")
self.l1.grid(row=0, column=0)
self.l2 = Label(self, bg="yellow", text=date_time)
self.l2.grid(row=1, column=0)
self.l3 = Label(self, bg="yellow", text="placeholder") # placeholder will be replaced with correct values
self.l3.grid(row=2, column=0)
self.l4 = Label(self, bg="yellow", text="placeholder")
self.l4.grid(row=3, column=0)
butt = Button(self, padx=2, bd=2, fg="black", font=("arial", 20, "bold"),
text="Button", bg="yellow", command=self.update_data) # when the user clicks the button, fetch the data again and update the labels
butt.grid(row=5, column=0)
self.geometry("500x500")
self.configure(bg="yellow")
self.update_data() # call update_data once to display correct values on the labels
def update_data(self, *args): # *args makes accept this function any number of arguments, args will be a list with all arguments
self.data = [] # make data a member of this class
cg = CoinGeckoAPI()
coins = cg.get_price(ids="bitcoin,ethereum,neo,ripple", vs_currencies="usd", include_24hr_change="true")
df = pd.DataFrame.from_dict(coins, orient="index")
PNL = "something"
self.data.append(df)
self.data.append(PNL)

date_time = datetime.datetime.now().strftime("%A %d.%m.%Y %H:%M:%S") # if you do not want
self.l2.config(text=date_time) # the time to be updated, remove these two lines

# update labels with values got above
self.l3.config(text=self.data[0])
self.l4.config(text=self.data[1])
def main():
mask = Window() # create window
mask.setup_ui() # create labels, buttons, etc.
mask.update() # make sure everything shows up correctly
mask.mainloop() # now show the window
if __name__ == "__main__":
main()

请在代码中处处使用单引号('(或双引号("(。python将两者混合使用是没有问题的,但不将它们混合使用会使代码更可读。

最新更新