Canvas in Gtk doesn't work while the loop is running

I’m trying to create a “game of life” with GUI (using Gtk.jl) but while the loop is running the canvas doesn’t work (the Windows is not visible,as if it was minimized) anyone knows why?

1 Like

It’s difficult to help without any code, can you provide a MWE?

Please have a look at

where I give a minimal example that works.

1 Like

yeah my code is similar but I put the show function in a loop to update the canvas, probably the canvas doesn’t update because the loop is so fast. Thanks

1 Like

you should not call the show function in a loop. This is not how it works. draw is automatically called whenever something changes.

It’s not straight forward to draw in GtkCanvas -es (and especially in julia where someone found it a good idea to double-buffer locally).

afair (i cannot test right now)
show(canvas) is establishing the widget into the current hierarchy
draw(canvas) is sending an ‘expose’/redraw event
so usually you get the gc from the canvas, draw on it (with Cairo commands) and call draw(canvas).

I made (long time ago, but should be portable) an timed/animation demo:

2 Likes

My Cellular model framework has a Gtk output. You should never need to use yield() for visualisation, just update the canvas in the loop:

https://github.com/rafaqz/Cellular.jl/blob/master/src/outputs/gtk.jl
https://github.com/rafaqz/Cellular.jl/blob/master/src/framework.jl

2 Likes

I have looked at your code and for some reason I cannot see which feature helps it perform a new draw upon subsequent calls in a function loop. I tried running the code which has an error referencing an issue to the Scene.

I have some similar code to try to do the multiple drawings between sleep calls and the only canvas update is upon the last call where an update is seen

function toggle_Button1(Widget)
    global c1,c2
    println("in toggle button")
    for i in 1:3
        tmpFn(c1)
        sleep(1)        
        println("loop")
    end
end

function tmpFn(c1)
    show(c1)
    Gtk.draw(c1)
end

I have looked at the code you link to but have not been able to pull out the feature to inclue inorder to get the update to take place within a loop or prior to exiting a function space. I have asked a question providing a MWE, it would be appeciated if you can see what can be added to make its canvas in an update loop realize on the canvas.

There is actually a sleep() in our simulation to get the framerate right. I hadn’t quite understood that yet a year ago obviously…

We needed yield as well for WebIO/Interact but not Gtk, but that was for input, and sleep() is enough for output, even if sleep is 0 most of the time it still interrupts.

1 Like