dqe9657 2017-09-04 07:42
浏览 46
已采纳

无法在GoTK3应用程序中创建菜单栏

I'm using gotk3 (a project that provides Go bindings for GTK+3) for writing a simple GUI application. I'd like to show a menu bar in the application but now it doesn't show any menu (tested on both MacOSX and Linux). This is the code:

package main

import (
    "github.com/gotk3/gotk3/gtk"
    "log"
)

func main() {

    gtk.Init(nil)

    // creates window
    win, err := gtk.WindowNew(gtk.WINDOW_TOPLEVEL)
    if err != nil {
        log.Fatal("Unable to create window:", err)
    }
    win.SetDefaultSize(800, 600)
    win.Connect("destroy", func() {
        gtk.MainQuit()
    })

    // creates grid and label
    grid, err := gtk.GridNew()
    if err != nil {
        log.Fatal("Unable to create grid:", err)
    }
    label, _ := gtk.LabelNew("Hello, gotk3!")
    grid.Add(label)


    // creates menu
    menuBar, err := gtk.MenuBarNew()
    if err != nil {
        log.Fatal("Unable to create menubar:", err)
    }

    menu, err := gtk.MenuNew()
    if err != nil {
        log.Fatal("Unable to create menu:", err)
    }
    menu.SetName("File")

    menuItem, err := gtk.MenuItemNewWithLabel("Open")
    if err != nil {
        log.Fatal("Unable to create menuitem:", err)
    }
    menu.Append(menuItem)

    // attaches menubar to grid
    grid.Attach(menuBar, 0, 0, 200, 200)

    // shows window
    win.Add(grid)
    win.ShowAll()
    gtk.Main()
}

Since it doesn't work I don't think that Attach() is the right function for doing this. I never worked with GTK+, so almost no idea of what to do or where to look. Any hint?

Thanks, Andrea

  • 写回答

1条回答 默认 最新

  • dpbsy60000 2017-09-05 07:44
    关注

    OK, I found out how to do it. Following this example I understood the steps needed to create a menu on GTK. The edited part is:

        // creates menu
        menuBar, err := gtk.MenuBarNew()
        if err != nil {
            log.Fatal("Unable to create menubar:", err)
        }
    
        fileMenu, err := gtk.MenuNew()
        if err != nil {
            log.Fatal("Unable to create menu:", err)
        }
    
        fileMenuItem, err := gtk.MenuItemNewWithLabel("File")
        if err != nil {
            log.Fatal("Unable to create menuitem:", err)
        }
    
        openMenuItem, err := gtk.MenuItemNewWithLabel("Open")
        if err != nil {
            log.Fatal("Unable to create menuitem:", err)
        }
    
        fileMenuItem.SetSubmenu(fileMenu)
        fileMenu.Append(openMenuItem)
        menuBar.Append(fileMenuItem)
    
        gtkGrid.Attach(menuBar, 0, 0, 200, 200)
        win.Add(gtkGrid)
    

    Now the menu is displayed over the label (on MacOsX), but at least it's displayed.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?