首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Gtk2hs中向菜单添加MenuItem

在Gtk2hs中向菜单添加MenuItem
EN

Stack Overflow用户
提问于 2016-03-30 22:11:57
回答 1查看 63关注 0票数 2

我想创建一个弹出式GTK Menu,然后添加一些MenuItems到它。我找到了这个bit of sample code for popup menus,但它已经腐烂了。修复后的版本如下。查找评论“这就是不起作用的部分!”。

问题是,当菜单显示时,我添加的MenuItem不会出现。使用UI管理器创建的操作项在那里,但不是我手动创建的"Test“项。

我是不是漏掉了什么步骤?还是说我走错了路?

代码语言:javascript
复制
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)
EN

Stack Overflow用户

发布于 2016-03-30 23:26:09

我已经弄明白了。我需要在新的菜单项上调用"widgetShow“。

代码语言:javascript
复制
     testItem <- menuItemNewWithLabel "Test"
     widgetShow testItem
     testItem `on` menuItemActivated $ putStrLn "Test clicked"
     menuShellAppend (castToMenu pop) testItem
票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36311436

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档