2016-11-25 58 views
0

在GTK/Python中,我試圖構建一個帶節點的接口。GTK如何爲1創建3個不同的過濾器Liststore

這是photo of my interface

ChronoMap

我創建一個liststore,我想篩選不同的東西。用戶必須執行兩個操作,首先,他必須在組合框中選擇他想要使用的過濾器類型,如果他想按節點類型,節點的開始名稱或其他名稱進行過濾。

然後在文本輸入中,他決定他想看到什麼信息。以節點類型爲例。有三種不同類型的節點,節點類型1,節點類型2和節點類型3.作爲用戶我只想看到節點類型1,所以我在節點條目中寫入1。其實我有一個問題,這是我的過濾器不起作用。

我首先創建2個liststores:

def create_liststore(self): 

    if len(self.FdessinCarto.pos) != 0: 
       for i,node in enumerate(self.FdessinCarto.pos): 
        self.node_liststore.append([str(node.title),self.controller.model.string_from_numdate(int(node.start_time)),self.controller.model.string_from_numdate(int(node.end_time)),str(node.node_group),str(node.description),str(node.attachment_list)]) 

    self.edgelist = self.controller.get_edge_list() 
    if len(self.edgelist) !=0: 
     for i in self.edgelist: 
      edge_prop=self.controller.edge_data(i[0],i[1]) 
      self.edge_liststore.append([edge_prop['label'],str(i[0].title),str(i[1].title),edge_prop['description'],edge_prop['attachment_list']])   

    #creating the treeview for Node, making it use the filter as a model, and adding the columns 
    self.treeviewNode = Gtk.TreeView.new_with_model(self.node_liststore) 
    for i, column_title in enumerate(["Name", "Beginning date", "End date", "Type of node", "Description of node","files"]): 
     self.Noderenderer = Gtk.CellRendererText() 
     self.Noderenderer.set_property("editable", True) 
     column = Gtk.TreeViewColumn(column_title, self.Noderenderer, text=i) 
     column.set_sort_column_id(0) 
     self.treeviewNode.append_column(column) 


    #creating the treeview for edge 
    self.treeviewEdge = Gtk.TreeView.new_with_model(self.edge_liststore) 
    for i, column_title in enumerate(["Name", "Node 1", "Node 2", "Description of edge","file"]): 
     self.Edgerenderer = Gtk.CellRendererText() 
     self.Edgerenderer.set_property("editable", True) 
     column = Gtk.TreeViewColumn(column_title, self.Edgerenderer, text=i) 
     column.set_sort_column_id(0) 
     self.treeviewEdge.append_column(column) 

    self.SWViewListStore.add(self.treeviewNode) 
    self.SWEdgeStore.add(self.treeviewEdge) 
    self.SWViewListStore.show_all() 
    self.SWEdgeStore.show_all() 

有我的3個不同的過濾器:

#creating the filtre 
    self.node_beginning_date_filter = self.node_liststore.filter_new() 
    self.node_end_date_filter = self.node_liststore.filter_new() 
    self.node_type_filter = self.node_liststore.filter_new() 


    #setting the filter function, note that we're not using the 
    self.node_end_date_filter.set_visible_func(self.node_end_date_filter_func) 
    self.node_beginning_date_filter.set_visible_func(self.node_beginning_date_filter_func) 
    self.node_type_filter.set_visible_func(self.node_type_filter_func) 

有一次,我改變我的組合框,它會激活我的功能,它採取的類型combofilter隨後還組合框的文本:

def on_entryComboBox_changed(self,widget): 
     textComboFilter = self.View.combo_filter.get_active_text() 
     print("textComboFilter %s" %textComboFilter) 
     if textComboFilter == "Filter by node's beginning date": 
      #print("%s language selected!" % textComboFilter) 
      self.View.current_filter = textComboFilter 
      self.View.node_beginning_date_filter.refilter() 

     if textComboFilter == "Filter by node's end date": 
      #print("%s language selected!" % textComboFilter) 
      self.View.current_filter = textComboFilter 
      self.View.node_end_date_filter.refilter() 

     if textComboFilter == "Filter by type of node": 
      #print("%s language selected!" % textComboFilter) 
      self.View.current_filter = textComboFilter 
      self.View.node_type_filter.refilter() 

而且這是行不通的。

+0

