0

I am simulating a banking application and I am making a screen where the user can view their active accounts. A user can have any number of accounts (3 in the example below) so I use a loop to create buttons and links for each account. When the 'show' link next to each account button is clicked, it should reveal the whole account number then hide again once the link is clicked again. Essentially, link1 should reveal button1 and so on. What is the best way to have each link distinguish between which button they should be editing?

So far I've tried giving the buttons and links a unique id or placing them in lists. However, I am unable to find a way distinguish between the values in the list when the link is clicked

Below is my current code and screenshot of the interface:

        diff = 0.1 # current position of newly made object
        i = 0 # helps distinguish unique names of links
        buttons = []
        links = []
        accts = []

       # account: ['account_no', balance, 'rounting_no', 'username']

        for account in accounts: # accounts is a mySQL query of all accounts owned by one user and account[0] is each account number[![enter image description here][1]][1]

            account_button = tk.Button(check_balance_tab, text="Account Ending in x" + account[0][-4:], height=1, width=40) # the account button is created
            account_button.configure(font=("Times New Roman", 14))
            account_button.place(relx=.3, rely=diff, anchor="center") # button is placed
            buttons.append(account_button)
            accts.append(account[0])

            show_account = tk.Label(check_balance_tab, name=str(i), text="Show", fg="blue") # label is created
            show_account.bind("<Button-1>", lambda e: show_account_no()) # show_account_no currently doesn't return anything
            show_account.place(relx=.55, rely=diff, anchor="center") # label is placed
            links.append(show_account)

            i += 1
            diff += 0.1 # increment diff so the next button and label are placed below the previous


def show_account_no():
    print()

enter image description here

I am more than happy to clarify any questions or confusion regarding the above code and description. Thank you

Mike
  • 53
  • 7
  • 1
    Note that `lambda e: show_account_no` will not execute `show_account_no()` when the label is clicked because of missing `()`. – acw1668 Apr 14 '22 at 04:16
  • @acw1668 thank you> I removed that by mistake when adding the code to stackoverflow. I fixed this now as to avoid confusion – Mike Apr 14 '22 at 04:22
  • 1
    See the duplicate question for how you can bind a lambda to a loop-specific variable. Then use this as the `command` option in the control. – Barmar Apr 14 '22 at 04:24

0 Answers0