Village
Village

Reputation: 24383

How to pack widgets towards the vertical center of a window in PyGTK?

I am using PyGTK to create a GUI:

vbox = gtk.VBox(False, 1)
vbox.set_border_width(1)
window.add(vbox)
vbox.show()

vbox.pack_start(menubar, False, True, 0)
vbox.pack_start(a, False, False, 0)
vbox.pack_start(b, False, False, 10)
vbox.pack_end(self.statusbar, False, False, 0)
vbox.pack_end(c, False, False, 10)

This creates a window like this:

 _______________________
|_Window____________|_X_|
|_File_Edit_____________|
|          [a]          |
|          [b]          |
|                       |
|                       |
|                       |
|                       |
|__________[c]__________|
|__Status okay._________|

I need to center a and b vertically, so that the bottom edge of a appears at vertical center and the top edge of b appears at the vertical center:

 _______________________
|_Window____________|_X_|
|_File_Edit_____________|
|                       |
|           |           |
|           v           |
|          [a]          |
|                       |<-- 10 px margin
|          [b]          |
|           ^           |
|           |           |
|                       |
|__________[c]__________|
|__Status okay._________|

Is there a way to vertically center the items in a window in this way?

Upvotes: 4

Views: 2106

Answers (1)

M.javid
M.javid

Reputation: 6637

You must using second VBox to pack a & b widgets together and then pack this box center, run below code:

import gtk

window = gtk.Window()
window.set_size_request(120, 180)

vbox = gtk.VBox()
vbox.set_border_width(1)
window.add(vbox)
vbox.show()

a = gtk.Label('a')
b = gtk.Label('b')
c = gtk.Label('c')

vbox2 = gtk.VBox()
vbox2.pack_start(a, True, False, 0)
vbox2.pack_start(b, True, False, 10)
vbox.pack_start(vbox2, True, False, 10)
vbox.pack_end(c, False, False, 10)

window.connect('destroy', gtk.main_quit)
window.show_all()
gtk.main()

The Pack_start and pack_end methods getting some parameters to set position of your widgets.

pack_start(widget?, center?, max_stretch?, border?)

Upvotes: 2

Related Questions