リスト項目を追加する

ListBox の動作を確認するために、これを拡張した ExListBox を利用します。


win = Window(Title=__file__, Width=200, Height=150)
items = "red", "green", "blue"
win.Content = ExListBox(items)
Application().Run(win)

3つの項目 items を含む、リスト ExListBox を作成して、これをウィンドウのコンテンツ Content に保持させます。すると、指定したリスト項目が表示されます。


class ExListBox(ListBox):
def __init__(self, items):
for e in items:
self.Items.Add(e)
self.SelectionChanged += self.selectionChanged

複数のデータ項目を管理するのが、プロパティー Items です。これに、メソッド Add を使って各項目 e を追加すると、それらがリスト項目として表示されます。そして、イベント SelectionChanged に呼応する、イベントハンドラー selectionChanged を登録しておきます。