你的問題很難馬上給出答案,因爲有缺失,我們不知道它是做了很多的代碼。例如,我不確定'self.View'是什麼類型,它的'current_filter'屬性是什麼。你能否做出一個簡短的,獨立的例子來說明問題?這甚至可以幫助你自己解決方案... – ptomato

+0

謝謝你,我會做到這一點。 –

+1

最後,由於我創建了一個小例子,我設法弄清楚了這個問題。我應該創建1個過濾器而不是3個。然後我應該使用字符串作爲輸入而不是int。 –

回答

1

最後,

我設法自己回答。

你應該建立一個過濾器而不是三個。對於有同樣問題的人來說,這是一個很好的例子,可以幫助我解決問題。

Picture of this program

import gi 
    gi.require_version('Gtk', '3.0') 
    from gi.repository import Gtk 

    #list of tuples for each software, containing the software name, initial release, and main programming languages used 
    software_list = [("Firefox", 2002, "C++"), 
      ("Eclipse", 2004, "Java"), 
      ("Pitivi", 2004, "Python"), 
      ("Netbeans", 1996, "Java"), 
      ("Chrome", 2008, "C++"), 
      ("Filezilla", 2001, "C++"), 
      ("Bazaar", 2005, "Python"), 
      ("Git", 2005, "C"), 
      ("Linux Kernel", 1991, "C"), 
      ("GCC", 1987, "C"), 
      ("Frostwire", 2004, "Java")] 

    class TreeViewFilterWindow(Gtk.Window): 

     def __init__(self): 
      Gtk.Window.__init__(self, title="Treeview Filter Demo") 
      self.set_border_width(10) 

    #Setting up the self.grid in which the elements are to be positionned 
      self.grid = Gtk.Grid() 
      self.grid.set_column_homogeneous(True) 
      self.grid.set_row_homogeneous(True) 
      self.add(self.grid) 

      #Creating the ListStore model 
      self.software_liststore = Gtk.ListStore(str, int, str) 
      for software_ref in software_list: 
       self.software_liststore.append(list(software_ref)) 
       self.current_filter_language = None 

    #Creating the filter, feeding it with the liststore model 
      self.language_filter = self.software_liststore.filter_new() 
    #setting the filter function, note that we're not using the 
      self.language_filter.set_visible_func(self.language_filter_func) 

    #creating the treeview, making it use the filter as a model, and adding the columns 
      self.treeview = Gtk.TreeView.new_with_model(self.language_filter) 
      for i, column_title in enumerate(["Software", "Release Year", "Programming Language"]): 
       renderer = Gtk.CellRendererText() 
       column = Gtk.TreeViewColumn(column_title, renderer, text=i) 
       self.treeview.append_column(column) 

    #creating buttons to filter by programming language, and setting up their events 
      self.buttons = list() 
      for prog_language in ["Java", "C", "C++", "Python", "None"]: 
       button = Gtk.Button(prog_language) 
       self.buttons.append(button) 
       button.connect("clicked", self.on_selection_button_clicked) 

    #setting up the layout, putting the treeview in a scrollwindow, and the buttons in a row 
      self.scrollable_treelist = Gtk.ScrolledWindow() 
      self.scrollable_treelist.set_vexpand(True) 
      self.grid.attach(self.scrollable_treelist, 0, 0, 8, 10) 
      self.grid.attach_next_to(self.buttons[0], self.scrollable_treelist, Gtk.PositionType.BOTTOM, 1, 1) 
      for i, button in enumerate(self.buttons[1:]): 
       self.grid.attach_next_to(button, self.buttons[i], Gtk.PositionType.RIGHT, 1, 1) 
      self.scrollable_treelist.add(self.treeview) 

      self.show_all() 

     def language_filter_func(self, model, iter, data): 
    """Tests if the language in the row is the one in the filter""" 
      if self.current_filter_language is None or self.current_filter_language == "None": 
       return True 
      else: 
       return model[iter][2] == self.current_filter_language 

     def on_selection_button_clicked(self, widget): 
    """Called on any of the button clicks""" 
    #we set the current language filter to the button's label 
      self.current_filter_language = widget.get_label() 
      print("%s language selected!" % self.current_filter_language) 
    #we update the filter, which updates in turn the view 
      self.language_filter.refilter() 


    win = TreeViewFilterWindow() 
    win.connect("delete-event", Gtk.main_quit) 
    win.show_all() 
    Gtk.main() 
相關問題