Variable global scope

In creating a Julia script for interactive plotting of compound interest and mortgages, I’ve run into a problem. Here’s a skeleton of the essential parts:

using Gtk
w, h = 200, 250
canvas = @GtkCanvas()
win = GtkWindow(canvas, "Interest", w, h)
amount = 1000
initialamount = 1000

function plotcurve()
  return 999
end 

@guarded draw(canvas) do widget
    gc = getgc(canvas)
    global amount
    amount = plotcurve()
    println("draw set amount=$amount") # amount changed correctly
end # do

function process()
println("process called with amount = $amount")
  draw(canvas)
  showall(win)
  println("process: initial amount=$initialamount, finalamount=$amount") # amount unchanged
end # process function

process()
  println("initial amount=$initialamount, finalamount=$amount")  # amount unchanged

Here’s the result of running it as an included script:

julia> include("desktop/globaltest.jl.txt")
process called with amount = 1000
process: initial amount=1000, finalamount=1000
initial amount=1000, finalamount=1000

julia> draw set amount=999

How can the correct value of amount, as set in the draw function, be passed to the global variable, as needed in the full implementation? As this is for a course I’m creating, the simplest solution is desirable. Thanks for any help.

Sorry, I was not able to reproduce the problem, I am on

julia> versioninfo()
Julia Version 1.5.3
Commit 788b2c77c1 (2020-11-09 13:37 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-9.0.1 (ORCJIT, skylake)

and for me using Gtk gives

[ Info: Precompiling Gtk [4c0ca9eb-093a-5379-98c5-f87ac0bbbf44]
ERROR: LoadError: LoadError: InitError: could not load library "/home/henrique/.julia/artifacts/079e2f87254bb2465a44de255cd5dde50d4d0b62/lib/libpangocairo-1.0.so"
/usr/lib/libpangoft2-1.0.so.0: undefined symbol: pango_coverage_get_type
...
ERROR: LoadError: Failed to precompile GTK3_jll [77ec8976-b24b-556a-a1bf-49a033a670a6] to /home/henrique/.julia/compiled/v1.5/GTK3_jll/D35Um_6iwJ1.ji.
...
ERROR: Failed to precompile Gtk [4c0ca9eb-093a-5379-98c5-f87ac0bbbf44] to /home/henrique/.julia/compiled/v1.5/Gtk/Vjnq0_6iwJ1.ji.
1 Like

Well, thanks for trying. My Platform Info is just like yours except on MacBookPro, so another entry for the Journal Of Irreproducible Results. It’s now clear that JuliaGtk does not support variables with global scope, so I’ve rewritten the original example so the calculations are done outside the draw function.