Python clist widget ne pas retourner la liste attendue, ne retourne que le premier caractère de chaque élément

StackOverflow https://stackoverflow.com/questions/6801370

Question

J'ai écrit un programme simple d'imprimer tous les fichiers non cachés et sous-répertoires dans un répertoire donné.

Je suis en train de migrer maintenant mon code dans un widget exemple clist j'ai trouvé sur Google. Autre que déchirer quelques boutons inutiles, tout ce que je changé était la partie supérieure pour intégrer mon code, et il fonctionne partiellement, sauf qu'il ne retourne que le premier caractère de chaque fichier et sous-répertoire. Donc, je m'y attendais:

Desktop
Downloads
Scripts
textfile.txt
pron.avi

Mais au lieu a ceci:

D
D
S
t
p

Voici l'exemple avec le code que j'ai changé (vraiment juste la première définition)

import gtk, os

class CListExample:
    # this is the part Thraspic changed (other than safe deletions)
    # User clicked the "Add List" button.
    def button_add_clicked(self, data):
        dirList=os.listdir("/usr/bin")
        for item in dirList: 
           if item[0] != '.':
              data.append(item)
        data.sort()
        return


    def __init__(self):
        self.flag = 0
        window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        window.set_size_request(250,150)

        window.set_title("GtkCList Example")
        window.connect("destroy", gtk.mainquit)

        vbox = gtk.VBox(gtk.FALSE, 5)
        vbox.set_border_width(0)
        window.add(vbox)
        vbox.show()

        scrolled_window = gtk.ScrolledWindow()
        scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_ALWAYS)

        vbox.pack_start(scrolled_window, gtk.TRUE, gtk.TRUE, 0)
        scrolled_window.show()

        clist = gtk.CList(1)

        # What however is important, is that we set the column widths as
        # they will never be right otherwise. Note that the columns are
        # numbered from 0 and up (to an anynumber of columns).
        clist.set_column_width(0, 150)

        # Add the CList widget to the vertical box and show it.
        scrolled_window.add(clist)
        clist.show()

        hbox = gtk.HBox(gtk.FALSE, 0)
        vbox.pack_start(hbox, gtk.FALSE, gtk.TRUE, 0)
        hbox.show()
        button_add = gtk.Button("Add List")
        hbox.pack_start(button_add, gtk.TRUE, gtk.TRUE, 0)

        # Connect our callbacks to the three buttons
        button_add.connect_object("clicked", self.button_add_clicked,
clist)

        button_add.show()

        # The interface is completely set up so we show the window and
        # enter the gtk_main loop.
        window.show()

def main():
    gtk.mainloop()
    return 0

if __name__ == "__main__":
    CListExample()
    main()
Était-ce utile?

La solution

Lorsque vous ajoutez des données à CList par la méthode append, vous devez passer une séquence. Ressaisissez votre code:

def button_add_clicked(self, data):
    dirList = os.listdir("/usr/bin")
    for item in dirList: 
       if not item.startswith('.'):
          data.append([item])
    data.sort()

Lorsque vous créant par exemple CList vous passe au numéro de constructeur de collumns. Dans votre exemple, vous avez créé CList avec un collumn, c'est pourquoi vous pouvez voir seulement le premier élément (premier caractère) de la séquence passée dans la méthode append.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top