GtkBox などのコンテナー内で Gtk がウィジェットのサイズを制御する方法について、私は完全に混乱しています。誰かがこの質問の複雑さを教えてくれますか?
2 つのウィジェットを含む GtkBox があります。この例では、これらは 2 つの GtkButton です。
最初のウィジェットは、GtkBox コンテナー内の使用可能なスペースを埋める必要があります。
2 番目のウィジェットは常に物理的な正方形にする必要があります。したがって、正方形が拡大すると、最初のウィジェットの幅が縮小します。
ここでは、いくつかのサンプル コードが役立ちます。
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メソッドを 使用しているにもかかわらず、2 番目のボタンが正方形になることはありません。
ウィンドウを縮小すると、2番目のボタンの最小サイズが変更されたため、ウィンドウのサイズを変更できないため、set_size_requestを使用できません(または使用できますか?)。
コンテナーが間隔を管理する方法を理解しようとしている理由は、最終的には次の iTunes の例のようなものを実装しようとしているからです。
つまり、カバーが常に正方形であることがわかりますが、音楽の詳細は、使用可能なスペースに応じて縮小または拡大されます。