I am using pygtk dialog, i have added a button and i now want to control the positioning of the button. for example centering it.
here is my code:
import pygtk
pygtk.require('2.0')
import gtk
dlg = gtk.Dialog('Marker Label')
dlg.set_size_request(350, 300)
dlg.show()
entry = gtk.Entry()
entry.show()
entry.set_activates_default(gtk.TRUE)
dlg.vbox.pack_start(entry)
# Create a centering alignment object
align = gtk.Alignment(0.5, 0.5, 0, 0)
button = dlg.add_button(gtk.STOCK_OK, gtk.RESPONSE_OK)
align.add(button)
dlg.vbox.pack_start(align, False, False, 5)
dlg.set_default_response(gtk.RESPONSE_OK)
response = dlg.run()
if response == gtk.RESPONSE_OK:
label = entry.get_text()
print label
dlg.destroy()
i am getting this message:
test.py:19: GtkWarning: Attempting to add a widget with type GtkButton to a container of type GtkAli
gnment, but the widget is already inside a container of type GtkHButtonBox, the GTK+ FAQ at http://library.gnome.org/devel/gtk-faq/stable/ explains how to reparent a widget.
align.add(button)
As it says, the problem is that the button returned by dlg.add_button() already has a parent – which is the Horizontal Button Box at the bottom of the dialog.
You could follow the FAQ – remove the button from the HButtonBox before you try to add it to your vbox. (Hint: either way, I really think you want to do all this before you set the dialog to be visible). It looks like the hbox is called “action_area”. Maybe dlg.action_area.remove(button)
OR you could create the button yourself. Instead of using dlg.add_button(), use gtk.button_new_from_stock(gtk.STOCK_OK).
[But I am not a Gtk programmer, and I don’t know how the event handling works in either case. I.e. I don’t know whether either of these would still close the dialog when you press the button, or whether you’d have to add something more to do that. The Gtk docs are certainly giving me that impression…]