# Updating gui via callback using Gtk

**URL:** https://discourse.julialang.org/t/updating-gui-via-callback-using-gtk/34804
**Category:** General Usage
**Tags:** question, package, guis
**Created:** [February 18, 2020, 11:39am UTC](https://discourse.julialang.org/t/updating-gui-via-callback-using-gtk/34804 "2020-02-18T11:39:41Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Noobie76](https://avatars.discourse-cdn.com/v4/letter/n/ee59a6/32.png) [@Noobie76](https://discourse.julialang.org/u/Noobie76)
#### Post date: [February 18, 2020, 11:39am UTC](https://discourse.julialang.org/t/updating-gui-via-callback-using-gtk/34804/1 "2020-02-18T11:39:42Z")

</div>

Hi, I’m currently working on a GUI interface for PDE solver with `Glk`. This Interface should be very basic and just should it make more comfortable to play around with system parameters to reach the best results ffor images. The problem I have is very elementary but unfortunately not covered by the [documentation](http://juliagraphics.github.io/Gtk.jl/v0.11/manual/textwidgets.html). (See code below)

For my solver I have a file called `parameters.jl` which will be included at the beginning, it defines the values of e.g. `x_min`. These values are used in the algorithm and to label certain text fields in the GUI via `set_gtk_property(name, :label, "string")`. In order to change them, I would like to insert the new values in the text fields and they should be changed by hitting the button `Start`. Now the updated values should appear as the “new labels”.

To achieve this I use the callback function `on_btn_start_clicked`. The function can read out the value from the text field but it won’t set the variable e.g `x_min` to the new value and the GUI won’t be refreshed with the new value (it now should display the updated value for the next run).

(I just posted the relevant pieces of my code here.)

Cheers

```julia
using Gtk

include("parameters.jl")
include("solver.jl")

win = Window("PDE Solver", 1000, 1000)
g = Grid()

#current varable values
lbl_boxlength = set_gtk_property!(lbl_boxlength, :label, string("Boxlength=" , x_max - x_min))
lbl_gridpoints = set_gtk_property!(lbl_gridpoints, :label, string("Jx=",Jx))
lbl_timestep = set_gtk_property!(lbl_timestep, :label, string("dt=",dt))
lbl_ending_time = set_gtk_property!(lbl_ending_time, :label, string("T=",T))
lbl_epsilon = set_gtk_property!(lbl_epsilon, :label, string("epsilon=",epsilon))
lbl_current = GtkLabel("current values")
lbl_system_parameters = GtkLabel("System parameters")
lbl_new = GtkLabel("insert new values")

variable_x_min = Entry()
variable_x_max = Entry()
variable_gridpoints = Entry()
variable_timestep = Entry()
variable_ending_time = Entry()
variable_epsilon = Entry()

set_gtk_property!(variable_x_min,:text,"min")

btn_start = GtkButton("Start")

# Cartesian coordinates, g[x,y]
g[3,1] = lbl_system_parameters
g[3,3] = lbl_current
g[4,3] = lbl_new

g[3,4] = lbl_boxlength
g[3,5] = lbl_gridpoints
g[3,6] = lbl_timestep
g[3,7] = lbl_ending_time
g[3,8] = lbl_epsilon

g[4,4] = variable_x_min
g[5,4] = variable_x_max
g[4,5] = variable_gridpoints
g[4,6] = variable_timestep
g[4,7] = variable_ending_time
g[4,8] = variable_epsilon

g[5,10] = btn_start

set_gtk_property!(g, :column_homogeneous, true)
set_gtk_property!(g, :column_spacing, 15) # introduce a 15-pixel gap between columns
push!(win, g)
showall(win)

function on_btn_start_clicked(widget)
  str_x_min = get_gtk_property(variable_x_min,:text,String)
  x_min = parse(str_x_min)
  set_gtk_property!(lbl_boxlength, :label, string("Boxlength=" , x_min))

  solver() #this starts the solver routine

end

```

My version

```julia
Julia Version 1.3.0
Commit 46ce4d7933 (2019-11-26 06:09 UTC)
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: Intel(R) Core(TM) i5-8350U CPU @ 1.70GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.1 (ORCJIT, skylake)
Environment:
  JULIA_EDITOR = "C:\Users\Noobie\AppData\Local\atom\app-1.44.0\atom.exe" -a
  JULIA_NUM_THREADS = 4

```

---

<div class="post-metadata">

### Author: ![jonathanBieler](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@jonathanBieler](https://discourse.julialang.org/u/jonathanBieler)
#### Post date: [February 18, 2020, 1:17pm UTC](https://discourse.julialang.org/t/updating-gui-via-callback-using-gtk/34804/2 "2020-02-18T13:17:23Z")

</div>

Here’s a small example of a button modifying an entry, is that what you want to do ?

```julia
using Gtk, Random

w = GtkWindow("")
entry = GtkEntry()

box = GtkBox(:v)
button = GtkButton("Randomize")

push!(box,entry)
push!(box,button)
push!(w,box)

@guarded (nothing) function button_cb(widgetptr::Ptr, entry)
    @info entry.text[String] #reading entry
    entry.text[String] = Random.randstring(10) #writing entry
    nothing              
end

signal_connect(button_cb, button, "clicked", Nothing, (), false, entry)
showall(w)

```

Note that `entry.text[String]` is equivalent to `get_gtk_property(entry, :text, String)` (we should maybe document that better).

---

<div class="post-metadata">

### Author: ![Noobie76](https://avatars.discourse-cdn.com/v4/letter/n/ee59a6/32.png) [@Noobie76](https://discourse.julialang.org/u/Noobie76)
#### Post date: [February 18, 2020, 8:39pm UTC](https://discourse.julialang.org/t/updating-gui-via-callback-using-gtk/34804/3 "2020-02-18T20:39:55Z")

</div>

Hi, thanks for your reply. It is close to, but I don’t want to rewrite the `variable_gridpoints`. Instead I want to update the label `lbl_gridpoints` and the variable `Jx` which is an integer in the `paramters.jl` file (since the new value of `Jx` should be used by the solver routine solver() )

The only not working part is to refresh `win` and to export the new value of `Jx`. Placing a `return Jx` in the callbackfunction only would produce a warning

```julia
(julia.exe:12460): Gtk-WARNING **: 21:43:49.315: Attempting to add a widget with type GtkGrid to a container of type GtkWindow, but the widget is already inside a container of type GtkWindow, please remove the widget from its existing container first.

```

Cheers

```julia
using Gtk

include("parameters.jl")
include("solver.jl")

win = GtkWindow("PDE", 1000, 1000)
g = GtkGrid()

#current varable values

lbl_gridpoints = GtkLabel(string("Jx=",Jx))

variable_gridpoints = GtkEntry()

btn_start = GtkButton("Start")

function button_cb(widgetptr::Ptr, variable_gridpoints)
     variable_gridpoints.text[String] #reading entry
     Jx = parse(Int,variable_gridpoints.text[String]) # setting the readed entrie to the variable Jx
     push!(win,g) # should refresh the label lbl_gridpoints
     solver() # solve the pde with the updated Jx
     #return Jx
     nothing
end

signal_connect(button_cb, btn_start, "clicked", Nothing, (), false, variable_gridpoints)

g[3,5] = lbl_gridpoints

g[4,5] = variable_gridpoints

g[5,10] = btn_start

set_gtk_property!(g, :column_homogeneous, true)
set_gtk_property!(g, :column_spacing, 15) # introduce a 15-pixel gap between columns
push!(win, g)
showall(win)

```
