IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

GTK+ avec Python Discussion :

[TreeView]CellRendererCombo, comment ça marche ?


Sujet :

GTK+ avec Python

  1. #1
    Membre émérite
    Homme Profil pro
    heu...
    Inscrit en
    Octobre 2007
    Messages
    648
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : heu...

    Informations forums :
    Inscription : Octobre 2007
    Messages : 648
    Par défaut [TreeView]CellRendererCombo, comment ça marche ?
    Salut, voilà ça fait maintenant trois heures que j'essayes différentes choses pour faire fonctionner un CellRendererCombo, et j'y suis presque, mais il me reste à afficher le string que l'on à choisi depuis la combobox...

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    import pygtk,os; pygtk.require('2.0')
    import gtk
     
    ICONS=dict([(name,gtk.image_new_from_file(name)) for name in os.listdir('./') if name[-4:]=='.ico'])
     
    class Window(gtk.Window):
        def __init__(s):
            gtk.Window.__init__(s)
            s.set_title('Basic TreeView Example')
            s.set_size_request(200,200)
            s.connect('delete_event', s.quit)
            s.ts = ListStore()
            s.tv = TreeView(s.ts)
            s.add(s.tv)
            s.show_all()
        def quit(s,w,e,data=None):
            gtk.main_quit()
        def main(s):
            gtk.main()
     
    class ListStore(gtk.ListStore):
        def __init__(s):
            gtk.ListStore.__init__(s,str,gtk.ListStore)
            s.cbmodel=gtk.ListStore(str)
            #populate the ComboBoxModel
            for key in ICONS:
                s.cbmodel.append([key])
            #populate the instance
            for x in range(4):
                piter = s.append(['%s'%x,s.cbmodel])
     
    class TreeView(gtk.TreeView):
        def __init__(s,model):
            gtk.TreeView.__init__(s,model)
            s.columns = [gtk.TreeViewColumn('idx'),gtk.TreeViewColumn('icons')]
            s.cells   = [[gtk.CellRendererText()], [gtk.CellRendererCombo()]]
            for idx,col in enumerate(s.columns) :
                s.append_column(col)
                for cell in s.cells[idx]:
                    col.pack_start(cell,True)
            s.columns[0].add_attribute(s.cells[0][0], 'text', 0)
            s.cells[1][0].set_property('model',model.cbmodel)
            s.cells[1][0].set_property('text-column',0)
            s.cells[1][0].set_property('has-entry',False)
            s.cells[1][0].set_property('editable',True)
            s.set_search_column(0)
            s.columns[0].set_sort_column_id(0)
     
    if __name__=='__main__':
        app=Window()
        app.main()
    J'ai donc un question : Mais comment qu'c'est qu'on fait pour afficher le string qu'on a choisi depuis la combobox ? (cause là la combo se casse et plus rien...)

    PS: pour teste mon code de suite (et sans obtenir une combo vide) : TreeView_ComboBox_test.zip

  2. #2
    Membre émérite
    Homme Profil pro
    heu...
    Inscrit en
    Octobre 2007
    Messages
    648
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : heu...

    Informations forums :
    Inscription : Octobre 2007
    Messages : 648
    Par défaut
    Ok, bon ben j'ai fini par trouver à force de tatoner .

    Pour ceux que ça pourrait intéresser, voici le code:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    import pygtk,os; pygtk.require('2.0')
    import gtk
     
    ICONS=dict([(name,gtk.image_new_from_file(name)) for name in os.listdir('./') if name[-4:]=='.ico'])
     
    class Window(gtk.Window):
        def __init__(s):
            gtk.Window.__init__(s)
            s.set_title('Basic TreeView Example')
            s.set_size_request(200,200)
            s.connect('delete_event', s.quit)
            s.ts = ListStore()
            s.tv = TreeView(s.ts)
            s.add(s.tv)
            s.show_all()
        def quit(s,w,e,data=None):
            gtk.main_quit()
        def main(s):
            gtk.main()
     
    class ListStore(gtk.ListStore):
        def __init__(s):
            gtk.ListStore.__init__(s,str, gtk.gdk.Pixbuf, str, gtk.ListStore)
            s.cbmodel=gtk.ListStore(str)
            #populate the ComboBoxModel
            first=None
            for key in ICONS:
                if first is None : first=key
                s.cbmodel.append([key])
            #populate the instance
            for x in range(4):
                piter = s.append(['%s'%x,ICONS[first].get_pixbuf(),first,s.cbmodel])
     
    class TreeView(gtk.TreeView):
        def __init__(s,model):
            gtk.TreeView.__init__(s,model)
            s.model   = model
            s.columns = [gtk.TreeViewColumn('idx'),gtk.TreeViewColumn('Icon'),gtk.TreeViewColumn('icons')]
            s.cells   = [[gtk.CellRendererText()],[gtk.CellRendererPixbuf()], [gtk.CellRendererCombo()]]
            for idx,col in enumerate(s.columns) :
                s.append_column(col)
                for cell in s.cells[idx]:
                    col.pack_start(cell,True)
            s.columns[0].add_attribute(s.cells[0][0], 'text', 0)
            s.columns[1].add_attribute(s.cells[1][0],'pixbuf', 1)
            s.columns[2].add_attribute(s.cells[2][0],'text', 2)
            s.cells[2][0].connect('changed',s.on_changed)
            s.cells[2][0].set_property('model',model.cbmodel)
            s.cells[2][0].set_property('text-column',0)
            s.cells[2][0].set_property('has-entry',False)
            s.cells[2][0].set_property('editable',True)
            s.set_search_column(0)
            s.columns[0].set_sort_column_id(0)
            s.ITER = None
        def on_changed(s, combo, path_string, new_iter, *data):
            print 'changed occured %s'%str((combo, path_string, new_iter, data))
            print s.get_selection()
            model,iter=s.get_selection().get_selected()
            model.set_value(iter,2,s.model.cbmodel.get_value(new_iter,0))
            model.set_value(iter,1,ICONS[s.model.cbmodel.get_value(new_iter,0)].get_pixbuf())
     
     
    if __name__=='__main__':
        app=Window()
        app.main()

  3. #3
    Membre émérite
    Homme Profil pro
    heu...
    Inscrit en
    Octobre 2007
    Messages
    648
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : heu...

    Informations forums :
    Inscription : Octobre 2007
    Messages : 648
    Par défaut
    Et voici le version final du script de test, sans données inutiles dans le ListStore, et un meilleur comportement de la combo :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    import pygtk,os; pygtk.require('2.0')
    import gtk
     
    ICONS=dict([(name,gtk.image_new_from_file(name)) for name in os.listdir('./') if name[-4:]=='.ico'])
     
    class Window(gtk.Window):
        def __init__(s):
            gtk.Window.__init__(s)
            s.set_title('Basic TreeView Example')
            s.set_size_request(200,200)
            s.connect('delete_event', s.quit)
            s.ts = ListStore()
            s.tv = TreeView(s.ts)
            s.add(s.tv)
            s.show_all()
        def quit(s,w,e,data=None):
            gtk.main_quit()
        def main(s):
            gtk.main()
     
    class ListStore(gtk.ListStore):
        def __init__(s):
            gtk.ListStore.__init__(s,int, str,str)
            s.cbmodel = gtk.ListStore(str)
            #populate the ComboBoxModel
            first     = None
            for key in ICONS:
                if first is None : first=key
                s.cbmodel.append([key])
            #populate the instance
            for x in range(4):
                piter = s.append([x,first,'note'])
     
    class TreeView(gtk.TreeView):
        def __init__(s,model):
            gtk.TreeView.__init__(s,model)
            s.model         = model
            s.combo_model   = model.cbmodel
            s.spin_adj      = gtk.Adjustment(1, 1, 100, 1)
            #s.spin_adj.connect('value_changed',s.on_adj_changed)
            s.columns = [gtk.TreeViewColumn('idx'),gtk.TreeViewColumn('Icon'),gtk.TreeViewColumn('icons'),gtk.TreeViewColumn('Note')]
            s.cells   = [[gtk.CellRendererSpin()],[gtk.CellRendererPixbuf()], [gtk.CellRendererCombo()],[gtk.CellRendererText()]]
            for idx,col in enumerate(s.columns) :
                s.append_column(col)
                for cell in s.cells[idx]:
                    col.pack_start(cell,True)
            s.columns[1].set_cell_data_func ( s.cells[1][0] , s.data_pixbuf_func)
            s.columns[0].add_attribute ( s.cells[0][0] , 'text' , 0      )
            s.columns[2].add_attribute ( s.cells[2][0] , 'text' , 1      )
            s.columns[3].add_attribute ( s.cells[3][0] , 'text' , 2      )
            s.cells[0][0].set_property ( 'adjustment'  , s.spin_adj      )
            s.cells[0][0].set_property ( 'climb-rate'  , 0               )
            s.cells[0][0].set_property ( 'editable'    , True            )
            s.cells[2][0].set_property ( 'model'       , model.cbmodel   )
            s.cells[2][0].set_property ( 'text-column' , 0               )
            s.cells[2][0].set_property ( 'has-entry'   , False           )
            s.cells[2][0].set_property ( 'editable'    , True            )
            s.cells[3][0].set_property ( 'editable'    , True            )
            s.cells[0][0].connect ( 'edited'  , s.on_edited     )
            #s.cells[2][0].connect ( 'changed' , s.on_changed    )
            s.cells[2][0].connect ( 'edited'  , s.on_txt_edited , 1 )
            s.cells[3][0].connect ( 'edited'  , s.on_txt_edited , 2)
            s.set_search_column(0)
            s.columns[0].set_sort_column_id(0)
            #s.ITER = None
        def data_pixbuf_func(s,col,cell,model,iter,data=None):
            icon_name = model.get_value(iter,1)
            cell.set_property('pixbuf', ICONS[icon_name].get_pixbuf())
        def on_edited(s, cell, path, new_text, *data):
            print 'edited',new_text, type(new_text)
            model,iter=s.get_selection().get_selected()
            model.set_value(iter,0, int(new_text))#str(int(s.spin_adj.get_value())))
            model.append([1,'pencil.ico','note'])
        def on_txt_edited(s, cell, path, new_text, *data):
            print 'txt edited'
            model,iter=s.get_selection().get_selected()
            if data[0]==2: model.remove(iter)
            else : model.set_value(iter,data[0], new_text)
     
    if __name__=='__main__':
        app=Window()
        app.main()

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. TreeView - Comment ça marche ?
    Par Kinesra dans le forum Débuter
    Réponses: 3
    Dernier message: 24/10/2011, 18h20
  2. Virtual Treeview Demo Comment ca marche ?
    Par deetox dans le forum Composants VCL
    Réponses: 2
    Dernier message: 18/02/2006, 12h59
  3. [SYNEDIT] -> Comment ça marche ?
    Par MaTHieU_ dans le forum C++Builder
    Réponses: 2
    Dernier message: 18/01/2004, 19h11
  4. [TP][Turbo Vision] comment ça marche ??
    Par Costello dans le forum Turbo Pascal
    Réponses: 7
    Dernier message: 05/08/2003, 00h24
  5. [update][req. imbriquee] Comment ca marche ??
    Par terziann dans le forum Langage SQL
    Réponses: 3
    Dernier message: 11/07/2003, 12h51

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo