我想创建一个弹出式GTK Menu,然后添加一些MenuItems到它。我找到了这个bit of sample code for popup menus,但它已经腐烂了。修复后的版本如下。查找评论“这就是不起作用的部分!”。
问题是,当菜单显示时,我添加的MenuItem不会出现。使用UI管理器创建的操作项在那里,但不是我手动创建的"Test“项。
我是不是漏掉了什么步骤?还是说我走错了路?
import Control.Monad.IO.Class
import Graphics.UI.Gtk
main :: IO ()
main= do
initGUI
window <- windowNew
set window [windowTitle := "Click Right Popup",
windowDefaultWidth := 250,
windowDefaultHeight := 150 ]
eda <- actionNew "EDA" "Edit" Nothing Nothing
pra <- actionNew "PRA" "Process" Nothing Nothing
rma <- actionNew "RMA" "Remove" Nothing Nothing
saa <- actionNew "SAA" "Save" Nothing Nothing
agr <- actionGroupNew "AGR1"
mapM_ (actionGroupAddAction agr) [eda,pra,rma,saa]
uiman <- uiManagerNew
uiManagerAddUiFromString uiman uiDecl
uiManagerInsertActionGroup uiman agr 0
maybePopup <- uiManagerGetWidget uiman "/ui/popup"
let pop = case maybePopup of
(Just x) -> x
Nothing -> error "Cannot get popup from string"
window `on` buttonPressEvent $ do
b <- eventButton
if b == RightButton
then do
liftIO $ menuPopup (castToMenu pop) Nothing
return True
else return True
-- This is the bit that doesn't work!
testItem <- menuItemNewWithLabel "Test"
testItem `on` menuItemActivated $ putStrLn "Test clicked"
menuShellAppend (castToMenu pop) testItem
mapM_ prAct [eda,pra,rma,saa]
widgetShowAll window
window `on` objectDestroy $ mainQuit
mainGUI
uiDecl = "<ui> \
\ <popup>\
\ <menuitem action=\"EDA\" />\
\ <menuitem action=\"PRA\" />\
\ <menuitem action=\"RMA\" />\
\ <separator />\
\ <menuitem action=\"SAA\" />\
\ </popup>\
\ </ui>"
prAct :: ActionClass self => self -> IO (ConnectId self)
prAct a = a `on` actionActivated $ do
name <- actionGetName a
putStrLn ("Action Name: " ++ name)发布于 2016-03-30 23:26:09
我已经弄明白了。我需要在新的菜单项上调用"widgetShow“。
testItem <- menuItemNewWithLabel "Test"
widgetShow testItem
testItem `on` menuItemActivated $ putStrLn "Test clicked"
menuShellAppend (castToMenu pop) testItemhttps://stackoverflow.com/questions/36311436
复制相似问题