2011-07-23 9 views
3

私は、特定のディレクトリ内の非隠しファイルとサブディレクトリをすべて印刷する簡単なプログラムを作成しました。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() 
+1

so.soへの+1、pron.aviの+1 –

+0

メソッドの最上部に 'print data 'と表示されたらどうなりますか?それの後に 'print dirlist'という行がありますか?ループの一番上にある 'print item'ですか?いくつかのデバッグ情報をお知らせください。 – agf

+2

'gtk.CList'はGTK 2.0以降で廃止され、GTK 3.0から完全に削除されています。代わりに 'gtk.TreeView'を使うべきです。 – ptomato

答えて

2

と例です。 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インスタンスを作成すると、コンストラクタの数のcollumnに渡されます。あなたの例では、1つの欄でCListを作成したので、appendメソッドで渡されたシーケンスの最初の要素(最初の文字)しか見ることができません。

+1

ありがとう、simplylizzのソリューションが動作し、私は完全にオフベースではなかったのでうれしいです。 agf:チップをありがとう、私は将来デフォルトでデバッグ情報を投稿します。 –

関連する問題