[wxruby-users] RES: Problem centering a label

Chauk-Mean Proum chauk.mean at gmail.com
Fri Mar 20 09:39:04 EDT 2009


Hi Alejandro,

I'm not sure to understand why you need 2 sizers but your main problem
is that you haven't told the panel to use the sizer !

Here is a sample code where there are 3 texts :
- at the top, the text is centered
- at the middle, the text is left aligned
- at the bottom, the text is right aligned

The important line is the one with self.sizer = box.
You should also use wx constants to make your code clearer.

#!/usr/bin/env ruby
# wxRuby2 Sample Code. Copyright (c) 2004-2008 wxRuby development team
# Freely reusable code: see SAMPLES-LICENSE.TXT for details
begin
  require 'rubygems'
rescue LoadError
end
require 'wx'

class MyPanel < Wx::Panel
  def initialize(parent)
    super(parent)
    box = Wx::BoxSizer.new (Wx::VERTICAL)
    box.add_item(Wx::StaticText.new(self, :label => "Centered Title"),
:flag => Wx::ALIGN_CENTER)
    box.add_item(Wx::StaticText.new(self, :label => "Leftside Text"),
:flag => Wx::ALIGN_LEFT)
    box.add_item(Wx::StaticText.new(self, :label => "Rightside Text"),
 :flag => Wx::ALIGN_RIGHT)
    self.sizer = box
  end
end

# This is the minimum code to start a WxRuby app - create a Frame, and
# show it.
Wx::App.run do
  frame = Wx::Frame.new(nil, :title => "Minimal wxRuby App")
  panel = MyPanel.new(frame)
  frame.show
end

Cheers.

Chauk-Mean.


More information about the wxruby-users mailing list