简体   繁体   English

为什么不能添加多个自定义按钮?

[英]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).我尝试在 Gtk.Box 中添加 3 个按钮,这个 Box 是在我的 Window 设计(带有 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;我是初学者,不知道为什么我不能为这个名为“Box”的容器一次添加多个带有图像的按钮; self.placeButtonCameras. self.placeButtonCameras。

In future, is for adding buttons from a config (0 to 5 cameras possibles)将来,用于从配置中添加按钮(可能有 0 到 5 个摄像头)

The problem is not that you're adding multiple buttons, but that you're trying to re-use a GtkImage in multiple GtkButtons.问题不在于您要添加多个按钮,而在于您试图在多个 GtkButtons 中重用 GtkImage。 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM