2

wx.TreeCtrl の推奨サイズは、ツリーが完全に折りたたまれたときにすべての要素が収まる最小サイズのようです。すべてが展開された状態でツリーの幅を計算する良い (つまり、クロスプラットフォーム互換の) 方法はありますか? 私の現在の解決策はこれです:

def max_width(self):
    dc = wx.ScreenDC()
    dc.SetFont(self.GetFont())
    widths = []
    for item, depth in self.__walk_items():
        if item != self.root:
            width = dc.GetTextExtent(self.GetItemText(item))[0] + self.GetIndent()*depth
            widths.append(width)
    return max(widths) + self.GetIndent()

これは win32 ではうまく機能しますが、Linux ではうまくいきません。報告されたサイズを上書きできるように、TreeCtrl 自体にそのサイズを教えてもらう方法はありますか? (常に最大に拡張された幅を返します)

編集: 上記で使用する関数を提供していないことをお許しください。しかし、ツリーをたどって、すべてのラベルの幅を取得し、最も広いラベルの合計幅を返します (インデントを考慮して)。

4

2 に答える 2

1

GetBestSize折りたたまれたアイテムを考慮しているようです。たとえばwxTreeCtrl、デモ (Ubuntu と wxPython 2.8.7.1 を使用) で、内部テキスト文字列の長さを変更すると (私のバージョンのデモでは 74 行目) の戻り値はself.tree.GetBestSize()、この新しい長さのツリーが展開されていない場合でも、内部の文字列が考慮されます。多分あなたは電話する必要がありますSetQuickBestSize(False)か?

于 2009-06-07T01:11:04.660 に答える
0

このコードは、Windows と Linux の両方で機能します。

def compute_best_size(self):
    if os.name == 'nt':
        best_size = (self.__max_width_win32(), -1)
    else:
        best_size = (self.__max_width(), -1)
    self.SetMinSize(best_size)

def __max_width_win32(self):
    dc = wx.ScreenDC()
    dc.SetFont(self.GetFont())
    widths = []
    for item, depth in self.__walk_items():
        if item != self.root:
            width = dc.GetTextExtent(self.GetItemText(item))[0] + self.GetIndent()*depth
            widths.append(width)
    return max(widths) + self.GetIndent()

def __max_width(self):
    self.Freeze()
    expanded = {}
    for item in self.get_items():
        if item is not self.root:
            expanded[item] = self.IsExpanded(item)
    self.ExpandAll()
    best_size = self.GetBestSize()
    for item in expanded:
        if not expanded[item]: self.Collapse(item)
    self.Thaw()
    return best_size[0]

...そしてcompute_best_size()、新しいアイテムがツリーに追加されるたびに呼び出します。

于 2009-06-10T13:32:11.383 に答える