我在加载 xrc 文件中定义的 wxListCtrl 的列标题时遇到问题。根据 XRC 文件格式描述(http://docs.wxwidgets.org/trunk/overview_xrcformat...),应该可以按类为该控件定义 wxLC_REPORT 样式的列名。您可以在下面找到我需要运行的代码(gui.xml 是具有所需布局的 xrc 文件)。不幸的是,在我的脚本中,我需要手动添加列标题,以便它们显示在 GUI 中(注释 my_gui.rb 中的第 22、23 行)。有没有办法强制 wxRuby 从 xrc 加载列标题,或者我做错了什么?
我正在使用 ruby 1.9.3p327 (2012-11-10) [i386-mingw32] 和 wxruby-ruby19 (2.0.1 x86-mingw32)。
这是 my_gui.rb:
require 'wx'
class My_frame < Wx::Frame
def initialize(parent)
# To load a layout defined in XRC into a Ruby subclass of Dialog,
# first call the empty constructor. All the details of size,
# title, position and so on are loaded from the XRC by the call to
# load_frame_subclass. Using a non-empty constructor will cause
# errors on GTK.
super()
# Load the dialog from XRC. We define $xml in on_init.
# We could use XmlResource.get() over and over again, but
# honestly, thats just too much work.
$xml.load_frame_subclass(self, nil, 'Gui')
@list_ctrl = find_window_by_name('MY_LIST')
puts "inspect list: " + @list_ctrl.inspect + " ID: " + Wx::xrcid('MY_LIST').to_s
#columnn names are shown only if you uncomment following 2 lines
# @list_ctrl.insert_column(0, 'col1_from_code', Wx::LIST_FORMAT_LEFT, 100)
# @list_ctrl.insert_column(1, 'col2_from_code', Wx::LIST_FORMAT_LEFT, 100)
end
end
class My_app < Wx::App
def on_init
#create GUI from xrc
xrc_file = File.join( File.dirname(__FILE__), 'gui.xml' )
$xml = Wx::XmlResource.new(xrc_file)
@frame = My_frame.new(self)
@frame.show(true)
end
end
app = My_app.new
app.main_loop()
这是gui.xml:
<?xml version="1.0" encoding="UTF-8"?>
<resource version="2.3.0.1">
<!-- Created by build 7.4.2.569 -->
<object class="wxFrame" name="Gui">
<title>GUI</title>
<IDident>ID_MAIN_FRM</IDident>
<ID>1000</ID>
<pos>8,8</pos>
<size>640,480</size>
<style>wxDEFAULT_FRAME_STYLE</style>
<object class="wxStaticBox" name="MY_SBOX">
<IDident>ID_MY_SBOX</IDident>
<ID>1005</ID>
<size>610,320</size>
<pos>5,120</pos>
<label>Label</label>
</object>
<object class="wxListCtrl" name="MY_LIST">
<IDident>ID_MY_LIST</IDident>
<ID>1008</ID>
<size>590,120</size>
<pos>15,270</pos>
<style>wxLC_REPORT</style>
<object class="listcol">
<text>Col1</text>
<width>100</width>
<align>wxLIST_FORMAT_LEFT</align>
</object>
<object class="listcol">
<text>Col2</text>
<width>100</width>
<align>wxLIST_FORMAT_LEFT</align>
</object>
</object>
</object>
</resource>
提前谢谢支持。