使用python

时间:2018-11-06 22:29:28

标签: python gtk3

im试图在GTK 3树存储中搜索字符串。树存储区有4列,用于具有可调用节点的Treeview小部件。我用这个功能创建节点:

def AddItem(self,ParentIter,txt,datapath='',projName=Project):         
    self.store = self.builder.get_object('theTreeStore')
    NodeId = secrets.token_hex(8) 

    if ParentIter == None:
        ParentNodeId = ''
    else:
        ParentNodeId = self.store.get_value(ParentIter, 2) 
    treeEntry = ['%s' %ParentNodeId,'%s' %txt,'%s' %NodeId,'%s' %datapath] 
    node = self.store.append(ParentIter, treeEntry) <<<<<<<<<<<<<
    self.view = self.builder.get_object('Tree')
    self.view.set_model(self.store)

    # table nodes(tParentNodeID ,tNodeTxt ,tNodeID ,tDataPath );



    sql = "INSERT INTO %s (tParentNodeID ,tNodeTxt ,tNodeID ,tDataPath ) VALUES ('%s','%s','%s','%s')" %(projName,ParentNodeId,txt,NodeId,datapath)
    self.cursor.execute(sql)
    self.mariadb_connection.commit()
    for x in self.cursor:
        print(x)
    return(node)

您会看到树中的数据嵌套在其父级中。

现在我需要以某种方式在树存储中搜索包含某个NodeId字符串的行。我已经一遍又一遍地阅读了gtk文档,但是我不知道该怎么做。我想我需要使用以下方法:

store.get_iter() store.iter_children()

但是idk我尝试的所有操作都只返回根节点,没有子节点。

我基本上想要一个搜索功能,该功能将递归地搜索每个节点及其子节点以及它们的子节点以获取字符串。像这样的东西:

def GetRowbyNodeID(nodeid):
     for row in treestore:
       if row[1]==nodeid:
          return(row) 
       for children in row:        
         if children[1] == nodeid    
             return(children)

代码在多个文件中,如果需要,我可以发布任何相关功能。

2 个答案:

答案 0 :(得分:0)

GtkTreeStore实现GtkTreeModel接口。因此,您可以使用以下方法:

我还建议您阅读this tutorial。 “模型”部分包含迭代模型所需的全部内容(扰流板:搜索print_tree_store

答案 1 :(得分:0)

一切正常。再次感谢。我会发布相关代码,以防万一其他人可以使用它。

def SearchTreeRows(self,store, treeiter, searchstr):
    print("\nsearch>%s"%searchstr)
    while treeiter != None:

        if store[treeiter][2] ==searchstr:
            print("found in:%s"%str(store[treeiter][:]))
            return(treeiter)
            break
        print("searched:%s"%str(store[treeiter][:]))    
        if store.iter_has_child(treeiter):
            childiter = store.iter_children(treeiter)
            ret = self.SearchTreeRows(store, childiter, searchstr)
            if ret is not None:
                return ret

        treeiter = store.iter_next(treeiter)

def NodeId2Tree(self,nodeid):
    self.store = self.builder.get_object('theTreeStore')
    rootiter = self.store.get_iter_first()
    row = self.SearchTreeRows(self.store, rootiter,nodeid)
    return(row)

def LoadProject(self):

    global Project
    global ProjSel
    sql = "SHOW TABLES"
    self.cursor.execute(sql)

    tbls = []
    for x in self.cursor:
        tbls.append(x)   

    diag = self.builder.get_object('ProjectChooser')


    self.combo = Gtk.ComboBox()
    ls =Gtk.ListStore(str)
    for tble in tbls:
        strg ="%s" %tble
        ls.append(tble)

    self.combo.set_model(ls)
    cellr = Gtk.CellRendererText()
    self.combo.pack_start(cellr,True) 
    self.combo.add_attribute(cellr, 'text', 0) 
    diag.vbox.pack_start(self.combo, True, True, 5)
    diag.show_all()
    response = diag.run()
    self.combo.destroy()
    print(ProjSel) 
    Project = ProjSel
    ProjSel = ''
    view = self.builder.get_object('Tree')
    self.store.clear()
    view.set_model(self.store)

    sql = "SELECT tParentNodeId,tNodeTxt,tNodeId FROM %s"%(Project)
    self.cursor.execute(sql)



    for x in self.cursor:
        parid = x[0]
        nodtxt = x[1]
        nodid =x[2]
        if parid == '':
            treeEntry = ['%s' %parid,  '%s' %nodtxt,  '%s' %nodid,  '']
            node = self.store.append(None, treeEntry)                #root nodes
        else:
            treeEntry = ['%s' %parid,  '%s' %nodtxt,  '%s' %nodid,  '']
            n2id = self.NodeId2Tree(parid)
            node = self.store.append(n2id, treeEntry)
            print("got return:%s   For:%s"%(n2id,treeEntry[0]))



    view.set_model(self.store)
    #select * where parentid == none  >> get root nodes ???? or parse line by line