# How to structure a Gtk app

**URL:** <https://discourse.julialang.org/t/how-to-structure-a-gtk-app/89647>\
**Category:** General Usage\
**Tags:** gtk\
**Created:** [November 2, 2022, 7:26am UTC](https://discourse.julialang.org/t/how-to-structure-a-gtk-app/89647 "2022-11-02T07:26:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [November 2, 2022, 7:26am UTC](https://discourse.julialang.org/t/how-to-structure-a-gtk-app/89647/1 "2022-11-02T07:26:00Z")

</div>

When looking at Gyk examples a lot of the apps use global variables. How can this be avoided while keeping access to the model values(like data, a figure object,…)

What I saw

```julia
using Gtk

win = GtkWindow()

f = Figure()

function on_event(w)
   %do something with f
end

```

In object orientet languages this is often done using properties. In julia I only see globals or closures as a possibility. Are there better options? Passing the model around is not useful as the api for Gtk is not designed that way.

```julia
function main()
 win = GtkWindow()

 f = Figure()
 
 function on_event()
    % do something with f
 end
end

```

---

<div class="post-metadata">

**Author:** ![tknopp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tknopp/32/3569_2.png) [@tknopp](https://discourse.julialang.org/u/tknopp)\
**Post date:** [November 2, 2022, 7:42am UTC](https://discourse.julialang.org/t/how-to-structure-a-gtk-app/89647/2 "2022-11-02T07:42:34Z")

</div>

use structs or mutable structs? We also have in the documentation an example how to create a composite widget using a struct. I structure all my code like this.

---

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [November 2, 2022, 8:39pm UTC](https://discourse.julialang.org/t/how-to-structure-a-gtk-app/89647/3 "2022-11-02T20:39:35Z")

</div>

Thank you for the tip. How would I apply this to `GtkCanvas`. As I noticed `GtkCanvas` is not an abstract structure so I can’t derive a structure from it.

---

<div class="post-metadata">

**Author:** ![tknopp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tknopp/32/3569_2.png) [@tknopp](https://discourse.julialang.org/u/tknopp)\
**Post date:** [November 3, 2022, 7:30am UTC](https://discourse.julialang.org/t/how-to-structure-a-gtk-app/89647/4 "2022-11-03T07:30:46Z")

</div>

`GtkCanvas` is not a Gtk type but a custom creation within `Gtk.jl`. You can just put it into a `Box` or a `Grid` and then you can follow [Custom/Composed Widgets · Gtk.jl](https://juliagraphics.github.io/Gtk.jl/latest/manual/customWidgets/)

By the way: This “inheriting” is not necessary if you don’t want your struct to behave like a widget. Your main application, for instance, does not need that. It would usually look like:

```julia
mutable struct MyApp
  win::GtkWindow
  ... # further widgets...
end

```
