英文:
Unable to create a menubar in a gotk3 application
问题
我正在使用gotk3(一个为Go语言提供GTK+3绑定的项目)编写一个简单的GUI应用程序。我想在应用程序中显示一个菜单栏,但是现在它没有显示任何菜单(在MacOSX和Linux上测试过)。这是代码:
package main
import (
"github.com/gotk3/gotk3/gtk"
"log"
)
func main() {
gtk.Init(nil)
// 创建窗口
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()
})
// 创建网格和标签
grid, err := gtk.GridNew()
if err != nil {
log.Fatal("Unable to create grid:", err)
}
label, _ := gtk.LabelNew("Hello, gotk3!")
grid.Add(label)
// 创建菜单
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)
// 将菜单栏附加到网格
grid.Attach(menuBar, 0, 0, 200, 200)
// 显示窗口
win.Add(grid)
win.ShowAll()
gtk.Main()
}
由于它不起作用,我认为Attach()
不是正确的函数来实现这个功能。我从未使用过GTK+,所以对该怎么做或者在哪里查找几乎没有任何想法。有什么提示吗?
谢谢,
Andrea
英文:
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
得分: 0
好的,以下是翻译好的内容:
好的,我找到了如何做到这一点。根据这个示例,我理解了在GTK上创建菜单所需的步骤。编辑的部分如下:
// 创建菜单栏
menuBar, err := gtk.MenuBarNew()
if err != nil {
log.Fatal("无法创建菜单栏:", err)
}
// 创建菜单
fileMenu, err := gtk.MenuNew()
if err != nil {
log.Fatal("无法创建菜单:", err)
}
// 创建菜单项
fileMenuItem, err := gtk.MenuItemNewWithLabel("文件")
if err != nil {
log.Fatal("无法创建菜单项:", err)
}
openMenuItem, err := gtk.MenuItemNewWithLabel("打开")
if err != nil {
log.Fatal("无法创建菜单项:", err)
}
fileMenuItem.SetSubmenu(fileMenu)
fileMenu.Append(openMenuItem)
menuBar.Append(fileMenuItem)
gtkGrid.Attach(menuBar, 0, 0, 200, 200)
win.Add(gtkGrid)
现在菜单显示在标签上方(在MacOSX上),但至少它被显示出来了。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论