Search code examples
gtk3pygtk

How can I access the Gtk.Box in Gtk.Dialog?


I get an error (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) when executing this code:

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()

But, if there already is a Gtk.Box in the Gtk.Dialog, how can I access it? In another Question on Stackoverflow, there was a Dialog.get_vbox()function, but it was C/C++ and when i use bpython3 to list the functions in Gtk.Dialog, it has nothing, no get_vbox(), an also nothing else like get_vbox: no get_box()' and no get_container()`.

How can I access the Gtk.Box in Gtk.Dialog

Information: I am using Version 3.0 of gi.repository.Gtk


Solution

  • Now I found out how to do it.

    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'  : None,
            'hbox'  : Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
        }
    
        self.cd['win'].add_buttons(
            Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
            Gtk.STOCK_OK, Gtk.ResponseType.OK
        )
    
        self.cd['vbox'] = self.cd['win'].get_content_area()
    
        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'].show_all()
        self.cd['win'].run()
    

    Important is this line:

    self.cd['vbox'] = self.cd['win'].get_content_area()
    

    I didn't know about that function, but this is how to access the Gtk.Box in Gtk.Dialog objects.