简体   繁体   中英

trouble setting up GtkTreeViews in PyGtk

I've got some code in a class that extends gtk.TreeView , and this is the init method. I want to create a tree view that has 3 columns. A toggle button, a label, and a drop down box that the user can type stuff into. The code below works, except that the toggle button doesn't react to mouse clicks and the label and the ComboEntry aren't drawn. (So I guess you can say it doesn't work). I can add rows just fine however.

    #make storage                   enable/disable  label    user entry
    self.tv_store = gtk.TreeStore(gtk.ToggleButton, str, gtk.ComboBoxEntry)
    #make widget
    gtk.TreeView.__init__(self, self.tv_store)
    #make renderers
    self.buttonRenderer = gtk.CellRendererToggle()
    self.labelRenderer = gtk.CellRendererText()
    self.entryRenderer = gtk.CellRendererCombo()
    #make columns

    self.columnButton = gtk.TreeViewColumn('Enabled')
    self.columnButton.pack_start(self.buttonRenderer, False)
    self.columnLabel = gtk.TreeViewColumn('Label')
    self.columnLabel.pack_start(self.labelRenderer, False)
    self.columnEntry = gtk.TreeViewColumn('Data')
    self.columnEntry.pack_start(self.entryRenderer, True)

    self.append_column(self.columnButton)
    self.append_column(self.columnLabel)
    self.append_column(self.columnEntry)

    self.tmpButton = gtk.ToggleButton('example')
    self.tmpCombo = gtk.ComboBoxEntry(None)
    self.tv_store.insert(None, 0, [self.tmpButton, 'example label', self.tmpCombo])

First of all, you need to create a model with bool , str and str columns, not the way you are doing now. Second, you need to bind properties of renderers from appropriate model columns, eg as in

self.columnButton = \
    gtk.TreeViewColumn ('Enabled', self.buttonRenderer, 
                        active = 0)  # 0 is the tree store column index

Then you need to set editable property on the renderer to True . And finally, you need to handle signals ( changed or editing-done , depending on renderer type) yourself and update the store accordingly.

It may be easier to use some helpers, eg Py-gtktree — there's even an example for editing a tree there.

Just connnect the toggled signal in the gtk.CellRendererToggle, when you click on it, it will emit that signal, then in your callback change the value in the model.

ej.

def toggle(self, cellrenderer, path):
        Self.model[path][column] = not self.model[path][column]

self.model is the model asociated to the treeview,

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