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()
解決
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インスタンスを作成すると、コンストラクター数のコマルン数に渡されます。例では、1つのCollumnでClistを作成したため、Append Methodで合格したシーケンスの最初の要素(最初の文字)のみが確認できます。
所属していません StackOverflow