简体   繁体   中英

Glade, python, GTK3: list view for data

After some frustrating hours doing something I expect to be simple (it was, in GTK-2) hereby my question. Sorry for lack of code or specifics in this question, as I have NOTHING at all working.

I'm writing an application that pulls some data from a database and has to present it in a table form. Standard stuff, I'd say. I just can't figure out how to do it. No tutorials (and those that are there, don't work for me, as I have more than just a ListStore in my window). I'm designing my UI in Glade, it's got a notebook with a grid in it with various stuff, including a place where the list should come.

I've tried adding a ListStore object, but can't get it to display at all. Python 2.7.6, Glade 3.16.1.

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

This is supposed to show the data, it doesn't. I can't get the ListStore thing in Glade to show up as preview, can only add it as toplevel object and not where it's supposed to go.

This is a very basic example that only shows one column with two entries. One of them created in the glade file, the other one created with python so you can see how to modify the liststore:

The glade file:

<?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>

And this is the python file:

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM