2014-06-12 30 views
1

在經歷了一些令人沮喪的工作之後,我期待着簡單一點(就是在GTK-2中),這就是我的問題。對不起,在這個問題缺乏代碼或細節,因爲我什麼都沒有工作。Glade,python,GTK3:數據列表視圖

我正在寫一個應用程序,它從數據庫中提取一些數據並且必須以表格形式呈現它。標準的東西,我會說。我只是無法弄清楚如何去做。沒有教程(和那些在那裏,不爲我工作,因爲我的窗口不僅僅是一個ListStore)。我正在設計我的用戶界面,它有一個帶有各種東西的網格的筆記本,包括列表應該出現的地方。

我已經嘗試添加一個ListStore對象,但無法讓它顯示在所有。 Python 2.7.6,Glade 3.16.1。

self.liststore = self.builder.get_object('liststore1') 
    self.liststore.append(['1st column','2nd column']) 

這應該顯示的數據,它沒有。我無法讓Glade中的ListStore事物顯示爲預覽,只能將其添加爲頂層對象,而不是它應該去的地方。

+0

您是否設置了綁定到ListStore的TreeView? TreeView和ListStore的glade XML配對片斷將會很有幫助。 –

+0

我不知道我在做什麼 - 嘗試了太多!在這方面完全沒有任何類似的教程是問題的一部分。我終於有東西來顯示一個listview - 通過修改其中一個教程中的一段代碼 - 但它是hacky,不使用glade,似乎無法更新其中的數據......我幾乎總共如何做一些基本的事情,如顯示項目列表的損失。 – Wouter

+0

基本上希望有一個完整的工作示例! – Wouter

回答

0

這是一個非常基本的例子,只顯示一列有兩個條目。其中一人在林間空地文件中創建,使用python創建另一個所以你可以看到如何修改liststore:

的空地文件:

<?xml version="1.0" encoding="UTF-8"?> 
<!-- Generated with glade 3.16.1 --> 
<interface> 
    <requires lib="gtk+" version="3.10"/> 
    <object class="GtkListStore" id="liststore1"> 
    <columns> 
     <!-- column-name test --> 
     <column type="gchararray"/> 
    </columns> 
    <data> 
     <row> 
     <col id="0" translatable="yes">entry</col> 
     </row> 
    </data> 
    </object> 
    <object class="GtkWindow" id="window1"> 
    <property name="can_focus">False</property> 
    <property name="default_width">247</property> 
    <property name="default_height">188</property> 
    <child> 
     <object class="GtkTreeView" id="treeview1"> 
     <property name="visible">True</property> 
     <property name="can_focus">True</property> 
     <property name="model">liststore1</property> 
     <child internal-child="selection"> 
      <object class="GtkTreeSelection" id="treeview-selection1"/> 
     </child> 
     <child> 
      <object class="GtkTreeViewColumn" id="treeviewcolumn1"> 
      <property name="title" translatable="yes">test-column</property> 
      <child> 
       <object class="GtkCellRendererText" id="cellrenderertext1"/> 
       <attributes> 
       <attribute name="text">0</attribute> 
       </attributes> 
      </child> 
      </object> 
     </child> 
     </object> 
    </child> 
    </object> 
</interface> 

這是Python文件:

from gi.repository import Gtk 

class Test: 
    def __init__(self): 
     builder = Gtk.Builder() 
     builder.add_from_file('test.glade') 

     self.liststore = builder.get_object('liststore1') 
     #append something with python: 
     self.liststore.append(('stackoverflow',)) 

     window = builder.get_object('window1') 
     window.connect('delete-event', Gtk.main_quit) 
     window.show_all() 

if __name__ == '__main__': 
    Test() 
    Gtk.main()