如何在 Gtk Overlay 中设置 Gtk 按钮的位置

How to set location of Gtk Button in Gtk Overlay

所以目前我有一个 Gtk Overlay。我可以调出我想使用 Gtk 的 Gtk.Align.CENTER 定位的按钮,但我已经尝试了很长时间来定位按钮,而不是对齐它。

    # Overlay
    self.overlay = Gtk.Overlay()
    self.overlay.show()

    # Image
    image_pixbuf = GdkPixbuf.Pixbuf.new_from_file("pics/sgcity.png")
    image_pixbuf = image_pixbuf.scale_simple(1200, 720, GdkPixbuf.InterpType.BILINEAR)
    self.image = Gtk.Image()
    self.image.set_from_pixbuf(image_pixbuf)
    self.overlay.add(self.image)
    self.image.show()


    # Test overlay button
    self.printerimg = Gtk.Image()
    self.printerimg.set_from_file("pics/printer.png")

    self.testbutton = Gtk.Button(None)
    self.testbutton.set_image(self.printerimg)
    #self.testbutton.set_alignment(0.1, 0.9)
    #self.testbutton.set_property("width-request", 20)
    #self.testbutton.set_property("height-request", 20)
    self.testbutton.set_allocation(allocation)
    self.testbutton.set_valign(Gtk.Align.CENTER)
    self.testbutton.set_halign(Gtk.Align.CENTER)
    self.overlay.add_overlay(self.testbutton)
    self.testbutton.show()
    self.testbutton.set_name("testbutton")
    self.testbutton.set_opacity(0.8)



    #for i in range(len(printer_store)):
        #self.overlay.connect("get-child-position", self.get_child_position)
        #self.printbuttons[i].show()

    #def get_child_position(self, overlay, widget, allocation):
        #self.child_position_count += 1
        #for i in range(len(self.logic.get_printer_button_attributes())):
            #button_attrib = self.logic.get_printer_button_attributes()
            #allocation.x = button_attrib[i][0]
            #allocation.y = button_attrib[i][1]
            #allocation.height = button_attrib[i][2]
            #allocation.width = button_attrib[i][3]
        #print "changed"
        #return True

正如您在注释行中看到的那样,我已经尝试使用 Gtk.Widget.set_property()Gtk.Widget.set_alignment() 和其他一些方法。 set_alignment() 只是将按钮中图像的对齐设置为其参数,而不是按钮本身。关于我应该如何设置按钮位置的任何指导?最终,我将在叠加层的特定位置设置多个按钮。

The Description section of the GtkOverlay documentation 有你的答案:你要么使用 GtkWidget 的边距属性,要么使用 GtkOverlay 的 get-child-position 信号来设置位置。