【发布时间】:2020-05-15 18:00:33
【问题描述】:
我正在尝试创建一个 tkinter gui,它以多元时间序列开始,并使用 tkinter gui 允许用户在使用 plotly express 创建的滑动窗口中选择和绘制某些通道读数。我刚刚开始了解 tkinter,并且正在 Mac OS 10.15.4 上使用带有 IDLE 的 Python 3.7。
在下面的示例中,我的时间序列有七个标记的通道,每个通道有 10000 个时间记录。通道标签是 'a','b','c','d','e','f','g' 并且系列存储在 1000×7 数组 X 中。因为我赢了在这个脚本的未来实现中,并不总是提前知道通道的数量和它们的标签,我在这里使用了一个循环来创建带标签的复选框。当我尝试创建数据框时出现问题:
import numpy as np
import plotly.express as px
from tkinter import Tk,Button, Label, Checkbutton,BooleanVar
import pandas as pd
window = Tk()
window.title('My Window')
window.geometry('500x500')
np.random.seed(123)
X = np.random.randn(10000,7)
channels=['a','b','c','d','e','f','g']
num_channels=len(channels)
'''Checkbuttons for channels, appearing in one row. Each initially set true'''
channel_vars=[]
channel_buttons=[]
for i in range(num_channels):
channel_vars.append(BooleanVar())
channel_vars[i].set(True)
channel_buttons.append(Checkbutton(window,text=channels[i],var=channel_vars[i]))
channel_buttons[i].grid(row=0,column=i)
'''Determine selected indices'''
def _selected_indices():
indices=[i for i in range(num_channels) if channel_vars[i].get()]
print(indices)
selected_indices_btn = Button(window, text="Select",command=_selected_indices)
selected_indices_btn.grid(row=1, column=0)
''' Create data frame using only the selected channels'''
df=pd.DataFrame(X[:,indices],columns=channels[indices])
df['x']=df.index
'''Plot selected channels'''
def _plot_selected():
df_melt = pd.melt(df, id_vars="x", value_vars=df.columns[:-1])
fig=px.line(df_melt, x="x", y="value",color="variable",labels = {'x':'time
(sec)','variable':'channel'})
fig.update_layout(xaxis=dict(rangeslider=dict(visible=True),type="linear"))
fig.show()
plot_button = Button(master=window, text="Plot", command=_plot_selected)
plot_button.grid(row=2, column=0)
'''Quit button'''
def _quit():
window.quit()
window.destroy()
quit_button = Button(master=window, text="Quit", command=_quit)
quit_button.grid(row=3, column=0)
window.mainloop()
错误消息告诉我,当我尝试构建我的数据框 df 时,“索引”是未知的,结果表明我在回调和/或组织我的 gui 窗口方面缺少一些基本的东西。
【问题讨论】: