2014-04-18 55 views
1

我对Gtk如何控制GtkBox等容器内的小部件大小感到十分困惑。请有人指导我解决这个问题的复杂性吗?如何更改GTK容器小部件的大小?

我有一个GtkBox包含两个小部件 - 在这个例子中,这些只是两个GtkButtons。

第一个小部件应该只填充GtkBox容器内可用的空间。

我想强制使用的第二个小部件始终是一个物理方块 - 因此,随着方形放大,第一个小部件将在宽度方面收缩。

一些示例代码,将有助于在这里:​​

from gi.repository import Gtk 

def on_check_resize(window): 
    boxallocation = mainbox.get_allocation() 

    width = 0 
    height = 0 

    if boxallocation.height >= boxallocation.width: 
     width = boxallocation.height 
     height = width 

    if boxallocation.width >= boxallocation.height: 
     width = boxallocation.width 
     height = width 

    secondbtn_allocation = secondbtn.get_allocation() 
    secondbtn_allocation.width = width 
    secondbtn_allocation.height = height 
    secondbtn.set_allocation(secondbtn_allocation) 

win = Gtk.Window(title="Containers Demo") 
win.set_border_width(10) 
win.connect("delete-event", Gtk.main_quit) 

mainbox = Gtk.Box() 

firstbtn = Gtk.Button(label="just fills the space") 
mainbox.pack_start(firstbtn, True, True, 0) 
secondbtn = Gtk.Button(label="square") 
mainbox.pack_start(secondbtn, False, True, 1) 

win.add(mainbox) 

win.connect("check_resize", on_check_resize) 

win.show_all() 

initial = firstbtn.get_allocation() 
initial.height = initial.width 
firstbtn.set_size_request(initial.width, initial.height) 

Gtk.main() 

当您展开窗口 - GtkBox将同样扩大。那很好。第一个按钮也类似地扩展。也好。但是,即使我使用GtkWidget的set_allocation方法,第二个按钮也不会是方形的。

我不能使用set_size_request(或可能吗?),因为当我缩小窗口,窗口不能调整由于第二个按钮的改变最小尺寸。

为什么我试图找出如何集装箱管理间距的原因是,我期待最终实现这样的iTunes的例子:

enter image description here

即你可以看封面总是广场 - 但音乐的细节将缩小或扩大到取决于可用空间。

回答

2

而不是Gtk.Box,使用Gtk.Grid并在您打包到网格的按钮上设置hexpandvexpand属性。

而且,考虑使用Gtk.AspectFrame为方形按钮。

相关问题