TreeStore提供分等級,分層次的數(shù)據(jù)存儲,而ListStore提供表格的數(shù)據(jù)存儲,TreeModelSort提供一個排序的模型,TreeModelFilter提供數(shù)據(jù)子集。通常有以下幾個步驟:
1.創(chuàng)建一個tree model對象,通過ListStore或TreeStore
2.TreeView widget 創(chuàng)建并與tree model關(guān)聯(lián)
3.一個或多個TreeViewColumns被創(chuàng)建并插入到TreeView,每個代表一列
4.對于每個TreeViewColumn,CellRenderers被創(chuàng)建并加入TreeViewColumn
5.設(shè)置每個CellRenderer的屬性
6.TreeView被插入并顯示在Window或ScrolledWindow中
7.響應(yīng)用戶的操作
?
?
#!/usr/bin/env python # example basictreeview.py import pygtk pygtk.require('2.0') import gtk class BasicTreeViewExample: # close the window and quit def delete_event(self, widget, event, data=None): gtk.main_quit() return False def __init__(self): # Create a new window self.window = gtk.Window(gtk.WINDOW_TOPLEVEL) self.window.set_title("Basic TreeView Example") self.window.set_size_request(200, 200) self.window.connect("delete_event", self.delete_event) # create a TreeStore with one string column to use as the model self.treestore = gtk.TreeStore(str) # we'll add some data now - 4 rows with 3 child rows each for parent in range(4): piter = self.treestore.append(None, ['parent %i' % parent]) for child in range(3): self.treestore.append(piter, ['child %i of parent %i' %(child, parent)]) # create the TreeView using treestore self.treeview = gtk.TreeView(self.treestore) # create the TreeViewColumn to display the data self.tvcolumn = gtk.TreeViewColumn('Column 0') # add tvcolumn to treeview self.treeview.append_column(self.tvcolumn) # create a CellRendererText to render the data self.cell = gtk.CellRendererText() # add the cell to the tvcolumn and allow it to expand self.tvcolumn.pack_start(self.cell, True) # set the cell "text" attribute to column 0 - retrieve text # from that column in treestore self.tvcolumn.add_attribute(self.cell, 'text', 0) # make it searchable self.treeview.set_search_column(0) # Allow sorting on the column self.tvcolumn.set_sort_column_id(0) # Allow drag and drop reordering of rows self.treeview.set_reorderable(True) self.window.add(self.treeview) self.window.show_all() def main(): gtk.main() if __name__ == "__main__": tvexample = BasicTreeViewExample() main()
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
