SaniT404 SaniT404 - 6 months ago 35
Python Question

How to set location of Gtk Button in Gtk Overlay

So currently I have a Gtk Overlay. I can bring up my button that I would like to position using Gtk's

Gtk.Align.CENTER
, but I've been trying for so long now to position the button, rather than aligning it.

# 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


As you can see by the commented lines, I've tried using
Gtk.Widget.set_property()
,
Gtk.Widget.set_alignment()
and a few other things. The
set_alignment()
is just setting the alignment of the image in the button to its parameters, rather than the button itself. Any guidance as to what I should be doing to set the position of the button? Eventually, I'll have multiple buttons at specific positions in the overlay.

Answer

The Description section of the GtkOverlay documentation has your answer: you either use the margin properties of the GtkWidget or the GtkOverlay's get-child-position signal to set the position.

Comments