pythonooptkinterttkttkwidgets

python tkinter - Deleting all TreeView Children


I will like to create a clear button that will delete all the records in the treeview while at the same time, I am trying to improve my OOP via Tkinter at the same. (So if there is other non-best practice in the code; could you please advise as well).

From what I found out through googling, I could possibl use a get_children method on treeview, retrieve them and then subsequently deleting them one bye one.

def delButton(self):
    x = main.tree.get_children()
    for item in x:
        main.tree.delete(item)

However, i keep getting attribute error : AttributeError: 'NoneType' object has no attribute 'get_children'. Is it the way I reference in in my classes?

import tkinter as tk
from tkinter import ttk,messagebox


gray = '#808080'
green = '#7FFF00'
black = '#000000'
filter_font = ('Arial',12,'bold')
search_font = ('Arial',10)

class News:
    def __init__(self):
        self.window = tk.Tk()
        self.window.geometry("1000x500")
        self.window.resizable(0,0)


        self.quit_button = self.create_quit_button()
        self.clear_button = self.create_clear_button()

        self.tree_view = self.create_tree_view()




    def create_tree_view(self):
        self.tree = ttk.Treeview(self.window,columns=(1,2,3,4) , show= 'headings',height='12')
        self.tree.grid(row=0, column=0, rowspan=6, columnspan=9, padx=10, pady=10)

        self.tree.column(1,width=70)
        self.tree.column(2,width=70)

        self.tree.heading(1,text ='Hello',anchor='w')
        self.tree.heading(2,text ='World',anchor='w')

        self.tree.insert(parent='',index='end',iid=0,values = ['John','John@gmail.com'])
        self.tree.insert(parent='',index='end',iid=1,values = ['John','John@gmail.com'])



        self.tree.grid(row=0, column=0, rowspan=6, columnspan=9, padx=10, pady=10)


    def create_clear_button(self):
        self.clear_button = tk.Button(self.window,text='Clear',fg=green,bg = black,command = lambda : self.clear())
        self.clear_button.grid(row=0,column=20)

    def create_quit_button(self):
        quit_button = tk.Button(self.window,text='Quit',fg=green,bg = black,command =lambda: self.quit())
        quit_button.grid(row =4,column = 10)


    def quit(self):
        self.window.quit()

    def run(self):
        self.window.mainloop()


    def clear(self):
        for item in self.tree_view.get_children():
            self.tree.delete(item)
        return (self.tree)

if __name__ == '__main__':
    news = News()
    news.run()

Solution

  • Note that inside clear(), you have used self.tree_view in the line for item in ..., while self.tree is used inside the for loop block:

    def clear(self):
        for item in self.tree_view.get_children(): # used self.tree_view
            self.tree.delete(item)                 # used self.tree
        return (self.tree)
    

    self.tree_view is None because it is the result of create_tree_view() which does not return anything:

    self.tree_view = self.create_tree_view()
    

    Actually you don't need self.tree_view, use self.tree in clear():

    def clear(self):
        for item in self.tree.get_children(): # used self.tree instead
            self.tree.delete(item)
        #return (self.tree) # return here is useless as clear() is triggered by a button