我有一个Accountid列。现在我用Tkinter绘制一些图形用户界面。我想为每个Accountid创建一个单独的按钮。我的代码可以做到这一点。但是在这之后,我想为每个按钮创建一个子窗口,其中包含每个Accountid的一些列。现在的问题是,我的代码为每个按钮创建子窗口,但它只获取最后一个Accountid的列。在
如果有人能帮我,我会非常感激的。在
import tkinter as tk
class Example(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
for id in df['AccountId']:##for each id create a button
new_win_button = tk.Button(self, text=id,
command= self.new_window)
new_win_button.pack(side="top", padx=20, pady=20)
def new_window(self):
top = tk.Toplevel(self)# create sub window
print (id)
#print (id is 'id')
label = tk.Label(top, text=s3.loc[s3.AccountId == id][['AccountId','confidence','lift']])
label.pack(side="top", fill="both", expand=True, padx=20, pady=20)
if __name__ == "__main__":
root = tk.Tk()
Example(root).pack(fill="both", expand=True)
root.mainloop()
在这个循环之后:
^{pr2}$id
是类Example
的局部变量。并且id
被设置为上述for循环末尾df['AccountId']
的最后一个值。例如:结果:
所以
比较
s3.AccountId == id
将始终与最新值id
匹配,这就是为什么总是只获取最后一个Accountid的列。在解决方法:
创建了一个
new_button
类,该类为dataframe中的每个AccountId
创建一个唯一的按钮。在相关问题 更多 >
编程相关推荐