Using Gtk.jl
I would like to be able to produce an animation within a canvas widget. I have this MWE, which when run, produces a window with 2 buttons and a canvas. The first button each time pressed changes the canvas to present a rectangle of a randomly chosen color. The second button uses the Timer
object to call a function multiple times with a delay to again produce on the canvas a new random color, but there is strange behavior in that only the last iteration produces a visible change in the color. I have a println statement showing that the timer is calling the draw function multiple times, but only upon the final iteration is an effect seen. Is this to do with âdouble-bufferingâ? Is there a solution similar to plots with the display(plt)
function? Is there a queue function to flush a stack?
using Gtk
using Cairo
global canvasWidget
function b_handler1(Widget)
println("button1")
global canvasWidget
Gtk.draw(canvasWidget)
end
function b_handler2(Widget)
println("button2")
global canvasWidget
Gtk.draw(canvasWidget)
callFive(canvasWidget)
end
function callFive(canvasWidget)
i=0
cb(timer) = begin
(Gtk.draw(canvasWidget))
i+=1
println("i=$(i)")
end
t = Timer(cb, 1, interval=0.5)
wait(t)
sleep(5)
close(t)
end
function drawColor(Widget)
println("color")
ctx = getgc(Widget)
h = height(Widget)
w = width(Widget)
rectangle(ctx, 0, 0, w, h)
set_source_rgb(ctx, rand(), rand(), rand())
fill(ctx)
end
win = Gtk.Window("random colors")
boxV = Gtk.Box(:v)
push!(win,boxV)
button1 = Gtk.Button("change color once")
button2 = Gtk.Button("change color 5x")
signal_connect(b_handler1,button1, "clicked")
signal_connect(b_handler2,button2, "clicked")
push!(boxV,button1)
push!(boxV,button2)
canvasWidget = Gtk.Canvas(200,200)
canvasWidget.draw = drawColor
push!(boxV,canvasWidget)
Gtk.showall(win)
Running this code (self contained) should display the effect I am referring to with the second button, which I would like to run for 5 seconds, and update the colors every half a second.
This recent post by @tobias.knopp, https://discourse.julialang.org/t/threading-1-3-success-story/27111
, shows an updating bar by utilizing the new threads capabilities in Julia1.3
signal_connect(button, "clicked") do widget
Threads.@spawn doWork()
end
Would upgrading let me use this to solve the task, or is there another approach that can be used to update upon each draw call?