我編寫了一個簡單的程序來打印出給定目錄中的所有非隱藏文件和子目錄。Python clist小部件不返回預期列表,僅返回每個項目的第一個字符
我現在正試圖將我的代碼遷移到我在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()
歡迎來到SO,+1爲pron.avi –
如果您在方法的頂部打印數據,您會得到什麼?在那之後的'打印dirlist'?循環頂部的「print item」?給我們一些調試信息。 – agf
請注意'gtk.CList'自從GTK 2.0被棄用,並且完全從GTK 3.0中移除。您應該使用'gtk.TreeView'來代替。 – ptomato