Errror in running wx Erlang code

417 views Asked by At

Using the sample code provided by wx:demo()I am getting various errors and exception and as im quite new to the erlang language im struggling to find a solution

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)

this is the error message im getting. here is the code:

-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

There are 1 answers

1
Michael On

You need to call wx:new/0 or similar, to initialise correctly.

For example, I can replicate your problem like this:

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>

However if I call wx:new/0 first:

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

It now works.