GUI elements from a function not being displayed in Red language

138 Views Asked by At

I am using following code to try to get GUI elements from a function:

  mypanelfn: func[] [
    collect[
      repeat i 10 [ 
        print append copy "i in loop: " i
        keep [t: text]  keep append copy "message: " i
        keep [field "entry"    
              button "Click" [t/text: "clicked"] return]]]]

  view [ 
    do [mypanelfn]]

There are no error messages and loop go on all right and a windows is also displayed. But this is only a small empty windows without any text, fields or buttons.

What is wrong with this code?

Edit: putting probe before collect shows (I have added line breaks for clarity):

[t: text "message: 1" field "entry" button "Click" [t/text: "clicked"] return 
t: text "message: 2" field "entry" button "Click" [t/text: "clicked"] return 
t: text "message: 3" field "entry" button "Click" [t/text: "clicked"] return 
t: text "message: 4" field "entry" button "Click" [t/text: "clicked"] return 
t: text "message: 5" field "entry" button "Click" [t/text: "clicked"] return
4

There are 4 best solutions below

2
On BEST ANSWER

once again; you need unique names for elements you want to address. Here a solution with reduce instead of compose

mypanelfn: func[] [
  collect[
    repeat i 10 [ 
      tname: to-word rejoin ['t i]
      print append copy "i in loop: " i
      keep  reduce [to-set-word tname 'text]  keep append copy "message: " i
      keep reduce [
       'field "entry" 'button "Click"  reduce  [ 
          to-set-path  reduce [
             tname 'text ]
            "clicked" ]
      'return ] ] ] ] 

I recommend that you use the commands in the console to see what they do. E.g.

rejoin ['t i] creates a string "t1"with t and the (reduced/get-)value of i.

to-word changes that to a Red(bol) word t1

to-setword tname creates a set-word t1:

to-set-path reduce [tname 'text ]creates a set-path t1/text:

2
On

this works for me in rebol/view:

lay: mypanelfn
insert head lay 'across
view layout lay

enter image description here

I think while you're learning this stuff you need to look at the generated VID code to check that there are no problems before trying to View it.

0
On

You don't necessarily need the function there, however:

view mypanelfn

Works.

Note: the equivalent code in Rebol requires layout: view layout mypanelfn

The reason this happens is because view processes blocks! (anything inside []). So you don't have to do it.

In general, it's better to think of Red as a functional, message passing language. Everything is an expression, in contrast to imperative languages with procedures and statements.

1
On

This method does not require setting any variables—it works by containing each iteration of faces within a common parent (panel):

view collect [
    keep [below space 0x0]
    repeat i 10 [
        keep compose/deep [
            panel [
                origin 0x0
                text (rejoin ["Message Number: " i])
                field "entry"
                button "Click" [face/parent/pane/1/text: "clicked"]
            ]
        ]
    ]
]

face/parent is the panel face whose first child (pane/1) is the text box (origin does not create a face).