运行wx Erlang代码时出错

时间:2016-01-25 16:52:17

标签: erlang wxerlang

使用wx:demo()提供的示例代码我收到各种错误和异常,因为我很难找到解决问题的erlang语言

wx_grid:start().    
** exception exit: {wxe,unknown_port}
     in function  wx:get_env/0 (wx.erl, line 133)
     in call from wxe_util:cast/2 (wxe_util.erl, line 61)
     in call from wx:batch/1 (wx.erl, line 179)
     in call from wx_object:init_it/6 (wx_object.erl, line 337)
     in call from proc_lib:init_p_do_apply/3 (proc_lib.erl, line 240)

这是我得到的错误消息。 这是代码:

-module(wx_grid).

-behaviour(wx_object).

%% Client API
-export([start/1,start/0]).

%% wx_object callbacks
-export([init/1, terminate/2,  code_change/3,
     handle_info/2, handle_call/3, handle_cast/2, handle_event/2]).

-include_lib("wx/include/wx.hrl").

-record(state, 
    {
      parent,
      config,
      grid
    }).

start() ->
  start([]).


start(Config) ->
    wx_object:start_link(?MODULE, Config, []).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
init(Config) ->
    wx:batch(fun() -> do_init(Config) end).

do_init(Config) ->
    Parent = proplists:get_value(parent, Config),  
    Panel = wxPanel:new(Parent, []),

    %% Setup sizers
    MainSizer = wxBoxSizer:new(?wxVERTICAL),
    Sizer = wxStaticBoxSizer:new(?wxVERTICAL, Panel, 
                 [{label, "wxGrid"}]),

    Grid = create_grid(Panel),

    %% Add to sizers
    Options = [{flag, ?wxEXPAND}, {proportion, 1}],

    wxSizer:add(Sizer, Grid, Options),
    wxSizer:add(MainSizer, Sizer, Options),

    wxPanel:setSizer(Panel, MainSizer),
    {Panel, #state{parent=Panel, config=Config,
          grid = Grid}}.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Async Events are handled in handle_event as in handle_info
handle_event(#wx{event = #wxGrid{type = grid_cell_change,
                 row = Row, col = Col}},
         State = #state{}) ->
    Val = wxGrid:getCellValue(State#state.grid, Row, Col),
    demo:format(State#state.config, "Cell {~p,~p} changed to ~p.\n",
        [Row,Col,Val]),
    {noreply, State}.

%% Callbacks handled as normal gen_server callbacks
handle_info(_Msg, State) ->
    {noreply, State}.

handle_call(shutdown, _From, State=#state{parent=Panel}) ->
    wxPanel:destroy(Panel),
    {stop, normal, ok, State};

handle_call(_Msg, _From, State) ->
    {reply,{error, nyi}, State}.

handle_cast(Msg, State) ->
    io:format("Got cast ~p~n",[Msg]),
    {noreply,State}.

code_change(_, _, State) ->
    {stop, ignore, State}.

terminate(_Reason, _State) ->
    ok.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Local functions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

create_grid(Panel) ->
    %% Create the grid with 100 * 5 cells
    Grid = wxGrid:new(Panel, 2, []),
    wxGrid:createGrid(Grid, 100, 5),

    Font = wxFont:new(16, ?wxFONTFAMILY_SWISS,
              ?wxFONTSTYLE_NORMAL,
              ?wxFONTWEIGHT_NORMAL, []),
    %% Fun to set the values and flags of the cells
    Fun =
    fun(Row) ->
        wxGrid:setCellValue(Grid, Row, 0, "Editable"),
        wxGrid:setCellValue(Grid, Row, 1, "Editable"),
        wxGrid:setCellValue(Grid, Row, 2, "Editable"),
        wxGrid:setCellValue(Grid, Row, 3, "Read only"),
        wxGrid:setCellTextColour(Grid, Row, 3, ?wxWHITE),
        wxGrid:setReadOnly(Grid, Row, 3, [{isReadOnly,true}]),
        wxGrid:setCellValue(Grid, Row, 4, "Editable"),
        case Row rem 4 of
            0 -> wxGrid:setCellBackgroundColour(Grid, Row, 3, ?wxRED);
            1 -> wxGrid:setCellBackgroundColour(Grid, Row, 3, ?wxGREEN),
             wxGrid:setCellTextColour(Grid, Row, 2, {255,215,0,255});
            2 -> wxGrid:setCellBackgroundColour(Grid, Row, 3, ?wxBLUE);
            _ -> wxGrid:setCellBackgroundColour(Grid, Row, 1, ?wxCYAN),
             wxGrid:setCellValue(Grid, Row, 1,
                         "Centered\nhorizontally"),
             wxGrid:setCellAlignment(Grid, Row, 4,
                         0,?wxALIGN_CENTER),
             wxGrid:setCellValue(Grid, Row, 4,
                         "Centered\nvertically"),
             wxGrid:setCellAlignment(Grid, Row, 1,
                         ?wxALIGN_CENTER,0),
             wxGrid:setCellTextColour(Grid, Row, 3, ?wxBLACK),
             wxGrid:setCellAlignment(Grid, Row, 2,
                         ?wxALIGN_CENTER,
                         ?wxALIGN_CENTER),
             wxGrid:setCellFont(Grid, Row, 0, Font),
             wxGrid:setCellValue(Grid, Row, 2,
                         "Centered vertically\nand horizontally"),
             wxGrid:setRowSize(Grid, Row, 80)
        end
    end,
    %% Apply the fun to each row
    wx:foreach(Fun, lists:seq(0,99)),
    wxGrid:setColSize(Grid, 2, 150),
    wxGrid:connect(Grid, grid_cell_change),
    Grid.

1 个答案:

答案 0 :(得分:1)

您需要拨打wx:new/0或类似名称,才能正确初始化。

例如,我可以像这样复制你的问题:

1> Frame = wxFrame:new(wx:null(), -1, "Hello").
** exception error: {wxe,unknown_port}
     in function  wx:get_env/0 (wx.erl, line 132)
     in call from wxe_util:call/2 (wxe_util.erl, line 69)
2>

但是,如果我先拨打wx:new/0

2> Wx = wx:new().
{wx_ref,0,wx,[]}
3> Frame = wxFrame:new(wx:null(), -1, "Hello").
{wx_ref,35,wxFrame,[]}
4>

它现在有效。