Skip to content

Commit f9b8496

Browse files
committed
More examples
1 parent 3f503f5 commit f9b8496

File tree

1 file changed

+29
-2
lines changed
  • examples/graphics/source/examples

1 file changed

+29
-2
lines changed

examples/graphics/source/examples/Python.h

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,20 @@ class PythonDemo : public yup::Component
3434
runPython.setButtonText("Run Python!");
3535
runPython.onClick = [this]
3636
{
37-
engine.runScript("print ('Hello, World!')");
37+
pybind11::dict locals;
38+
locals["custom"] = pybind11::module_::import ("custom");
39+
locals["yup"] = pybind11::module_::import (yup::PythonModuleName);
40+
locals["this"] = pybind11::cast (this);
41+
42+
auto result = engine.runScript (yup::String (R"(
43+
import sys
44+
print("Scripting YUP!")
45+
this.backgroundColor = yup.Color.opaqueRandom()
46+
this.repaint();
47+
)").dedentLines(), locals);
48+
49+
if (result.failed())
50+
std::cout << result.getErrorMessage();
3851
};
3952
addAndMakeVisible (runPython);
4053
}
@@ -53,11 +66,25 @@ class PythonDemo : public yup::Component
5366

5467
void paint (yup::Graphics& g) override
5568
{
56-
g.setFillColor (findColor (yup::DocumentWindow::Style::backgroundColorId).value_or (yup::Colors::dimgray));
69+
g.setFillColor (backgroundColor);
5770
g.fillAll();
5871
}
5972

73+
yup::Color backgroundColor = yup::Colors::transparentBlack;
74+
6075
private:
6176
yup::TextButton runPython;
6277
yup::ScriptEngine engine;
6378
};
79+
80+
//==============================================================================
81+
82+
PYBIND11_EMBEDDED_MODULE(custom, m)
83+
{
84+
namespace py = pybind11;
85+
86+
py::module_::import (yup::PythonModuleName);
87+
88+
py::class_<PythonDemo, yup::Component> (m, "PythonDemo")
89+
.def_readwrite ("backgroundColor", &PythonDemo::backgroundColor);
90+
}

0 commit comments

Comments
 (0)