【问题标题】:How to access values from panels in Red language如何访问红色语言面板中的值
【发布时间】:2017-09-18 08:33:16
【问题描述】:

我正在使用以下代码查找 2 系列数字的产品,然后找到这些产品的总和:

  make-row: func [][
    compose [
        t1: text "N1:"
        f1: field 
        t2: text "N2: "
        f2: field
        t3: text "Product: "
        t4: text ""
        b: button "Get product" [
            x: face/extra/2/text          
            y: face/extra/4/text
            z: (to-integer x) * (to-integer y)
            face/extra/6/text: rejoin [z]]
        do [b/extra: reduce [t1 f1 t2 f2 t3 t4]] ]  ]

  view compose [
        (make-row) return 
        (make-row) return
    b: button "Calculate" [t2/text: "..to be given"]
    t1: text "Sum of products:"
    t2: text ""                       ; NEED TO GET SUM OF ALL PRODUCTS IN ABOVE ROWS. 
  ]  

第一部分工作正常 - 产品计算正确。但是我怎样才能访问这些单独的产品来找到产品的总和呢?我找不到任何方法,因为这些行并不是我可以访问的公共变量或方法/函数的真正对象。如何解决?感谢您的帮助。

【问题讨论】:

  • b/extra: reduce [t1 f1 t2 f2 t3 t4],为什么你以后只使用了3个人脸参考,你却保存了6个?

标签: object rebol red


【解决方案1】:

正如我刚刚了解的面孔和窗格,这里有一个没有错误处理的解决方案

make-row: func [][
     compose [
        text "N1:"
        field 
        text "N2: "
        field
        text "Product: "
        text ""
        button "Get product" [
            b1: index? find face/parent/pane  face
            face/parent/pane/(b1 - 1)/text:  form multiply   to-integer   face/parent/pane/(b1 - 5)/text  to-integer face/parent/pane/(b1 - 3)/text  
        ]

    ]  
]
view compose [
    (make-row) return 
    (make-row) return
    button "Calculate" [
        t2/text:   form add to-integer face/parent/pane/6/text  to-integer face/parent/pane/13/text
    ]
    text "Sum of products:"
    t2: text ""  
]   

所有面对象都在父面的窗格块中排序。因此,查看单击的人脸对象的索引,您可以获得计算其他人脸对象位置的参考。

【讨论】:

  • 我们将不胜感激。
  • 顺便说一句 @rnso,我注意到您以 lispy 风格重新格式化了缩进和括号。在此处查看红色标准:github.com/red/red/wiki/Coding-Style-Guide#block-layouts
  • 我不知道 Red 语言有官方风格。 Lispy 风格主要是节省空间(几乎是空行)。
  • @rnso 从表面上看,计算是基于面与单击按钮的接近程度来完成的(参见find face/parent/pane face 行)。请记住,一旦创建,面孔只是以分层方式构造的对象,其中应用了 Red 的通常规则。你可以在这里看到这个:view [text "Some" text "Words" text "In" text "A" text "Window" button "Remove" [remove face/parent/pane]]
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-06
  • 2018-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多