我编写了一个简单的程序,以打印给定目录中的所有非隐藏文件和子目录。

现在,我正在尝试将代码迁移到我在Google上找到的Clist小部件示例。除了撕掉一些不需要的按钮之外,我更改的只是集成我的代码的最高部分,它部分工作起来,除了它仅返回每个文件和子目录的第一个字符。所以我希望这一点:

Desktop
Downloads
Scripts
textfile.txt
pron.avi

而是得到了:

D
D
S
t
p

这是我更改的代码的示例(实际上只是第一个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()

        # 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()
有帮助吗?

解决方案

当您通过附加方法添加数据为CLIST时,必须传递序列。重写您的代码:

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

当您创建Clist实例时,您将传递给collumns的构造函数。在您的示例中,您用一个collumn创建了Clist,这就是为什么您只能在附加方法中看到传递序列的第一个元素(第一个字符)。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top