For a course I’m writing on Julia + Gtk, I want to include an alert function that waits for a user-specified time and then displays a message, converted from my working Python version. The Julia version fails, and I’ve included a minimum working example of the failure. The function test() draws a rectangle when called from the button click callback, but stops the timer callback process(t) when called from process, as can be demonstrated by changing the variable autocall to true. This sounds like a stack management error. I’ll be grateful for any ideas on how to make this work.
Macbook Pro OS 10.14, Julia 1.5.3 and 1.0.3
# alertfaildemo.jl
# The function process() prints the time every second.
# When the button is clicked, the function test() draws a rectangle.
# If autocall is set true, the timer call fails and the autoprint stops
using Gtk, Dates
autocall = false # Fails if this is changed to true
holdsecs = 1.0 # Wait time between time displays
status = 0 # 0, 1 for off, on
tprevdis = 0. # Time of previous display
button = GtkButton("Press to test")
win = GtkWindow("Draw Failure Demo")
canvas = @GtkCanvas(400, 50)
set_gtk_property!(canvas, :expand, true)
g = GtkGrid()
push!(win, g)
g[1,1] = button
g[1,2] = canvas
showall(win)
function onbuttonclick(button)
global status
status = 1 - status
label = "Run"
if status == 0
label = "Stop"
end
Gtk.GAccessor.label(button, label)
println("button clicked, status=$status")
test()
end # onbuttonclick
id = signal_connect(onbuttonclick, button, "clicked")
function test()
println("test called")
ctx=getgc(canvas)
set_source_rgb(ctx, rand(), rand(), rand()) # random color on each call
rectangle(ctx, 0, 0, width(canvas)/2, height(canvas)/2)
fill(ctx)
return
end
function process(t)
global tprevdis
tnow = time() # since the epoch
if tnow - tprevdis < holdsecs || status == 1 return end
if autocall
test() # Stops future calls
end
println(Dates.format(Dates.now(), Dates.RFC1123Format))
tprevdis = tnow
end
t = Timer(process, 0, interval = 0.5) # Check every half sec