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

如何访问Gtk.Dialog中的Gtk.Box?(Gtk3.0 Python)

问题:Gtk 3.0 Python绑定中如何访问Gtk.Dialog内置的Gtk.Box

执行代码时出现以下错误提示:

Gtk-WARNING **: 18:33:56.632: Attempting to add a widget with type GtkBox to a GtkDialog, but as a GtkBin subclass a GtkDialog can only contain one widget at a time; it already contains a widget of type GtkBox

出错代码如下:

def switchfile(self, widget):
    self.cd = None
    self.cd = {
        'win'   : Gtk.Dialog(),
        'entry' : Gtk.Entry(),
        'btnok' : Gtk.Button.new_from_icon_name("document-open-symbolic", Gtk.IconSize.MENU),
        'label' : Gtk.Label(),
        'vbox'  : Gtk.Box(orientation=Gtk.Orientation.VERTICAL),
        'hbox'  : Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
    }

    self.cd['entry'].set_placeholder_text('Notebook Name')
    self.cd['btnok'].connect('clicked', self.switch_yes)

    self.cd['hbox'].pack_start(self.cd['entry'], True, True, 0)
    self.cd['hbox'].pack_start(self.cd['btnok'], True, True, 0)

    self.cd['vbox'].pack_start(self.cd['label'], True, True, 0)
    self.cd['vbox'].pack_start(self.cd['hbox'], True, True, 0)

    self.cd['vbox'].show_all()
       
    self.cd['win'].add(self.cd['vbox'])
    self.cd['win'].show_all()
    self.cd['win'].run()

已知Gtk.Dialog中已内置一个Gtk.Box,但在Python的gi.repository.Gtk 3.0版本中找不到C/C++里的get_vbox()方法,也没有get_box()、get_container()等相关方法,请问如何访问这个内置的Gtk.Box?


解决方法

在Gtk 3的Python绑定中,Gtk.Dialog的内置内容区域需要通过get_content_area()方法获取,这个方法会返回Dialog自带的Gtk.Box容器。

修改代码时,只需把原来的self.cd['win'].add(self.cd['vbox'])替换为将自定义vbox添加到content area中即可:

# 替换原来的add语句
content_area = self.cd['win'].get_content_area()
content_area.pack_start(self.cd['vbox'], True, True, 0)

完整修改后的代码:

def switchfile(self, widget):
    self.cd = None
    self.cd = {
        'win'   : Gtk.Dialog(),
        'entry' : Gtk.Entry(),
        'btnok' : Gtk.Button.new_from_icon_name("document-open-symbolic", Gtk.IconSize.MENU),
        'label' : Gtk.Label(),
        'vbox'  : Gtk.Box(orientation=Gtk.Orientation.VERTICAL),
        'hbox'  : Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
    }

    self.cd['entry'].set_placeholder_text('Notebook Name')
    self.cd['btnok'].connect('clicked', self.switch_yes)

    self.cd['hbox'].pack_start(self.cd['entry'], True, True, 0)
    self.cd['hbox'].pack_start(self.cd['btnok'], True, True, 0)

    self.cd['vbox'].pack_start(self.cd['label'], True, True, 0)
    self.cd['vbox'].pack_start(self.cd['hbox'], True, True, 0)

    self.cd['vbox'].show_all()
       
    # 获取Dialog内置的内容区域并添加自定义vbox
    content_area = self.cd['win'].get_content_area()
    content_area.pack_start(self.cd['vbox'], True, True, 0)
    
    self.cd['win'].show_all()
    self.cd['win'].run()

原错误的核心原因是:Gtk.Dialog作为GtkBin子类,只能直接包含一个子部件,而它默认已经内置了用于承载内容的Box容器,因此不能用add()方法再添加新的Box,必须将自定义部件添加到这个内置的content area中。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 19:57:40