2

GtkTextView があり、テキストの最大線幅を設定できるようにしたいと考えています。TextView の幅が最大テキスト幅を超える場合、余分なスペースはテキストの左右のパディングで埋める必要があります。Gtk はmin-widthCSS プロパティをサポートしていますが、max-widthプロパティはないようです。size-allocate代わりに、に接続して TextView のサイズが変更されるたびに、マージンを動的に設定しようとしました

def on_textview_size_allocate(textview, allocation):
    width = allocation.width
        if width > max_width:                       
            textview.set_left_margin((width - max_width) / 2)            
            textview.set_right_margin((width - max_width) / 2)            
        else:                                       
            textview.set_left_margin(0)
            textview.set_right_margin(0)

これにより、特定の TextView 幅に対して目的のテキスト行幅が生成されますが、ウィンドウのサイズを変更すると奇妙な動作が発生します。ウィンドウをより狭い幅にサイズ変更すると、遅延が遅くなります。ウィンドウを最大化しようとすると、ウィンドウが画面よりもはるかに大きな幅にジャンプします。 size-allocate接続する正しい信号ではないかもしれませんが、TextView のサイズが変更されたときにマージンを動的に設定する他の方法を見つけることができませんでした。

テキスト行の最大幅を達成する正しい方法は何ですか?

4

1 に答える 1

1

私は解決策を思いつきました。GtkBin、 overrodeから継承してカスタム コンテナーを作成し、そのコンテナーにdo_size_allocatemy を追加しました。GtkTextView

class MyContainer(Gtk.Bin):
    max_width = 500

    def do_size_allocate(self, allocation):
        # if container width exceeds max width
        if allocation.width > self.max_width:
            # calculate extra space
            extra_space = allocation.width - self.max_width
            # subtract extra space from allocation width
            allocation.width -= extra_space
            # move allocation to the right so that it is centered
            allocation.x += extra_space / 2
        # run GtkBin's normal do_size_allocate
        Gtk.Bin.do_size_allocate(self, allocation)
于 2020-10-05T19:13:33.757 に答える