在PyGobject中动态调整GtkLayout的大小

时间:2012-05-23 08:11:14

标签: python-3.x gtk gtk3 pygobject

我正在寻找一种方法来自动“缩放以适应”GtkLayout的内容,具体取决于允许占用的空间量。

这是我的解决方案:

class MyLayout(Gtk.Layout):
    def __init__(self, document, **args):
        Gtk.Layout.__init__(self, **args)
        doc_width = 0
        doc_height = 0
        self.document = document

        # Determine the size of the document, I want to display
        for page in self.document.pages:
            doc_width = max(doc_width, page.width)
            doc_height += page.height
        self.aspect_ratio = doc_height / doc_width

        self.connect("draw", self.draw)

    def draw(self, widget, context):
        w = self.get_allocated_width()
        h = self.get_allocated_width()*self.aspect_ratio
        self.set_size(w, h) # sets the _content_ size, not the actual widget size
        print("draw")

不幸的是,这会导致每次调整窗口大小时调用draw()两次,这很慢,一旦我真的需要在布局中绘制一些东西。

这个问题的正确解决方案是什么?

最好的问候,
法比安·亨泽

解决方案:我前段时间找到了一个解决方案。我的项目中是herehere。我没有时间从中提取一个最小的例子。如果有人愿意这样做,我很乐意接受它作为答案。

1 个答案:

答案 0 :(得分:1)

我的猜测是,这可能是父母多次重新绘制子项并由层次结构对象触发的问题。

我扩展你的例子,以便在绘制流程上显示更详细。

from gi.repository import Gtk

class MyLayout(Gtk.Layout):
    def __init__(self, document, **args):
        Gtk.Layout.__init__(self, **args)
        doc_width = 0
        doc_height = 0
        self.document = document

        # Determine the size of the document, I want to display
        for page in self.document.pages:
            doc_width = max(doc_width, page.width)
            doc_height += page.height
        self.aspect_ratio = doc_height / doc_width

        self.connect("draw", self.draw)

    def do_realize(self):
        Gtk.Layout.do_realize(self)
        print ('do_realize')

    def do_show(self):
        Gtk.Layout.do_show(self)
        print ('do_show')

    def do_draw(self, widget):
        Gtk.Layout.do_draw(self, widget)
        print ('do_draw'), widget

    def draw(self, widget, context):
        print widget, context
        w = self.get_allocated_width()
        h = self.get_allocated_width() * self.aspect_ratio
        self.set_size(w, h) # sets the _content_ size, not the actual widget size
        print("draw")

class page(object):
    def __init__(self, width, height):
        self.width = width
        self.height = height

class document(object):
    def __init__(self):
        self.pages = [page(20, 10), page(40, 30)]

layout = MyLayout(document())
win = Gtk.Window()
layout.show()
win.add(layout)
win.show()
Gtk.main()

对我来说,输出是:

do_show
do_realize
<MyLayout object at 0xb6b0ef54 (__main__+MyLayout at 0x898e950)> <cairo.Context object at 0xb73237f0>
draw
do_draw <cairo.Context object at 0xb73237f0>
<MyLayout object at 0xb6b0ef54 (__main__+MyLayout at 0x898e950)> <cairo.Context object at 0xb73237f0>
draw
do_draw <cairo.Context object at 0xb73237f0>
<MyLayout object at 0xb6b0ef54 (__main__+MyLayout at 0x898e950)> <cairo.Context object at 0xb73237f0>
draw
do_draw <cairo.Context object at 0xb73237f0>
<MyLayout object at 0xb6b0ef54 (__main__+MyLayout at 0x898e950)> <cairo.Context object at 0xb73237f0>
draw
do_draw <cairo.Context object at 0xb73237f0>
<MyLayout object at 0xb6b0ef54 (__main__+MyLayout at 0x898e950)> <cairo.Context object at 0xb73237f0>
draw
do_draw <cairo.Context object at 0xb73237f0>

我可以看到绘制信号是在do_draw内部窗口小部件功能之前发出的,并且它被多次触发。