Search code examples
pythonraspberry-pigtk3glade

Why can't add more than one custom button?


I try to add 3 buttons inside a Gtk.Box, this Box was created in my Window design (with Glade). See the code;

    # Button for IPCamera (MJpeg stream)    
    self.buttonIPCam1 = Gtk.Button()
    self.buttonIPCam1.add(self.imageIPCam)
    self.buttonIPCam1.connect("clicked", self.triggerIPCam1)
    self.buttonIPCam1.props.relief = Gtk.ReliefStyle.NONE
    self.placeButtonCameras.add(self.buttonIPCam1)
    self.buttonIPCam2 = Gtk.Button()
    self.buttonIPCam2.add(self.imageIPCam)
    self.buttonIPCam2.connect("clicked", self.triggerIPCam2)
    self.buttonIPCam2.props.relief = Gtk.ReliefStyle.NONE
    self.placeButtonCameras.add(self.buttonIPCam2) 
    self.buttonIPCam3 = Gtk.Button()
    self.buttonIPCam3.add(self.imageIPCam)
    self.buttonIPCam3.connect("clicked", self.triggerIPCam3)
    self.buttonIPCam3.props.relief = Gtk.ReliefStyle.NONE
    self.placeButtonCameras.add(self.buttonIPCam3)
    
    self.placeButtonCameras.show_all()

Only one button with icon is displayed and got this error message;

(app_pre.py:3068): Gtk-WARNING **: 13:30:27.422: Attempting to add a widget with type GtkImage to a container of type GtkButton, but the widget is already inside a container of type GtkButton, please remove the widget from its existing container first.

I'm beginner and don't known why I can't add more than one Button with Image at time for this container "Box" named; self.placeButtonCameras.

In future, is for adding buttons from a config (0 to 5 cameras possibles)


Solution

  • The problem is not that you're adding multiple buttons, but that you're trying to re-use a GtkImage in multiple GtkButtons. Rather than trying to re-use the widget, you should create a new one each time.

    In other words, your current code does this:

        // somewhere earlier: self.imageIPCam = new Gtk.Image()
    
        self.buttonIPCam1.add(self.imageIPCam)
        // ...
        self.buttonIPCam2.add(self.imageIPCam)
        // ...
        self.buttonIPCam3.add(self.imageIPCam)
    

    To fix it you can do this:

        imageIPCam1 = new Gtk.Image()
        self.buttonIPCam1.add(self.imageIPCam1)
        // ...
        imageIPCam2 = new Gtk.Image()
        self.buttonIPCam2.add(self.imageIPCam2)
        // ...
        imageIPCam3 = new Gtk.Image()
        self.buttonIPCam3.add(self.imageIPCam3)