coolio85
coolio85

Reputation: 171

Python Tkinter csv read file to Entry

I am trying to open a csv file, loop through and show what is in the csv file in a Tkinter Entry box so I am able to manually edit and then write to the file

I have tried changing the Label to an Entry but when I do nothing appears in the Entry

def panel_admin():

    admingui.destroy()
    adminpanel = Toplevel()

    with open("Staff Bookings.csv", newline = "") as file:
        reader = csv.reader(file)


        r = 0
        for col in reader:
            c = 0
            for row in col:

                Label(adminpanel, width = 10, height = 2, \
                                   text = row, relief = RIDGE).grid(row = r, column = c)
                c += 1
            r += 1

Upvotes: 1

Views: 835

Answers (1)

scotty3785
scotty3785

Reputation: 7006

You can't set the text of an Entry field in the same way that you would for a Label. You need to use the insert method of an entry widget as shown below

def panel_admin():

    admingui.destroy()
    adminpanel = Toplevel()

    with open("Staff Bookings.csv", newline = "") as file:
        reader = csv.reader(file)


        r = 0
        for col in reader:
            c = 0
            for row in col:

                curEntry = Entry(adminpanel, width = 10, relief = RIDGE)
                curEntry.grid(row = r, column = c)
                curEntry.insert(0, row)
                c += 1
            r += 1

Note that if you want to write out the edited entry fields, you should probably store the curEntry in a list or dictionary for you to access later. For example you could keep the row,col number as a dictionary key and the Entry widget as the values using something like the code below

fields[(r,c)] = curEntry

Upvotes: 1

Related Questions