Open jarvisteach opened 6 years ago
Maybe updating drawings on a canvas could be a good improvement.
I did a coloured dice
dicecolors = ["blue", "red", "white", "yellow", "green"]
dicecolor = random.choice(dicecolors)
app.addCanvas("dice")
app.addCanvasRectangle("dice", x=40, y=80, w=100, h=100, fill=dicecolor)
app.addNamedButton("Launch", title="dicebutton", func=dice)
def dice(button):
if button == "dicebutton":
dicecolor = random.choice(dicecolors)
app.clearCanvas("dice")
app.addCanvasRectangle("dice", x=40, y=80, w=100, h=100, fill=dicecolor)
What about updating the "fill" argument instead of clearing and adding a new Canvas rectangle ?
It's already possible to just update the fill colour, just get a handle on the canvas & rectangle, then callitemconfig()
:
import random
from appJar import gui
dicecolors = ["blue", "red", "white", "yellow", "green"]
def dice():
canvas.itemconfig(rect, fill=random.choice(dicecolors))
with gui() as app:
dicecolor = random.choice(dicecolors)
canvas = app.addCanvas("dice")
rect = app.addCanvasRectangle("dice", x=40, y=80, w=100, h=100, fill=dicecolor, tag="dice")
app.button("Launch", dice)
Oh thanks.
That's excellent. I didn't see that in the documentation.
Thanks for clarifying
Canvas needs to function as both a widget and a container.
Should be able to start a canvas and place widgets in it, as well as add/get/set a canvas.
This will probably require making a new class, that implements the contextmanager functions, but a bit more research needs to be done.