2

I have created a gtk button with an image on it. The size of my image is 150x150. I want to create a button of 150x150 and it should be same even while maximizing the window.

image = gtk.Image()
image.set_from_file("/home/jeevan/Pictures/msnbus.png")
box1=gtk.VBox()
box2=gtk.HBox()
box1.pack_start(box2)
gobutton=gtk.Button()
gobutton.add(image)
gobutton.set_size_request(150,150)
box2.pack_start(gobutton)

button when unmaximized

button while maximized

I want mybutton to be of same 150x150 size even when maximized. How is it possible in python gtk?

1 Answer 1

4

You don't want the Gtk.Button to expand so you should do something like this:

gobutton.set_hexpand(False)
gobutton.set_halign(Gtk.Align.CENTER)
gobutton.set_vexpand(False)
gobutton.set_valign(Gtk.Align.CENTER)

Instead of Gtk.Align.CENTER you can of course also use Gtk.Align.END, Gtk.Align.START or Gtk.Align.BASELINE.

EDIT:

For Gtk+ 2 (with pygtk) you can pack the Gtk.Button in another Gtk.Box with two Gtk.Alignments

image = gtk.Image.new_from_file("filename")
button = gtk.Button()
button.add(image)

inner_box = gtk.VBox()
inner_box.pack_start(gtk.Alignment(), True, True, 0)
inner_box.pack_start(button, False, False, 0)
inner_box.pack_start(gtk.Alignment(), True, True, 0)

outer_box = gtk.HBox()
outer_box.pack_start(gtk.Label("some widget"), True, True, 0)
outer_box.pack_start(inner_box, False, False, 0)
Sign up to request clarification or add additional context in comments.

2 Comments

After using your code I get this error: AttributeError: 'gtk.Button' object has no attribute 'set_hexpand'
See the edit in my answer. If you are writing a new program, you should consider switching to gtk+ 3.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.