我想了解如何在之后更新“statusField” 点击“面板”。
以下程序演示了该问题。该计划绘制了两个 帧。你可以想象左边的框架是某种绘图区域 右边的框架包含“红色”和“绿色”按钮。 单击标有“红色”的按钮后,statusField的文本为 更新为“当前颜色:红色”。标有“绿色”的按钮将文本更新为“当前颜色:绿色”。
如何在用户点击后更改statusField的文本 左侧面板?例如。将其更改为“您已成功点击了 绘图板。“
为什么我不能在“点击”中以与“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"
答案 0 :(得分:0)
在这个问题上花了很多时间后我找到了解决方案。
解决方案是改变
的定义drawingAreaOnClick :: statusField -> Var Command -> Panel () -> Point -> IO ()
到
drawingAreaOnClick :: Textual x => x -> Var Command -> Panel () -> Point -> IO ()
因为“statusField”本身是“Textual”类的成员,所以我不明白这个问题。
为了完整起见,我将提到我也切换了GHC版本。最初的问题出现在GHC 7.8.4中,我找到的解决方案适用于GHC 7.10.3。我不知道GHC版本是否会影响这个问题。
供参考完整的工作代码:
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."]
-- not needed: currentStatus <- varCreate status
-- 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"]
--sf <- varGet currentStatus
-- set sf [text := "yyy"]
]
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 :: Textual x => x -> Var Command -> Panel () -> Point -> IO ()
drawingAreaOnClick sf command panel pt
= do c <- varGet command
set sf [text := "Drawing on the screen."]
case c of
Red -> do putStrLn "Imagine some code to do red painting"
Green -> do putStrLn "Imagine some code to do green painting"