Python clist 小部件不返回预期列表,仅返回每个项目的第一个字符

标签 python list widget pygtk clist

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

我现在正尝试将我的代码迁移到我在 Google 上找到的列表小部件示例中。除了删除一些不需要的按钮外,我所做的只是整合我的代码的顶部部分,它部分工作,除了它只返回每个文件和子目录的第一个字符。所以我期望这样:

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

最佳答案

当你通过append方法向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 实例时,您将列数传递给构造函数。在您的示例中,您使用一个列创建了 CList,这就是为什么您只能在 append 方法中看到传递序列的第一个元素(第一个字符)。

关于Python clist 小部件不返回预期列表,仅返回每个项目的第一个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6801370/

相关文章:

qt - 如何在 QLabel 中调整图像大小

python - 在 Azure 中使用 Python 调整 Excel 文件工作表列的大小

python - pip 如何删除带有前导破折号 : "-pkgname" 的错误安装包

python - 访问嵌套字典 panda 中的单元格的最佳方式是什么?

list - 跳过列表元素的列表

python - 从列表中删除特定项目但保留一些在 python 中

python - 以奇怪的方式对 Pandas 数据框进行排序和分组

jquery - 如果可能的话,如何使用 jquery 和/或 bootstrap 制作可切换的 div 以进行多种选择?

python - 使用 python 将图像上传到 azure blob 存储

android - 如何通过按下应用程序小部件来启动 IntentService