我有一个林间空地布局,其中我有一个固定大小的 GtkDrawingArea。层次结构的当前相关部分是:
GtkWindow > GtkVBox > GtkHPaned > GtkViewport > GtkAlignment > GtkFixed > GtkDrawingArea
目前,GtkFixed 小部件绘制在 GtkViewport 的左上角。
但是,如果 GtkFixed(以及它唯一的孩子,GtkDrawingArea)在 GtkViewport 中居中,我真的很喜欢它(当然,当小部件被压缩到不适合其内容和滚动条的程度时除外出现)。
实现这一目标的最佳方法是什么?(我正在使用 pygtk 来做实际的应用程序逻辑,但这应该是一个通用的 gtk 问题)
这是一个有代表性的代码示例:
import gtk
class Controller:
def on_window1_destroy(self, widget, data=None):
gtk.main_quit()
def __init__(self):
self.builder = gtk.Builder()
self.builder.add_from_file("sample.glade")
self.window = self.builder.get_object("window1")
self.builder.connect_signals(self)
def on_drawingarea1_expose_event(self, widget, event):
cr = widget.window.cairo_create()
cr.set_source_rgb(0,0,0)
cr.paint()
return True
if __name__ == "__main__":
client = Controller()
client.window.show()
gtk.main()
还有一个具有相同问题的示例林间空地文件:
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<requires lib="gtk+" version="2.16"/>
<!-- interface-naming-policy project-wide -->
<object class="GtkWindow" id="window1">
<signal name="destroy" handler="on_window1_destroy"/>
<child>
<object class="GtkVBox" id="vbox1">
<property name="visible">True</property>
<child>
<object class="GtkHPaned" id="hpaned1">
<property name="visible">True</property>
<property name="can_focus">True</property>
<child>
<object class="GtkLabel" id="label1">
<property name="visible">True</property>
<property name="label" translatable="yes">label</property>
</object>
<packing>
<property name="resize">False</property>
<property name="shrink">True</property>
</packing>
</child>
<child>
<object class="GtkScrolledWindow" id="scrolledwindow1">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="hscrollbar_policy">automatic</property>
<property name="vscrollbar_policy">automatic</property>
<child>
<object class="GtkAlignment" id="alignment1">
<property name="visible">True</property>
<child>
<object class="GtkFixed" id="fixed1">
<property name="width_request">300</property>
<property name="height_request">300</property>
<property name="visible">True</property>
<child>
<object class="GtkDrawingArea" id="drawingarea1">
<property name="width_request">300</property>
<property name="height_request">300</property>
<property name="visible">True</property>
<signal name="expose_event" handler="on_drawingarea1_expose_event"/>
</object>
</child>
</object>
</child>
</object>
</child>
</object>
<packing>
<property name="resize">True</property>
<property name="shrink">True</property>
</packing>
</child>
</object>
<packing>
<property name="position">0</property>
</packing>
</child>
</object>
</child>
</object>
</interface>