MetaCoreEngineV2/Demos/B_Button.py
2026-01-13 17:06:06 +08:00

65 lines
1.6 KiB
Python

"""
This file shows the smallest working LUI example, on which you can base your
LUI projects.
"""
# Add lui modules to the path. This will not be required when LUI is included
# in panda.
import sys
from pathlib import Path
lui_path = Path(__file__).parent.parent.absolute()
builtin_path = lui_path / "Builtin"
# Make sure the compiled lui.pyd and Builtin helpers can be found
sys.path.insert(0, str(lui_path))
sys.path.insert(0, str(builtin_path))
# Load Panda3D core first so the dll dependencies for lui are present, then
# register the bundled lui module as panda3d.lui (expected by wrappers).
import panda3d.core
import lui
import panda3d
panda3d.lui = lui
sys.modules["panda3d.lui"] = lui
from DemoFramework import DemoFramework
from LUIButton import LUIButton
from LUIHorizontalLayout import LUIHorizontalLayout
import random
f = DemoFramework()
f.prepare_demo("LUIButton")
# Constructor
f.add_constructor_parameter("text", "u'Button'")
f.add_constructor_parameter("template", "'ButtonDefault'")
# Functions
f.add_public_function("set_text", [("text", "string")])
f.add_public_function("get_text", [], "string")
f.add_property("text", "string")
# Construct source code
f.construct_sourcecode("LUIButton")
# Create 2 new buttons, and store them in a vertical layout
layout = LUIHorizontalLayout(parent=f.get_widget_node(), spacing=10)
button1 = LUIButton(text="Do not click me")
button2 = LUIButton(text="Instead click me", template="ButtonGreen")
layout.add(button1)
layout.add(button2)
def set_btn_text(text):
button1.text = text
f.set_actions({
"Set Random Text": lambda: set_btn_text("Text: " + str(random.randint(100, 10000000))),
})
run()