Skip to content

Commit

Permalink
Find the Python widget for a given HTML element
Browse files Browse the repository at this point in the history
  • Loading branch information
laffra committed Dec 13, 2024
1 parent c2e53cf commit 63f727b
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 16 deletions.
18 changes: 9 additions & 9 deletions examples/canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

def create():

canvas = ltk.Canvas()
colors = ["black", "red", "white", "blue", "green", "yellow", "purple", "teal", "orange"]

def mousemove(event):
canvas = ltk.find("#pink-canvas").widget()
canvas.fill_style = random.choice(colors)
canvas.fill_rect(event.offsetX, event.offsetY, 50, 50)

Expand All @@ -23,14 +23,14 @@ def mousemove(event):
return (
ltk.VBox(
ltk.Heading2("This is a Canvas. Move the mouse to draw."),
canvas
.attr("id", "pink-canvas") \
.on("mousemove", ltk.proxy(mousemove)) \
.attr("width", "500px") \
.attr("height", "500px") \
.css("width", "500px") \
.css("height", "500px") \
.css("border", "1px solid gray") \
ltk.Canvas()
.attr("id", "pink-canvas")
.on("mousemove", ltk.proxy(mousemove))
.attr("width", "500px")
.attr("height", "500px")
.css("width", "500px")
.css("height", "500px")
.css("border", "1px solid gray")
.css("background", "pink")
)
.attr("name", "Canvas")
Expand Down
6 changes: 6 additions & 0 deletions ltk/ltk.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@
return elementBottom > viewportTop && elementTop < viewportBottom;
};

window.ltk_widgets = {};

$.fn.widget = function() {
return window.ltk_widgets[$(this).attr("ltk_id")];
};

// change the following to your own development root location
window.development_location = "C:/Users/laffr/dev/ltk";

Expand Down
2 changes: 2 additions & 0 deletions ltk/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ def __init__(self, *args):
.addClass(" ".join(self.classes))
.append(*self._flatten(args))
)
window.ltk_widgets[id(self)] = self
self.attr("ltk_id", id(self))
self._handle_css(args)
if Widget.INSPECT:
self.on("mousemove", proxy(lambda event: self._on_mousemove(event)))
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "pyscript-ltk"
version = "0.2.15"
version = "0.2.16"
description = "A little toolkit for writing UIs in PyScript"
readme = "README.md"
authors = [{ name = "Chris Laffra", email = "chris@chrislaffra.com" }]
Expand Down
6 changes: 0 additions & 6 deletions widgets.py

This file was deleted.

0 comments on commit 63f727b

Please sign in to comment.