[pygtk] Centering widgets horizontally on screen

Art Hunkins abhunkin at uncg.edu
Thu Aug 27 11:28:21 WST 2009


Hello, Saeed,

I need a little more help.

Following one of your examples, here is the code I wrote (import gtk is of 
course included):

   but1 = gtk.Button("    1 - MyMusicMIDI ?     ")
   but2 = gtk.Button("    2 - MyMusicASCII ?    ")
   but3 = gtk.Button("    3 - OurMusicMIDI ?    ")
   but4 = gtk.Button("    4 - OurMusicASCII ?   ")
   hbox = gtk.HBox()
   hbox.pack_start(but1, False, False)
   hbox.pack_start(but2, False, False)
   hbox.pack_start(but3, False, False)
   hbox.pack_start(but4, False, False)
   win2 = gtk.Window()
   win2.add(hbox)
   win2.show_all() [I also tried: win2.show()]

This code is placed right under other code that displays on the screen just 
fine; however, *this* code displays nothing. What am I lacking?

Also, just to be clear, I think I *do* need something like the following:
   win2.set_position(gtk.WIN_POS_CENTER)
inserted the third line from the end, above.

I want the *Window* to be centered mid-screen, with all four buttons 
touching together in the middle.

I tried the code with and without this line and got nothing.

Otherwise, the Activity runs as expected, and the Log indicates no errors.

I really appreciate your help.

Art Hunkins

----- Original Message ----- 
From: "saeed" <saeed.gnu at gmail.com>
To: "Art Hunkins" <abhunkin at uncg.edu>
Cc: <pygtk at daa.com.au>
Sent: Tuesday, August 25, 2009 8:42 PM
Subject: Re: [pygtk] Centering widgets horizontally on screen


> That's related to packing, not WIN_POS_CENTER! (that's position of
> window on the screen!)
>
> See gtk.Box.pack_start
>    def pack_start(child, expand=True, fill=True, padding=0)
> I assume you should use expand=True, fill=False
>
> Also you may need to use an empty label gtk.Label('') with packing 
> expand=True
> for example, for your first questtion:
> _________________________________
> #!/usr/bin/python
> import gtk
>
> b1 = gtk.Button('Button1')
> b2 = gtk.Button('Button2')
> b3 = gtk.Button('Button3')
> b4 = gtk.Button('Button4')
> hbox = gtk.HBox()
> hbox.pack_start(b1, True, False)
> hbox.pack_start(b2, True, False)
> hbox.pack_start(b3, True, False)
> hbox.pack_start(b4, True, False)
> win = gtk.Window()
> win.add(hbox)
> win.show_all()
> gtk.main()
> _______________________________
> Now run the script and resize the window to see the result.
> If you want to remove spaces between button, use two empty label (in
> the beginning and the end of box):
> _______________________________
> #!/usr/bin/python
> import gtk
>
> b1 = gtk.Button('Button1')
> b2 = gtk.Button('Button2')
> b3 = gtk.Button('Button3')
> b4 = gtk.Button('Button4')
> hbox = gtk.HBox()
> hbox.pack_start(gtk.Label(''), True, False)
> hbox.pack_start(b1, False, False)
> hbox.pack_start(b2, False, False)
> hbox.pack_start(b3, False, False)
> hbox.pack_start(b4, False, False)
> hbox.pack_start(gtk.Label(''), True, False)
> win = gtk.Window()
> win.add(hbox)
> win.show_all()
> gtk.main()
> __________________________________
> I assume this is what you wish.
>
> About second question, no different between button or hbox or another
> widget. You should only understand packing options and behavior.
>
>
>
> On 8/26/09, Art Hunkins <abhunkin at uncg.edu> wrote:
>> Another newbee question (or two):
>>
>> I've no problem centering text in the middle of the screen, but otherwise
>> don't know how to do the same with buttons and Hboxes. Hence:
>>
>> 1) how to center a group of 4 buttons horizontally?
>>
>> 2) how to center a nested (packed) set of boxes horizontally?
>>
>> I assume gtk.Window and gtk.WIN_POS_CENTER are involved, but that's about
>> all I "know."
>>
>> Art Hunkins
>>
>> _______________________________________________
>> pygtk mailing list   pygtk at daa.com.au
>> http://www.daa.com.au/mailman/listinfo/pygtk
>> Read the PyGTK FAQ: http://faq.pygtk.org/
>> 



More information about the pygtk mailing list