1from gi.repository import Gtk 2from gi.repository import Pango 3import sys 4 5list_of_dvd = [["The Usual Suspects"], 6 ["Gilda"], 7 ["The Godfather"], 8 ["Pulp Fiction"], 9 ["Once Upon a Time in the West"], 10 ["Rear Window"]] 11 12 13class MyWindow(Gtk.ApplicationWindow): 14 15 def __init__(self, app): 16 Gtk.Window.__init__(self, title="My DVDs", application=app) 17 self.set_default_size(250, 100) 18 self.set_border_width(10) 19 20 # the data are stored in the model 21 # create a liststore with one column 22 self.listmodel = Gtk.ListStore(str) 23 for i in range(len(list_of_dvd)): 24 self.listmodel.append(list_of_dvd[i]) 25 26 # a treeview to see the data stored in the model 27 view = Gtk.TreeView(model=self.listmodel) 28 29 # cellrenderer for the first column 30 cell = Gtk.CellRendererText() 31 # the first column is created 32 col = Gtk.TreeViewColumn("Title", cell, text=0) 33 # and it is appended to the treeview 34 view.append_column(col) 35 36 # when a row of the treeview is selected, it emits a signal 37 self.selection = view.get_selection() 38 self.selection.connect("changed", self.on_changed) 39 40 # the label we use to show the selection 41 self.label = Gtk.Label() 42 self.label.set_text("") 43 44 # a button to add new titles, connected to a callback function 45 self.button_add = Gtk.Button(label="Add") 46 self.button_add.connect("clicked", self.add_cb) 47 48 # an entry to enter titles 49 self.entry = Gtk.Entry() 50 51 # a button to remove titles, connected to a callback function 52 self.button_remove = Gtk.Button(label="Remove") 53 self.button_remove.connect("clicked", self.remove_cb) 54 55 # a button to remove all titles, connected to a callback function 56 self.button_remove_all = Gtk.Button(label="Remove All") 57 self.button_remove_all.connect("clicked", self.remove_all_cb) 58 59 # a grid to attach the widgets 60 grid = Gtk.Grid() 61 grid.attach(view, 0, 0, 4, 1) 62 grid.attach(self.label, 0, 1, 4, 1) 63 grid.attach(self.button_add, 0, 2, 1, 1) 64 grid.attach_next_to( 65 self.entry, self.button_add, Gtk.PositionType.RIGHT, 1, 1) 66 grid.attach_next_to( 67 self.button_remove, self.entry, Gtk.PositionType.RIGHT, 1, 1) 68 grid.attach_next_to( 69 self.button_remove_all, self.button_remove, Gtk.PositionType.RIGHT, 1, 1) 70 71 # add the grid to the window 72 self.add(grid) 73 74 def on_changed(self, selection): 75 # get the model and the iterator that points at the data in the model 76 (model, iter) = selection.get_selected() 77 # set the label to a new value depending on the selection, if there is 78 # one 79 if iter is not None: 80 self.label.set_text("\n %s" % (model[iter][0])) 81 else: 82 self.label.set_text("") 83 return True 84 85 # callback function for the "Add" button 86 def add_cb(self, button): 87 # append to the model the title that is in the entry 88 title = self.entry.get_text() 89 self.listmodel.append([title]) 90 # and print a message in the terminal 91 print("%s has been added" % (title)) 92 93 def remove_cb(self, button): 94 # if there is still an entry in the model 95 if len(self.listmodel) != 0: 96 # get the selection 97 (model, iter) = self.selection.get_selected() 98 # if there is a selection, print a message in the terminal 99 # and remove it from the model 100 if iter is not None: 101 print("%s has been removed" % (model[iter][0])) 102 self.listmodel.remove(iter) 103 # otherwise, ask the user to select something to remove 104 else: 105 print("Select a title to remove") 106 # else, if there are no entries in the model, print "Empty list" 107 # in the terminal 108 else: 109 print("Empty list") 110 111 def remove_all_cb(self, button): 112 # if there is still an entry in the model 113 if len(self.listmodel) != 0: 114 # remove all the entries in the model 115 for i in range(len(self.listmodel)): 116 iter = self.listmodel.get_iter(0) 117 self.listmodel.remove(iter) 118 # print a message in the terminal alerting that the model is empty 119 print("Empty list") 120 121 122class MyApplication(Gtk.Application): 123 124 def __init__(self): 125 Gtk.Application.__init__(self) 126 127 def do_activate(self): 128 win = MyWindow(self) 129 win.show_all() 130 131 def do_startup(self): 132 Gtk.Application.do_startup(self) 133 134app = MyApplication() 135exit_status = app.run(sys.argv) 136sys.exit(exit_status) 137