Platforms to show: All Mac Windows Linux Cross-Platform
Required plugins for this example: MBS Python Plugin
Last modified Sun, 2nd May 2026.
You find this example project in your MBS Xojo Plugin download as a Xojo project file within the examples folder: /Python/Python
Download this example: Python.zip
Project "Python.xojo_binary_project"
Class App Inherits DesktopApplication
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
EventHandler Sub Opening()
#if TargetMacOS then
Const path = "/opt/homebrew/Cellar/python@3.12/3.12.9/Frameworks/Python.framework/Versions/3.12/Python"
#ElseIf Targetwindows
Const path = "C:\Users\cs\AppData\Local\Programs\Python\Python312\python312.dll"
#ElseIf TargetLinux Then
Const path = "libpython3.11.so"
#Else
#Pragma error Invalid Platform?
#EndIf
Var loaded As Boolean = PythonMBS.Load(path)
If loaded Then
MessageBox "Version: "+PythonMBS.LibraryVersion
else
MessageBox "Error: "+PythonMBS.LibraryError
End If
End EventHandler
End Class
Class MainWindow Inherits DesktopWindow
Control CodeText Inherits DesktopTextArea
ControlInstance CodeText Inherits DesktopTextArea
End Control
Control Result Inherits DesktopTextArea
ControlInstance Result Inherits DesktopTextArea
End Control
Control PrintLog Inherits DesktopTextArea
ControlInstance PrintLog Inherits DesktopTextArea
End Control
Control EvalButton Inherits DesktopButton
ControlInstance EvalButton Inherits DesktopButton
EventHandler Sub Pressed()
run False
End EventHandler
End Control
Control RunButton Inherits DesktopButton
ControlInstance RunButton Inherits DesktopButton
EventHandler Sub Pressed()
run True
End EventHandler
End Control
Control InputText Inherits DesktopTextArea
ControlInstance InputText Inherits DesktopTextArea
End Control
Control RunButton1 Inherits DesktopButton
ControlInstance RunButton1 Inherits DesktopButton
EventHandler Sub Pressed()
var t as New MyThread
#If XojoVersion >= 2025
t.Type = Thread.Types.Preemptive
#endif
t.InputLines = Split(InputText.Text.ReplaceLineEndings(EndOfLine), EndOfLine)
t.code = CodeText.Text
t.Run
End EventHandler
End Control
EventHandler Sub Opening()
p = New Python
// read and write local variables
p.Value("test") = "Hello World"
Var v1 As Variant = p.Value("test")
// and add custom functions
p.AddCustomFunction "Test"
p.AddCustomFunction "UserName"
p.AddCustomFunction "Sum"
// also available with delegate
p.AddCustomFunction "Product", AddressOf DelegateTestFunction
// Try it:
Var r As Variant = p.Evaluate("1+2")
End EventHandler
Function DelegateTestFunction(args() as Variant) As Variant
Var d As Double = 1
For Each a As Double In args
d = d * a
Next
Return d
Exception r As RuntimeException
Return Introspection.GetType(r).name+": "+r.message
End Function
Sub Run(AsScript as Boolean)
// clear logs
result.Text = ""
PrintLog.Text = ""
// setup data for input function
p.InputLines = Split(InputText.Text.ReplaceLineEndings(EndOfLine), EndOfLine)
// run it
Var r As Variant
If AsScript Then
r = p.run(CodeText.Text)
Else
r = p.Evaluate(CodeText.Text)
End If
// show result
result.Text = r.StringValue
Exception rr As RuntimeException
MessageBox Introspection.GetType(rr).name+": "+rr.message
End Sub
Property Code As string
Property p As Python
End Class
MenuBar MainMenuBar
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem EditSeparator1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem EditSeparator2 = "-"
MenuItem EditSelectAll = "Select &All"
MenuItem HelpMenu = "&Help"
End MenuBar
Sign
End Sign
Class Python Inherits PythonMBS
EventHandler Sub Flush()
End EventHandler
EventHandler Function FunctionCall(FunctionName as String, Args() as Variant) As Variant
Select Case FunctionName
Case "Test"
Return Args(0)
Case "UserName"
Return "test" // SystemInformationMBS.UserName
Case "Sum"
Var d As Double = 0
For Each v As Double In args
d = d + v
Next
Return d
End Select
Exception r As RuntimeException
Return Introspection.GetType(r).name+": "+r.message
End EventHandler
EventHandler Function Input(Args() as Variant) As String
If InputLines.Count > 0 Then
Var r As String = InputLines(0)
InputLines.RemoveAt 0
Return r
End If
Exception r As RuntimeException
Return Introspection.GetType(r).name+": "+r.message
End EventHandler
EventHandler Sub Print(text as String)
If Thread.Current = Nil Then
// main thread
MainWindow.PrintLog.AddText Text
Else
Thread.Current.AddUserInterfaceUpdate "text":Text
End If
End EventHandler
Property InputLines() As String
End Class
Class MyThread Inherits Thread
EventHandler Sub Run()
// make our own instance
p = New Python
p.InputLines = InputLines
// read and write local variables
p.Value("test") = "Hello World"
Var v1 As Variant = p.Value("test")
// and add custom functions
p.AddCustomFunction "Test"
p.AddCustomFunction "UserName"
p.AddCustomFunction "Sum"
// also available with delegate
p.AddCustomFunction "Product", AddressOf DelegateTestFunction
// Try it:
call p.run(Code)
End EventHandler
EventHandler Sub UserInterfaceUpdate(data() as Dictionary)
For Each d As Dictionary In data
var text as string = d.Value("text")
MainWindow.PrintLog.AddText Text
next
End EventHandler
Function DelegateTestFunction(args() as Variant) As Variant
Var d As Double = 1
For Each a As Double In args
d = d * a
Next
Return d
Exception r As RuntimeException
Return Introspection.GetType(r).name+": "+r.message
End Function
Property Code As string
Property InputLines() As string
Property p As Python
End Class
End Project
Download this example: Python.zip
The items on this page are in the following plugins: MBS Python Plugin.