0
from tkinter import *
from Createwindow import *

button_name = ""

def click():
    window = Tk()
    window.geometry("600x500")
    frame = Frame(window, bg = "#161853", width = 600, height = 500)
    row_ = 0
    column_ = 0

    list = []
    with open ("Passwords.txt", 'r') as data_file:
        data = data_file.readlines()
        for i in data:
            names = i.split("|")
            list.append(names[0])
    for i in range(len(list)):
        name = list[i]
        button_ = Button(frame,text=name, font=("Helvetica", 12), width=16, bg="white", fg="black", command=create_window)
        button_.grid(row=row_, column=column_, padx=(25, 0), pady=(25,0))
        column_ += 1
        if column_ == 3:
           row_ += 1
           column_ = 0
    button_name = button_.cget('text')

    frame.pack(fill="both", expand=True)
    window.mainloop()   

def create_window():
    def fill_fields():
        with open ("Passwords.txt", 'r') as data_file:
            data = data_file.readlines()
            
            for i in data:
                names = i.split("|")
                if names[0] == button_name:
                    Website_.insert(0, names[0])
                    Username_.insert(0, names[1]) 
                    Password_.insert(0, names[2])

    def save_password():
        if Website_.get() == "" or Username_.get() == "" or Password_.get() == "":
            window = Tk()
            Error_Message = Label(window, text = "Error! Please fill all the above fields.", bg="red", fg="white", font=("Arial", 16))
            Error_Message.pack()
            window.mainloop()
        else:
            with open("Passwords.txt" , 'a') as file:
                file.write(Website_.get() + "|" + Username_.get() + "|" + Password_.get() + "\n")

    window = Tk()
    window.geometry("600x500")
    window.configure(bg="#161853")
    window.minsize(600, 500)
    frame = Frame(window, bg="#161853")
    frame.rowconfigure(2, weight=1)
    frame.columnconfigure([0, 1, 2], weight=1)
    Text = Label(frame, text="Save your password here", font=("Helvetica", 14), fg="white", bg="#161853")
    Text.grid(row=0, column=1, pady=(0,25))
    Website = Label(frame, text="Website:", font=("Helvetica", 14), fg="white", bg="#161853")
    Website.grid(row=1, column=0, padx=(0, 15), pady=(0,20))
    Website_ = Entry(frame, width=25)
    Website_.grid(row=1, column=1, pady=(0, 20))
    Username = Label(frame, text="Username/Email:", font=("Helvetica", 14), fg="white", bg="#161853")
    Username.grid(row=2, column=0, padx=(0, 15), pady=(0, 20))
    Username_ = Entry(frame, width=25)
    Username_.grid(row=2, column=1, pady=(0, 20))
    Password = Label(frame, text="Password:", font=("Helvetica", 14), fg="white", bg="#161853" )
    Password.grid(row=3, column=0, padx=(0, 15), sticky="s")
    Password_ = Entry(frame, width=25)
    Password_.grid(row=3, column=1, pady=(0, 30), sticky="s")
    Generate = Button(frame, text="Generate Password", font=("Helvetica", 12), width=16)
    Generate.grid(row=3, column=2, sticky="s")
   
    Save = Button(frame, text="Save Password", font=("Helvetica", 12), width=14, command=save_password)
    Save.grid(row=4, column=1)
    frame.place(relx=0.5, rely=0.5, anchor="center")

    fill_fields()  

    window.mainloop()  

In this piece of code basically I am creating a window where all the passwords saved in password.txt file will appear. The code creates a button on the name of the website whose password is saved.

What I want to do is that when that button is clicked I want to get the text of the clicked button bcoz all the buttons are created automatically they have same name and the text of button is the one that is created in the last iteration.

AcK
  • 2,063
  • 2
  • 20
  • 27
Newbie
  • 1
  • 3
  • 1
    Please [edit] your question to have a [mre] and format it in a maner that makes it easy to read, in order to get help. – Thingamabobs Feb 19 '22 at 09:37

1 Answers1

-1

You may pass the argument while adding click event in dynamic generated button. for example below is your old code:

button_ = Button(frame,text = name , font = ("Helvetica", 12), width = 16, bg = "white" , fg = "black", command= create_window)

If you want to get the value of name when click on this button. Just do as like below:

button_ = Button(frame,text = name , font = ("Helvetica", 12), width = 16, bg = "white" , fg = "black", command= create_window(name))

Here i'm adding name argument in create_window function. That i had already impletemented in my project and its working fine. Please try it and let me know if you will face any issue.

Please check below link for more details. https://www.delftstack.com/howto/python-tkinter/how-to-pass-arguments-to-tkinter-button-command/