使用GTK3固定的布局

时间:2015-11-28 20:16:11

标签: python layout gtk gtk3

如何制作带有可滚动布局的窗口,并在其上方固定位置叠加图像?

我想我应该制作这样的嵌套结构:

。窗口
..固定
... ScrolledWindow
....布局
.......
...图片

但布局内容未在固定或其他布局中显示

这是一个简单的例子:

import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk


class MainWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="Example")

        self.set_default_size(200, 200)

        self.fixed = Gtk.Fixed()
        self.add(self.fixed)

        self.layout = Gtk.Layout()
        self.fixed.put(self.layout, 0, 0)
        self.layout.set_vexpand(True)
        self.layout.set_hexpand(True)

        icon = Gtk.Image.new_from_icon_name('icon', Gtk.IconSize.DIALOG)

        self.layout.put(icon, 0, 0)


win = MainWindow()
win.connect("destroy", lambda q: Gtk.main_quit())
win.show_all()
Gtk.main()

1 个答案:

答案 0 :(得分:0)

以下是Overlay小部件的完整测试,例如@ elya5推荐:

import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk


class MainWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="Example")

        # Window options
        self.set_default_size(200, 200)

        # Overlay for overlaying widgets
        self.overlay = Gtk.Overlay()
        self.add(self.overlay)

        # ScrolledWindow for scrolling
        self.scrolledwindow = Gtk.ScrolledWindow()
        self.overlay.add(self.scrolledwindow)

        # Layout for absolutely positioned widgets
        self.layout = Gtk.Layout()
        self.layout.set_vexpand(True)
        self.layout.set_hexpand(True)
        self.layout.set_size(200, 900)
        self.scrolledwindow.set_hadjustment(self.layout.get_hadjustment())
        self.scrolledwindow.set_vadjustment(self.layout.get_vadjustment())
        self.scrolledwindow.add(self.layout)

        # Fixed for fixed positioned overlay widgets
        self.fixed = Gtk.Fixed()
        self.overlay.add_overlay(self.fixed)
        self.overlay.set_overlay_pass_through(self.fixed, True)


        entry = Gtk.Entry()
        self.layout.put(entry, 20, 20)

        icon = Gtk.Image.new_from_icon_name('dialog-information', Gtk.IconSize.DIALOG)
        self.fixed.put(icon, 60, 10)


win = MainWindow()
win.connect("destroy", lambda q: Gtk.main_quit())
win.show_all()
Gtk.main()