0
我想點擊一個「面板」後,如何在 之後更新「狀態欄」的建議。wxhaskell:使用面板上的「點擊」更新狀態欄
下面的程序演示了這個問題。該程序繪製兩個 幀。你可以想象左邊框架是某種繪畫區域 ,右邊框架包含按鈕「紅色」和「綠色」。 點擊標有「紅色」的按鈕後,statusField的文本爲 已更新爲「當前顏色:紅色」。標有「綠色」的按鈕將文本更新爲「當前顏色:綠色」。
如何在用戶點擊 左側面板後更改statusField的文本?例如。將其更改爲「您成功點擊了 繪圖面板。」
爲什麼我不能在「on click」中按照與「on command」相同的方式爲 按鈕? (請參閱下面的源代碼註釋。)
非常感謝。
module Main where
import Graphics.UI.WX
-- | NOP (= No Operation)
data Command = Nop
| Red
| Green
deriving (Eq)
main :: IO()
main
= start hello
hello :: IO()
hello
= do currentCommand <- varCreate $ Nop -- current command performed on next click on "pDrawingarea"
status <- statusField [text := "Welcome."]
-- Frames and Panels
f <- frame [ text := "Demo"
, bgcolor := lightgrey ]
pButtons <- panel f [ bgcolor := lightgrey]
pDrawingarea <- panel f [ on paint := draw
, bgcolor := lightgrey
]
set pDrawingarea [on click := do drawingAreaOnClick status currentCommand pDrawingarea
-- set status [text := "User clicked on the panel."]
-- Problem: uncommenting the line above shows the problem
]
bRed <- button pButtons [text := "Red", on command := do varSet currentCommand Red
set status [text := "Current color: Red"]
]
bGreen <- button pButtons [text := "Green", on command := do varSet currentCommand Green
set status [text := "Current color: Green"]
]
set pButtons [ layout := column 1 [ hstretch.expand $ widget bRed
, hstretch.expand $ widget bGreen
]
]
set f [ statusBar := [status]
, layout := row 3 [
minsize (sz 600 500) $ stretch.expand $ widget pDrawingarea
, vstretch.expand $ rule 3 500
, minsize (sz 200 500) $ vstretch.expand $ widget pButtons
]
]
return()
draw :: DC a -> Rect -> IO()
draw dc viewArea
= do putStrLn "Imagine some code to repaint the screen."
drawingAreaOnClick :: statusField -> Var Command -> Panel() -> Point -> IO()
drawingAreaOnClick sf command panel pt
= do c <- varGet command
case c of
Red -> do putStrLn "Imagine some code to do red painting"
Green -> do putStrLn "Imagine some code to do green painting"