2017-08-24 79 views
0

我的代碼幾乎與Skulpt.org上提供的代碼相匹配,但我似乎無法讓任何海龜出現在我的畫布上。我添加了代碼以確保正在加載turtle庫並且python正在成功運行。兩條消息都說這是事實;一切似乎都在按照原樣進行。如何在流星中實現龜(在流星中)?

下面是一些我的模板代碼:

Template.pythonEnv.events({ 
'click #py_run': function(event, template) { 
    // get content from Ace 
    Tracker.autorun(function (e) { 
     var editorPy = AceEditor.instance("py_inPanel", { 
      theme:"dawn", 
      mode:"python" 
     }); 
     if(editorPy.loaded === true){ 
      e.stop(); 
      Session.set("pyContent", editorPy.getValue()); 
     } 
    }); 
    // Skulpt 
    var prog = Session.get("pyContent"); 
    var mypre = document.getElementById("py_outputPanel"); 
    mypre.innerHTML = ''; 

    Sk.onAfterImport = function(library) { 
     switch(library) { 
      case 'turtle': 
       console.log('turtle loaded'); // *** CHECK TO SEE IF TURTLE LOADED *** 
       break; 
     } 
    } 

    Sk.pre = "py_outputPanel"; // output 
    Sk.configure({ 
     inputfun: function(prompt) { 
      return window.prompt(prompt); // for raw_input() 
     }, 
     inputfunTakesPrompt: true, 
     output: outf, 
     read: builtinRead 
    }); 
    (Sk.TurtleGraphics || (Sk.TurtleGraphics = {})).target = 'py_turtlePanel'; 
    var myPromise = Sk.misceval.asyncToPromise(function() { 
     return Sk.importMainWithBody("<stdin>", false, prog, true); 
    }); 
    myPromise.then(function(mod) { 
     console.log('Python - success'); // *** CHECK TO SEE IF PYTHON RAN *** 
    }, function(err) { 
     console.log(err.toString()); 
    }); 
    // resize output panel 
    template.$(".py_panel").height($(window).height() - template.$("#py_header").height() - 90); 
}, 
... 

而且這裏是我的html代碼:

<template name="pythonEnv"> 
{{#with file}} 
    <div id="py_container"> 
     <div id="py_header"> 
      <div id="py_buttonContainer"> 
       <p id="py_toggle_note">Click to toggle: &nbsp; </p> 
       <div class="py_toggleButton py_active" id="py_in">Python</div> 
       <div class="py_toggleButton py_active" id="py_output">Output</div> 
       <div class="py_toggleButton" id="py_turtle">Turtle</div> 
      </div> 
      <button class="login" id="py_run">Run!</button> 
     </div> 
     <div id="py_bodyContainer"> 
      <pre id="py_inPanel" class="py_input py_panel"></pre> 
      <pre id="py_outputPanel" class="py_panel"></pre> 
      <canvas id="py_turtlePanel" class="py_panel py_hidden"></canvas> 
     </div> 
    </div> 
{{/with}} 
</template> 

修訂: 我加入這個代碼在我'click #py_run'功能:

var canvas = document.getElementById("py_turtlePanel"); 
var context = canvas.getContext("2d"); 
context.fillStyle = "blue"; 
context.font = "bold 16px Arial"; 
context.fillText("Zibri", (canvas.width/2) - 17, (canvas.height/2) + 8); 

而且它作品!但我的烏龜仍然無法加載。我試着運行下面的龜代碼:

import turtle 

t = turtle.Turtle() 

for c in ['red', 'green', 'yellow', 'blue']: 
    t.color(c) 
    t.forward(75) 
    t.left(90) 

沒有任何運氣。此外,所有使用Skulpt-Python功能的東西似乎也能正常工作。

預先感謝您!

+0

你可以輸出其他東西到你的畫布? Python解釋器是否工作?是否有其他軟件包可以嘗試加載? – Mikkel

+0

@Mikkel剛剛爲我更新了我的帖子 – kstarboi

+0

@Mikkel並且對您的觀點,我嘗試導入numpy和pygal作爲客戶端,並且我收到:'ImportError:兩次都沒有在第10行上名爲numpy的模塊。 – kstarboi

回答

0

所以事實證明,問題是我讀了http://www.skulpt.org/#給出的初始代碼錯誤。我應該將它放在<div id="mycanvas"></div>中,而不是將我的烏龜代碼放在<canvas id="mycanvas"></canvas>中。