2016-05-18 63 views
0

所以目前我有一个Gtk覆盖。我可以调出我想要使用Gtk的Gtk.Align.CENTER来定位的按钮,但我一直试图这么长时间来定位按钮,而不是对齐它。如何在Gtk覆盖中设置Gtk按钮的位置

# 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()只是将按钮中图像的对齐方式设置为其参数,而不是按钮本身。任何指导我应该如何设置按钮的位置?最终,我会在叠加层中的特定位置放置多个按钮。

回答

2

The Description section of the GtkOverlay documentation有你的答案:你可以使用GtkWidget的margin属性或GtkOverlay的get-child-position信号来设置位置。

+0

是的,我一直在尝试可能6个小时,只是玩'code'get-child-position signal'code',仍然没有得到它的工作。我不确定我做错了什么。 – TheEggSample

+0

我沿'self.overlay.connect(“get-child-position”,self.get_child_position)'行创建处理程序,并且我得到它是向我发送覆盖层和按钮以及Gdk Rectangle。我无法确定如何真正从矩形设置位置。我尝试使用'set_allocation(modified_gdk_rectangle)',但没有得到那个工作。我想我只是对我应该如何改变'self.get_child_position()'中的位置感到困惑,并且无法在网上找到任何示例。 (请原谅我糟糕的降价格式) – TheEggSample

+0

您可以通过设置该矩形参数的字段并返回“True”来设置位置。我不知道Python是否有一个引用参数的概念,但这就是'allocate'的含义。 – andlabs