开发者

Python clist widget not returning expected list, returns only the first character of each item

I wrote a simple program to print out all non-hidden files and subdirectories in a given directory.

I am now trying to migrate my code into a clist widget example I found on Google. Other than ripping out some unneeded buttons, all I changed was the top portion to integrate my code, and it partially works except that it only returns the first character of each file and subdirectory. So I expected this:

Desktop
Downloads
Scripts
textfile.txt
pron.avi

But instead got this:

D
D
S
t
p

Here is the example with the code I changed (really just the first def)

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

        #开发者_Go百科 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()


When you adding data to CList through append method, you must pass a sequence. Rewrite your 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()

When you creating CList instance you passes to the constructor number of collumns. In your example you created CList with one collumn, that's why you can see only first element (first character) of passed sequence in the append method.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