我还有另一个clojure问题。
所以我现在正在做一个项目,并试图为它编写一个GUI组件。我已经有了所有的功能部分,所以现在我只想让它看起来更漂亮,并学习更多关于摇摇板是如何工作的。
基本上,我有一堆输入字段(即文本字段、滑块、组合框),用户可以使用它们来输入特定类型的数据。当用户单击“确认”按钮时,我希望该按钮的操作返回上述输入字段的所有值。我对线程没有太多的经验,但我知道(显然)可能会有一些并发问题。我如何才能做到这一点呢?
作为参考,下面是我的代码的一个小示例:
;
;in restaurant-gui.core
;
(defn window-setup []
(let [my-font (font :name "Arial"
:size 22)
cuisine-label (label :text "Cuisine: "
:font my-font
:border [20 10])
cuisine (combobox :model["Any cuisine"
"American"
"Barbecue"
"Chinese"
"French"
"Italian"])
confirm-button (button :text "Confirm"
:listen [:action (fn [event] event
(selection cuisine))])
window-contents (vertical-panel :items [cuisine-label cuisine
confirm-button])]
window-contents))
;
;in restaurant-inference-engine.core
;
(defn -main
[&args]
(binding [window-contents (window-setup)]
(draw-window window-contents) ;somehow listen here for
;information from the button
;and bind it to button-info?...
(search-for-restaurant button-info)))此外,如果有人知道任何简单到中间的clojure代码,我将非常感激。我想更多地接触编写良好的clojure代码,以提高我对该语言的理解。
发布于 2016-02-25 08:28:01
当使用像GUI这样的固有的statefull特性时,使用Clojure的可变状态特性通常很有帮助。我看到了两种基本的方法:
ref中,然后将相关的refs传递到需要处理UI状态的函数中。atom中,并将该原子传递给使用任何UI组件的每个函数,以便它们可以随时使用UI的当前状态。注意以一致的方式更新它。其中任何一个似乎都不太可能适用于所有甚至大多数项目,所以这取决于您的特定项目会是什么样子。如果您坚持使用官方的Clojure方法来处理不断变化的状态,那么您可能不需要担心“并发”问题。vars、refs、agent、chans)
https://stackoverflow.com/questions/35613312
复制相似问题