【发布时间】:2020-10-17 14:27:39
【问题描述】:
所以我有一个 animate() 函数,它在我的 tkinter 应用程序结束时每秒被调用一次,看起来像这样 ani = animation.FuncAnimation(fig, animate, interval=1000)。
函数 animate() 被调用,下面列出了该函数以及图形和子图。
我的问题是如何让按钮(button1000)改变kline_data中的interval?我正在尝试将初始间隔设置为“1m”或1 分钟,然后单击一个按钮并让它为“5m”、“30m”或“1h”间隔设置动画。
kline_data = client.get_klines(symbol='ETHUSDT', interval='1m', limit=30)
以下是代码,但由于这是一个较大项目的一部分,因此已删除了部分代码。 任何帮助将不胜感激!
from binance.client import Client
client = Client(api_key, api_secret)
graph_window = tk.Tk()
fig = Figure(figsize=(7, 5), tight_layout=True)
a = fig.add_subplot(111)
def animate(i):
kline_data = client.get_klines(symbol='ETHUSDT', interval='1m', limit=30)
df = pd.DataFrame(kline_data).drop([7, 9, 10, 11], axis=1)
df.columns = ['Open Time', 'Open', 'High', 'Low', 'Close', 'Volume', 'Close Time', 'Num of Trades']
df[['Open', 'High', 'Low', 'Close', 'Volume']] = df[['Open', 'High', 'Low', 'Close', 'Volume']].astype(
float)
df['Open Time'] = pd.to_datetime(df['Open Time'], unit='ms')
df['Close Time'] = pd.to_datetime(df['Close Time'], unit='ms')
df['EMA7'] = df['Close'].ewm(span=7, adjust=False).mean()
df['SMA7'] = df['Close'].rolling(7).mean()
a.clear()
a.yaxis.tick_right()
a.margins(x=0.03)
a.set_title('Last Price of ETHUSDT: ' + str(df['Close'][29]))
df.plot(x='Close Time', y='Close', ax=a)
df.plot(x='Close Time', y='EMA7', ax=a)
df.plot(x='Close Time', y='SMA7', ax=a)
canvas = FigureCanvasTkAgg(fig, graph_container)
canvas.draw()
canvas.get_tk_widget().pack()
labelframe = tk.LabelFrame(controls, borderwidth=5, text='Graph Controls', labelanchor='n', pady=5)
button0000=ttk.Label(labelframe, text="Time:")
button1000=tk.Button(labelframe, text="1m")
ani = animation.FuncAnimation(fig, animate, interval=1000)
graph_window.mainloop()
【问题讨论】:
-
animate()函数被FuncAnimation对象重复调用。只需将区间设为全局变量(或其他持久存储,例如实例属性),然后更改该变量:更改将从下一个动画帧开始生效。
标签: python function user-interface tkinter lambda