0

I have Accountid column in a Dataframe. Now I am using Tkinter to draw some gui. For each Accountid I want to create a separate button.My code is able to do that. But after this, for each button I want to create a subwindow, which would contain some columns for each Accountid. Now the problem is that my code creates sub-window for each button, but it fetches the columns for only the last Accountid.

I would really appreciate if someone can help me.

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()​
0

1 Answer 1

1

After this loop :

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)

id is a local variable to the class Example. And id is set to the last value of df['AccountId'] at the end of the above for loop. for example:

for i in range(0,10):
  pass
print i 

Result:

9

So in

label = tk.Label(top, text=s3.loc[s3.AccountId == id][['AccountId','confidence','lift']])

the comparison s3.AccountId == id will always comapare with the latest value of id that's why you are always getting the columns for only the last Accountid.

workaround :

import tkinter as tk
import pandas as pd

root = tk.Tk() #declare root in global scope for simplicity


class new_button:

    def __init__(self,A_id):

        self.id=A_id
        but = tk.Button(root,text=self.id,command=self.new_window)
        but.pack(side="top", padx=20, pady=20)



    def new_window(self):

        top = tk.Toplevel(root)# create sub window

        label = tk.Label(top, text=s3.loc[s3.AccountId == self.id][['AccountId','confidence','lift']])

        label.pack(side="top", fill="both", expand=True, padx=20, pady=20)


if __name__ == "__main__":
    for i in df['AccountId']:##for each id create a button

            butt=new_button(A_id=i)

    root.mainloop()

created a new_button class which creates a unique button for each AccountId in the dataframe.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the detailed response. But how to resolve this? Any suggestions.
@user2906657, provide a sample for df and s3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.