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

**URL:** <https://discourse.julialang.org/t/canvas-in-gtk-doesnt-work-while-the-loop-is-running/15398>\
**Category:** Internals & Design\
**Tags:** question\
**Created:** [September 23, 2018, 8:02pm UTC](https://discourse.julialang.org/t/canvas-in-gtk-doesnt-work-while-the-loop-is-running/15398 "2018-09-23T20:02:46Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![dsHitman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dshitman/32/5465_2.png) [@dsHitman](https://discourse.julialang.org/u/dsHitman)\
**Post date:** [September 23, 2018, 8:02pm UTC](https://discourse.julialang.org/t/canvas-in-gtk-doesnt-work-while-the-loop-is-running/15398/1 "2018-09-23T20:02:46Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [September 23, 2018, 9:20pm UTC](https://discourse.julialang.org/t/canvas-in-gtk-doesnt-work-while-the-loop-is-running/15398/3 "2018-09-23T21:20:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tobias.knopp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tobias.knopp/32/7551_2.png) [@tobias.knopp](https://discourse.julialang.org/u/tobias.knopp)\
**Post date:** [September 23, 2018, 9:30pm UTC](https://discourse.julialang.org/t/canvas-in-gtk-doesnt-work-while-the-loop-is-running/15398/4 "2018-09-23T21:30:54Z")

</div>

Please have a look at

> [@Setting size of GtkCanvas and other widgets](https://discourse.julialang.org/t/setting-size-of-gtkcanvas-and-other-widgets/14658):
>
> I’m wanting to make a GUI with several items - I made a GtkBox(:v), and pushed in a GtkBox(:h), followed by a GtkCanvas(), then another GtkBox(:h). The hBoxes contain buttons, labels, etc. When I call showall(win) it brings up the two hBoxes Ok, but the canvas is sqeezed down to a thin line. What I want is for it to fill the space between the hboxes. If I call GtkCanvas(width,height) then that gives me a useable canvas, but it prevents me from resizing the window to less than that size. I t…

where I give a minimal example that works.

---

<div class="post-metadata">

**Author:** ![dsHitman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dshitman/32/5465_2.png) [@dsHitman](https://discourse.julialang.org/u/dsHitman)\
**Post date:** [September 24, 2018, 9:12am UTC](https://discourse.julialang.org/t/canvas-in-gtk-doesnt-work-while-the-loop-is-running/15398/5 "2018-09-24T09:12:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tobias.knopp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tobias.knopp/32/7551_2.png) [@tobias.knopp](https://discourse.julialang.org/u/tobias.knopp)\
**Post date:** [September 24, 2018, 9:15am UTC](https://discourse.julialang.org/t/canvas-in-gtk-doesnt-work-while-the-loop-is-running/15398/6 "2018-09-24T09:15:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lobingera](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lobingera/32/211_2.png) [@lobingera](https://discourse.julialang.org/u/lobingera)\
**Post date:** [September 24, 2018, 10:46am UTC](https://discourse.julialang.org/t/canvas-in-gtk-doesnt-work-while-the-loop-is-running/15398/7 "2018-09-24T10:46:24Z")

</div>

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:

> <https://gist.github.com/lobingera/712c886cc75ca6019e49>

---

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [September 24, 2018, 1:39pm UTC](https://discourse.julialang.org/t/canvas-in-gtk-doesnt-work-while-the-loop-is-running/15398/8 "2018-09-24T13:39:42Z")

</div>

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/outputs/gtk.jl)  
[https://github.com/rafaqz/Cellular.jl/blob/master/src/framework.jl](https://github.com/rafaqz/Cellular.jl/blob/master/src/framework.jl)

---

<div class="post-metadata">

**Author:** ![mantzaris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mantzaris/32/3852_2.png) [@mantzaris](https://discourse.julialang.org/u/mantzaris)\
**Post date:** [August 6, 2019, 8:35pm UTC](https://discourse.julialang.org/t/canvas-in-gtk-doesnt-work-while-the-loop-is-running/15398/9 "2019-08-06T20:35:26Z")

</div>

> [@lobingera](#):
>
> 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 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

```julia
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

```

---

<div class="post-metadata">

**Author:** ![mantzaris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mantzaris/32/3852_2.png) [@mantzaris](https://discourse.julialang.org/u/mantzaris)\
**Post date:** [August 7, 2019, 9:04pm UTC](https://discourse.julialang.org/t/canvas-in-gtk-doesnt-work-while-the-loop-is-running/15398/10 "2019-08-07T21:04:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [August 8, 2019, 8:20am UTC](https://discourse.julialang.org/t/canvas-in-gtk-doesnt-work-while-the-loop-is-running/15398/11 "2019-08-08T08:20:39Z")

</div>

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.
