I’m trying to learn Julia Language and write a five-in-a-line game using Julia Gtk. Here are my current sketches for the interface:
using Gtk cur_step = "x" function click_once_callback(widget) set_gtk_property!(widget, :sensitive, false) set_gtk_property!(widget, :label, cur_step) if cur_step == "x" cur_step = "o" else cur_step = "x" end end letters = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o'] win = GtkWindow("GoMoku") g = GtkGrid() buttons = [] for i=1:15 b = [] for j=1:15 letter = letters[i] push!(b,GtkButton("$letter:$j")) end push!(buttons,b) end for i=1:15 for j=1:15 g[i,16-j] = buttons[i][j] id = signal_connect(click_once_callback, buttons[i][j], "clicked") end end set_gtk_property!(g, :column_homogeneous, true) set_gtk_property!(g, :column_spacing, 15) # introduce a 15-pixel gap between columns set_gtk_property!(g, :row_spacing, 15) # introduce a 15-pixel gap between rows push!(win, g) showall(win) Problem: The variable cur_step does not change from the callback function, and generally when trying to change it, the label of the buttons is not set to the value of cur_step. If you remove all the work with cur_step in the function, except for setting its value in the label of the current button, then everything works as expected - the button receives the inscription "x" after pressing and stops being active.
I tried to declare the cur_step variable via global, this did not solve the problem. Can you please tell me why working with a variable outside the function causes such effects?