2016-01-25 67 views
2

使用WX提供的示例代碼:演示()我得到的各種錯誤和異常,並作爲即時通訊很新的用Erlang語言IM努力尋找解決Errror在跑步寬x二郎代碼

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. 
+0

如果您通常編寫自己的函數而不使用特殊行爲將會更容易。 –

回答

2

你需要調用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> 

現在的作品。