# Tkinter - Error on button-click

**URL:** <https://discourse.julialang.org/t/tkinter-error-on-button-click/64740>\
**Category:** General Usage\
**Created:** [July 16, 2021, 10:58am UTC](https://discourse.julialang.org/t/tkinter-error-on-button-click/64740 "2021-07-16T10:58:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![trilobit](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@trilobit](https://discourse.julialang.org/u/trilobit)\
**Post date:** [July 16, 2021, 10:58am UTC](https://discourse.julialang.org/t/tkinter-error-on-button-click/64740/1 "2021-07-16T10:58:47Z")

</div>

Hi,  
I’m playing around with Tkinter and don’t understand why I get an error, when  
I want to replace a text. The test program should start a new measurement,  
but something goes wrong, when the “Repeat”-Button is clicked. The code  
I’m playing with it.

```julia
using Base.Threads, Tk

l=ReentrantLock()
cn1=Channel{Any}(1)
val1=Atomic{Float64}(0)
val2=Atomic{Float64}(0)

function mt_func()
   lock(l)
   try
      @threads for i = 1:10^6
          atomic_add!(val1,1.924)
      end
      put!(cn1,val1[])
      atomic_cas!(val2,val2[],0.5val1[])
      atomic_xchg!(val1,fetch(cn1)^0.5)
   finally
      unlock(l)
   end 
   return take!(cn1),val2[], val1[]
end

function mt_rep(path)
   set_value(Info1, "Bla, bla, bla")
end

function prog_end(path)
    destroy(w)
end

function main()
    global w=Toplevel("Systemtest",1048,570)
    pack_stop_propagate(w)
    mFr=Frame(w, padding=[5,5,5,5], relief="groove")
    pack(mFr,expand=true,fill="both")
   
    grid(Label(mFr,"Info"), 1,1, sticky="nw", pady=5)
    Info1=Text(mFr, width=128, height=30, padx=5, relief="flat")
    grid(Info1,2,1, sticky="w")
    Button1=Button(mFr,"Repeat")
    grid(Button1,3,1, sticky="ws", padx=15, pady=15)
    bind(Button1, "command", mt_rep)
    Button2=Button(mFr,"Quit") 
    grid(Button2,3,1, sticky="es", padx=15, pady=15)
    bind(Button2, "command", prog_end)  
    
    et=@elapsed mw,hw,qw=mt_func()
    itxt="""
    Elapsed time: $et seconds
    Threads: $(Sys.CPU_THREADS)
    val1[]: $mw
    val2[]: $hw
    sqrt val1[]: $qw
    
    \n$(Sys.cpu_info())"""
    set_value(Info1, "$itxt")
 end

 main();

```

The function that produces the error is **mt\_rep** , no matter what it says.  
Any hints?

Thank You

btw.  
Sorry for my english, I’m no native speaker.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [July 18, 2021, 2:05pm UTC](https://discourse.julialang.org/t/tkinter-error-on-button-click/64740/2 "2021-07-18T14:05:37Z")

</div>

In your function

```julia
function mt_rep(path)
   set_value(Info1, "Bla, bla, bla")
end

```

`Info1` is not defined because it is out of scope.

For a working example just put a `global` before the initialization of `Info1`:

```julia
global Info1=Text(mFr, width=128, height=30, padx=5, relief="flat")

```

like you already have done it for

```julia
global w=Toplevel("Systemtest",1048,570)

```

In general relying on `global` values is not the best design. I can’t find the Tk.jl documentation, in particular for the `bind` command, so I can’t tell now, how to do it better, e.g. by providing `Info1` as a parameter to `mt_rep`.

---

<div class="post-metadata">

**Author:** ![trilobit](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@trilobit](https://discourse.julialang.org/u/trilobit)\
**Post date:** [July 20, 2021, 5:29am UTC](https://discourse.julialang.org/t/tkinter-error-on-button-click/64740/3 "2021-07-20T05:29:04Z")

</div>

Oh, I’d overseen the scope of the variable…but now it runs 😀  
Many thx for your help.  
.  
btw.  
I’m only trying some things with Julia and Tk .This is currently no  
seriously code. So the global variables are no problem  
The Tk.jl docu is here: [Docu Tk.jl](https://github.com/JuliaGraphics/Tk.jl/blob/master/docs/src/index.md)
