Ali Zulfiqar
Ali Zulfiqar

Reputation: 1

How to make multiple CTkButtons in a for loop with different command functions

I want to create multiple CTkButtons with the same function but different parameters in a single loop.

This is a code snippet that I have written:


#Heres the gui_read function
def gui_read(row):
            for widget in main.winfo_children():
                widget.destroy()

            entry_frame = CTkTextbox(main, font = ('georgia', 17), corner_radius = 0, width = 650, height = 500, fg_color = '#1E5994')
            entry_frame.place(x=0, y = 80)
            entry = row['entry'].split('\\n')
            entry_frame.insert('0.0', row['date']+'\n'+row['category']+'\n'+'\n'+'\n'.join(entry))
            entry_frame.configure(state = 'disabled')


with open('diary.csv', 'r') as db:
    reader = csv.DictReader(db)
    for i, row in enumerate(reader, start = 2):
        CTkLabel(frame, text = row['date'], font = ('roboto', 15)).grid(row = i, column = 1)
        CTkLabel(frame, text = row['category'], font = ('roboto', 15)).grid(row = i, column = 2, padx = 60)
        CTkButton(frame, text = 'Read', width = 100, height = 15, font = ('roboto', 15), command = lambda : gui_read(row)).grid(row = i, column = 3)

And this a sample that shows how diary.csv look like:

date,category,entry

13-09-2024,'special','hello diary,'

Imports include customtkinter and csv

In this code snippet, the gui_read function takes in a row and shows it on the screen. The problem is that I want all the buttons to show their own rows but all of them show the last one that iterated through the loop, which makes sense.

Please provide some code to help me on how to do this task.

Upvotes: 0

Views: 30

Answers (0)

Related Questions