Welcome, guest | Sign In | My Account | Store | Cart

Delete a Gtk.TreeView row

Python, 90 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#! /usr/bin/python

###########################################################
#
# Delete a Gtk.TreeView row
#
###########################################################

from gi.repository import Gtk
import os

class MyWindow(Gtk.Window):
    
    def __init__(self):

        Gtk.Window.__init__(self, title='My Window Title')
        self.connect('delete-event', Gtk.main_quit)        
        
        store = Gtk.ListStore(str, str)
        self.populate_store(store)
        
        self.treeview = Gtk.TreeView(model=store)

        renderer = Gtk.CellRendererText()
        
        column_name = Gtk.TreeViewColumn('Song Name', renderer, text=0)
        column_name.set_sort_column_id(0)        
        self.treeview.append_column(column_name)
        
        column_artist = Gtk.TreeViewColumn('Artist', renderer, text=1)
        column_artist.set_sort_column_id(1)
        self.treeview.append_column(column_artist)
        
        scrolled_window = Gtk.ScrolledWindow()
        scrolled_window.set_policy(
            Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC)
        scrolled_window.add(self.treeview)
        scrolled_window.set_min_content_height(200)
        
        button = Gtk.Button('Delete Selected Row')
        button.connect('clicked', self.on_button_clicked) 

        box = Gtk.Box()
        box.pack_start(scrolled_window, True, True, 1)
        box.pack_start(button, False, False, 1)

        self.add(box)
        self.show_all()

    def on_button_clicked(self, button):
     
  # Get the TreeView selected row(s)
        selection = self.treeview.get_selection()
        # get_selected_rows() returns a tuple
        # The first element is a ListStore
        # The second element is a list of tree paths
        # of all selected rows
        model, paths = selection.get_selected_rows()
        
        # Get the TreeIter instance for each path
        for path in paths:
   iter = model.get_iter(path)
   # Remove the ListStore row referenced by iter
   model.remove(iter)


    # Add data to ListStore
    def populate_store(self, store):
        
        store.append(['American Pie', 'Don McLean'])
        store.append(['Crossroads', 'Don McLean'])
        store.append(['Winterwood', 'Don McLean'])
        store.append(['Mountains of Mourne', 'Don McLean'])
        store.append(['Birthday song', 'Don McLean'])
        store.append(['Everyday', 'Don McLean'])
        store.append(['Crying', 'Don McLean'])
        store.append(['My Dear', 'Azra'])
        store.append(['Niska Bisera', 'Azra'])
        store.append(['Sticenik', 'Azra'])
        store.append(['Mon Ami', 'Azra'])
        store.append(['3N', 'Azra'])
        store.append(['Jane', 'Azra'])
        store.append(['Ghost of a Smile', 'The Pogues'])
        store.append(['Fairytale of New York', 'The Pogues'])
        store.append(['Fiesta', 'The Pogues'])
        store.append(['Summer in Siam', 'The Pogues'])
        
            
win = MyWindow()
Gtk.main()