gotk3 / gotk3-examples

164 stars 34 forks source link

Custom signals #15

Closed d-fal closed 5 years ago

d-fal commented 5 years ago

Dears, I need to know how to write a custom signal in GoGtk3. Having a gui thread wherein gtk is working in, I want to update a textview from adjacent goroutine.

label,_:= gtk.LabelNew()

go func(){
   // This is listening to a websocket
   //update label
   for {
       c.ReadJSON(&frame) // c is my gorilla websocket 
       fmt.Printf("I received something %v\n", frame)
       // label.SetText("update ...") is not working properly

       }
}()

When using label.SetText method, I noticed that this is not working correctly. I suspect this is not thread safe and I should emit a signal instead. I appreciate if you lead me how.

I have also asked it on stackoverflow

Bios-Marcel commented 5 years ago

If you just want to know how to run this on the correct thread, then the answer should be glib.IdleAdd(...).

d-fal commented 5 years ago

Dear Marcel, Thanks for your reply. I need more clarifications on this. Do you mean something like this:

label,_:= gtk.LabelNew()
type Frame struct {
     Id int
     Name string
}
// ....
var frame Frame
glib.IdleAdd(func(){
   // This is listening to a websocket
   for {
       c.ReadJSON(&frame) 
       label.SetText("update ...") 

       }
})
Bios-Marcel commented 5 years ago

No, you have to wrap the SetText call only.

d-fal commented 5 years ago

Thanks Marcel,

It saved my day!