3

I am trying a test application with Tkinter. I have created a table using grid layout manager as shown below. There are two buttons - Add row and Delete row. Add row works as expected. How i can go about deleting selected rows. My idea was to give a Checkbutton for each row as shown below. And every selected row can be deleted. But i don't know how to do it exactly and if its possible.

Is there a better way to delete the rows in this case ? please suggest a solution.

enter image description here

CODE:

        from Tkinter import *
        import ttk
        from ttk import * 

        i =2

        def add_row():
            global i 
            var = IntVar()
            c = Checkbutton(root, variable = var)
            c.grid(row = i, column = 0)
            for j in range(1,5): #Columns

                    b = Entry(root)
                    b.grid(row=i, column=j)
            i =i+1 

        root = Tk()
        bt = ttk.Button(root , text = 'Add Row', command = add_row)
        bt.grid(row =0, column=0)


        dl = ttk.Button(root , text = 'Delete Row')
        dl.grid(row =0, column=1)

        v0 = StringVar()
        e0 = Entry(root, textvariable = v0, state = 'readonly')
        v0.set('Select')
        e0.grid(row = 1, column = 0 )

        v1 = StringVar()
        e1 = Entry(root, textvariable = v1, state = 'readonly')
        v1.set('Col1')
        e1.grid(row = 1, column = 1 )

        v2 = StringVar()
        e2 = Entry(root, textvariable = v2, state = 'readonly')
        v2.set('Col2')
        e2.grid(row = 1, column = 2)

        v3 = StringVar()
        e3 = Entry(root, textvariable = v3, state = 'readonly')
        v3.set('Col3')
        e3.grid(row = 1, column = 3 )

        v4 = StringVar()
        e4 = Entry(root, textvariable = v4, state = 'readonly')
        v4.set('Col4')
        e4.grid(row = 1, column = 4 )

        mainloop()

Note: I do not want to use tktable or treeview to create tables.

Jio
  • 578
  • 3
  • 8
  • 27

1 Answers1

3

In order to delete widgets, you need to keep a reference to them when they are created.

In the code below, I have created a list rows which has a sub-list items for each row. Each row contains a reference to the checkbutton and all the entries.

When Delete Row is pressed, the function loops through the list and destroys all items in the row that has the checkbutton activated, then removes the entry from the list.

Notes:

  • I have made var an attribute of the checkbutton, so that we can access it to see if it has been checked - see this question.

  • You were creating your checkbutton five times for each row; I've taken it out of the loop.

  • I iterate through the list of rows backwards, so that when an item is pop-ed, it only changes the index for the rows already processed. This means that you can delete multiple rows at once.

from Tkinter import *
import ttk
from ttk import * 

i=2
rows = []

def add_row():
    global i 
    i=i+1
    items = []
    var = IntVar()
    c = Checkbutton(root, variable = var)
    c.val = var
    items.append(c)
    c.grid(row = i, column = 0)
    for j in range(1,5): #Columns
        b = Entry(root)
        items.append(b)
        b.grid(row=i, column=j)
    rows.append(items)

def delete_row():
    for rowno, row in reversed(list(enumerate(rows))):
        if row[0].val.get() == 1:
            for i in row:
                i.destroy()
            rows.pop(rowno)


root = Tk()
bt = ttk.Button(root , text = 'Add Row', command = add_row)
bt.grid(row =0, column=0)


dl = ttk.Button(root , text = 'Delete Row', command = delete_row)
dl.grid(row =0, column=1)

v0 = StringVar()
e0 = Entry(root, textvariable = v0, state = 'readonly')
v0.set('Select')
e0.grid(row = 1, column = 0 )

v1 = StringVar()
e1 = Entry(root, textvariable = v1, state = 'readonly')
v1.set('Col1')
e1.grid(row = 1, column = 1 )

v2 = StringVar()
e2 = Entry(root, textvariable = v2, state = 'readonly')
v2.set('Col2')
e2.grid(row = 1, column = 2)

v3 = StringVar()
e3 = Entry(root, textvariable = v3, state = 'readonly')
v3.set('Col3')
e3.grid(row = 1, column = 3 )

v4 = StringVar()
e4 = Entry(root, textvariable = v4, state = 'readonly')
v4.set('Col4')
e4.grid(row = 1, column = 4 )

mainloop()
Community
  • 1
  • 1
SiHa
  • 7,830
  • 13
  • 34
  • 43