开发者

Conditional CellRenderCombo in pyGTK TreeView

开发者 https://www.devze.com 2022-12-28 03:36 出处:网络
I have a two column TreeView attached to a ListStore.Both columns are CellRenderCombo combo boxes. When the user selects an entry in the first box, I need to dynamically load a set of options in the

I have a two column TreeView attached to a ListStore. Both columns are CellRenderCombo combo boxes.

When the user selects an entry in the first box, I need to dynamically load a set of options in the second.

For example, the behavior I want is:

On row 0, the user selects "Alphabet" in the first col开发者_如何学编程umn box.
     The second column box is populated with the letters "A-Z".
On row 1, the user selects "Numbers" in the first column box.
     The second column box is populated with the numbers "0-9".
On row 2, the user selects "Alphabet" in the first column box.
     The second column box is populated with the letters "A-Z".
etc.

Does anyone know how to do this, or seen any open source pygtk or gtk projects that have similar behavior which I can analyze?


First, you need to bind model property of the second renderer from the model, like:

gtk.TreeViewColumn ('...', gtk.CellRendererCombo (), text = N, model = M)

where M is the column number which stores models (likely gtk.ListStore). Or use any other method of binding properties from model columns.

Then connect to the first renderer's changed signal. In the callback you need to change the model used for the second renderer's combo (i.e. value in column M) accordingly. I'd bet you can use the same models in different rows, i.e. one for numbers, one for letters, without creating more, but I'm not sure. In other words, callback could look similar to this (store is the main gtk.ListStore, X is the column with the value of the first combo):

def combo1_changed (combo, path, iter):
    main_iter = store.get_iter (path)
    selected  = store.get_value (main_iter, X)
    if selected == 'Alphabet':
        store.set_value (main_iter, M, alphabet_list_store)
    elif selected == 'Numbers':
        store.set_value (main_iter, M, number_list_store)
    ...
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号