Skip to content

Commit 63f727b

Browse files
committed
Find the Python widget for a given HTML element
1 parent c2e53cf commit 63f727b

File tree

5 files changed

+18
-16
lines changed

5 files changed

+18
-16
lines changed

examples/canvas.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
def create():
88

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

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

@@ -23,14 +23,14 @@ def mousemove(event):
2323
return (
2424
ltk.VBox(
2525
ltk.Heading2("This is a Canvas. Move the mouse to draw."),
26-
canvas
27-
.attr("id", "pink-canvas") \
28-
.on("mousemove", ltk.proxy(mousemove)) \
29-
.attr("width", "500px") \
30-
.attr("height", "500px") \
31-
.css("width", "500px") \
32-
.css("height", "500px") \
33-
.css("border", "1px solid gray") \
26+
ltk.Canvas()
27+
.attr("id", "pink-canvas")
28+
.on("mousemove", ltk.proxy(mousemove))
29+
.attr("width", "500px")
30+
.attr("height", "500px")
31+
.css("width", "500px")
32+
.css("height", "500px")
33+
.css("border", "1px solid gray")
3434
.css("background", "pink")
3535
)
3636
.attr("name", "Canvas")

ltk/ltk.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,12 @@
134134
return elementBottom > viewportTop && elementTop < viewportBottom;
135135
};
136136

137+
window.ltk_widgets = {};
138+
139+
$.fn.widget = function() {
140+
return window.ltk_widgets[$(this).attr("ltk_id")];
141+
};
142+
137143
// change the following to your own development root location
138144
window.development_location = "C:/Users/laffr/dev/ltk";
139145

ltk/widgets.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ def __init__(self, *args):
141141
.addClass(" ".join(self.classes))
142142
.append(*self._flatten(args))
143143
)
144+
window.ltk_widgets[id(self)] = self
145+
self.attr("ltk_id", id(self))
144146
self._handle_css(args)
145147
if Widget.INSPECT:
146148
self.on("mousemove", proxy(lambda event: self._on_mousemove(event)))

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ build-backend = "setuptools.build_meta"
66

77
[project]
88
name = "pyscript-ltk"
9-
version = "0.2.15"
9+
version = "0.2.16"
1010
description = "A little toolkit for writing UIs in PyScript"
1111
readme = "README.md"
1212
authors = [{ name = "Chris Laffra", email = "chris@chrislaffra.com" }]

widgets.py

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)