You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Gtk+3中实现水平盒嵌入垂直盒(Python代码示例)

Properly Adding a Horizontal Box to a Vertical Box in GTK+3 (Python)

Hey there! Let's walk through how to correctly embed a horizontal box into your vertical box for your GTK+3 file manager. Your existing code is on the right track, but let's clarify the details and provide a complete, working example.

Key Concepts to Remember

First, a quick recap of how Gtk.Box works:

  • A vertical box (Gtk.Orientation.VERTICAL) stacks child widgets top-to-bottom.
  • A horizontal box (default Gtk.Orientation.HORIZONTAL) arranges widgets left-to-right.
  • When adding a box to another box, you use pack_start() (or pack_end()) which controls how the child widget behaves within its parent.

Fixing and Completing Your Implementation

Your code snippet already starts adding the horizontal box to the vertical box, but let's fill in the gaps and explain the critical parameters. Here's a full, runnable example:

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

def create_menu_bar():
    # Placeholder for your menu bar implementation
    menu_bar = Gtk.MenuBar()
    file_menu = Gtk.Menu()
    file_item = Gtk.MenuItem(label="File")
    file_item.set_submenu(file_menu)
    menu_bar.append(file_item)
    return menu_bar

def create_tool_bar():
    # Placeholder for your toolbar implementation
    tool_bar = Gtk.Toolbar()
    new_btn = Gtk.ToolButton(icon_name="document-new")
    tool_bar.add(new_btn)
    open_btn = Gtk.ToolButton(icon_name="document-open")
    tool_bar.add(open_btn)
    return tool_bar

class FileManager(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="File Manager")
        self.set_size_request(800, 600)
        self.set_position(Gtk.WindowPosition.CENTER)

        # Main vertical box to hold all components
        vertical_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
        self.add(vertical_box)

        # Add menu bar to vertical box
        menu_bar = create_menu_bar()
        vertical_box.pack_start(menu_bar, False, False, 0)

        # Add toolbar to vertical box
        tool_bar = create_tool_bar()
        vertical_box.pack_start(tool_bar, False, False, 0)

        # Create and configure the horizontal box
        horizontal_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=10)
        # Add sample widgets to the horizontal box (replace with your content)
        left_pane = Gtk.Label(label="Left Pane (e.g., Directory Tree)")
        right_pane = Gtk.Label(label="Right Pane (e.g., File List)")
        horizontal_box.pack_start(left_pane, True, True, 0)
        horizontal_box.pack_start(right_pane, True, True, 0)

        # Add horizontal box to vertical box - this is the key part!
        vertical_box.pack_start(horizontal_box, True, True, 0)

        # Show all widgets (critical for visibility)
        self.show_all()

if __name__ == "__main__":
    app = FileManager()
    app.connect("destroy", Gtk.main_quit)
    Gtk.main()

Breaking Down the Critical Line

When adding the horizontal box to the vertical box, this line is crucial:

vertical_box.pack_start(horizontal_box, True, True, 0)

Let's explain each parameter:

  • horizontal_box: The child widget we're adding.
  • True (expand): If True, the horizontal box will take up any extra vertical space in the vertical box. This is why the panes will stretch to fill the window height.
  • True (fill): If True, the horizontal box will expand to fill the space allocated to it (matches the expand parameter here).
  • 0 (padding): The space (in pixels) between this widget and the one above/below it.

Common Mistakes to Avoid

  • Forgetting to call self.show_all(): Without this, your widgets won't be visible. You can also call show() on individual widgets, but show_all() is easier for complex layouts.
  • Incorrect expand/fill parameters: If you set expand=False for the horizontal box, it won't stretch to fill the window's vertical space, which is probably not what you want for a file manager.
  • Not specifying spacing in boxes: Adding a small spacing (like spacing=10) makes the layout look cleaner and prevents widgets from being squished together.

内容的提问来源于stack exchange,提问作者Abhishek Keshri

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:09:56