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: </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功能的东西似乎也能正常工作。
预先感谢您!
你可以输出其他东西到你的画布? Python解释器是否工作?是否有其他软件包可以尝试加载? – Mikkel
@Mikkel刚刚为我更新了我的帖子 – kstarboi
@Mikkel并且对您的观点,我尝试导入numpy和pygal作为客户端,并且我收到:'ImportError:两次都没有在第10行上名为numpy的模块。 – kstarboi