英文:
Change the menu text after clicking it in go fyne systray menu
问题
这是我的系统托盘菜单,我需要在点击“start”按钮后将其更改为“stop”(并将其动作更改为“stop”动作)。
这个问题提到了这个问题,但我找不到“menu1”的refresh()
方法。
顺便说一下,我正在macOS Big Sur 11.5.1 (20G80)上进行测试,我的Fyne版本是v2.3.3。
这是我的代码:
package main
import (
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/driver/desktop"
"fyne.io/fyne/v2/widget"
)
var menu1 *fyne.MenuItem
func main() {
a := app.New()
w := a.NewWindow("SysTray")
if desk, ok := a.(desktop.App); ok {
menu1 = fyne.NewMenuItem("start", func() {
fmt.Println("started")
// 在启动后,我将菜单标签更改为“stop”,并将Action更改为stop action,
// 但我不知道如何将其更新到系统托盘菜单
menu1.Label = "stop"
menu1.Action = func() {
fmt.Println("stopped")
}
// MenuItem没有Refresh()方法,只有menu1.ChildMenu有
// menu1.ChildMenu.Refresh()
})
menu2 := fyne.NewMenuItem("menu1", nil)
menu2.ChildMenu = fyne.NewMenu("",
fyne.NewMenuItem("submenu1", func() {
fmt.Println("submenu1")
}),
fyne.NewMenuItem("submenu2", func() {
fmt.Println("submenu2")
}),
)
menu3 := fyne.NewMenuItem("show2", func() {
fmt.Println("menu2")
})
m := fyne.NewMenu("MyApp", menu1, menu2, menu3)
desk.SetSystemTrayMenu(m)
}
w.SetContent(widget.NewLabel("Fyne System Tray"))
w.SetCloseIntercept(func() {
w.Hide()
})
w.ShowAndRun()
}
这是我的go.mod
:
module go-GUI
go 1.19
require fyne.io/fyne/v2 v2.3.3
require (
fyne.io/systray v1.10.1-0.20230312215936-7f71b037e260 // indirect
github.com/benoitkugler/textlayout v0.3.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fredbi/uri v0.1.0 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/fyne-io/gl-js v0.0.0-20220119005834-d2da28d9ccfe // indirect
github.com/fyne-io/glfw-js v0.0.0-20220120001248-ee7290d23504 // indirect
github.com/fyne-io/image v0.0.0-20220602074514-4956b0afb3d2 // indirect
github.com/go-gl/gl v0.0.0-20211210172815-726fda9656d6 // indirect
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20221017161538-93cebf72946b // indirect
github.com/go-text/typesetting v0.0.0-20221212183139-1eb938670a1f // indirect
github.com/godbus/dbus/v5 v5.1.0 // indirect
github.com/goki/freetype v0.0.0-20220119013949-7a161fd3728c // indirect
github.com/gopherjs/gopherjs v1.17.2 // indirect
github.com/jsummers/gobmp v0.0.0-20151104160322-e2ba15ffa76e // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/srwiley/oksvg v0.0.0-20220731023508-a61f04f16b76 // indirect
github.com/srwiley/rasterx v0.0.0-20210519020934-456a8d69b780 // indirect
github.com/stretchr/testify v1.8.0 // indirect
github.com/tevino/abool v1.2.0 // indirect
github.com/yuin/goldmark v1.4.13 // indirect
golang.org/x/image v0.0.0-20220601225756-64ec528b34cd // indirect
golang.org/x/mobile v0.0.0-20211207041440-4e6c2922fdee // indirect
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
golang.org/x/text v0.6.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
honnef.co/go/js/dom v0.0.0-20210725211120-f030747120f2 // indirect
)
英文:
Here is my systray menu, I need to change "start" button to "stop" after clicking it(and change it's action to "stop" action)
This issue had mentioned about this, but I can not find the refresh()
method of "menu1".
BTW, I'm testing it in macOS Big Sur 11.5.1 (20G80), and my Fyne version is v2.3.3
Here's my code
package main
import (
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/driver/desktop"
"fyne.io/fyne/v2/widget"
)
var menu1 *fyne.MenuItem
func main() {
a := app.New()
w := a.NewWindow("SysTray")
if desk, ok := a.(desktop.App); ok {
menu1 = fyne.NewMenuItem("start", func() {
fmt.Println("started")
// after started, I change the menu label to "stop" and change the Action to stop action,
// but I don't know how to update this to the system tray menu
menu1.Label = "stop"
menu1.Action = func() {
fmt.Println("stopped")
}
// There is no Refresh() method for MenuItem, only for menu1.ChildMenu
// menu1.ChildMenu.Refresh()
})
menu2 := fyne.NewMenuItem("menu1", nil)
menu2.ChildMenu = fyne.NewMenu("",
fyne.NewMenuItem("submenu1", func() {
fmt.Println("submenu1")
}),
fyne.NewMenuItem("submenu2", func() {
fmt.Println("submenu2")
}),
)
menu3 := fyne.NewMenuItem("show2", func() {
fmt.Println("menu2")
})
m := fyne.NewMenu("MyApp", menu1, menu2, menu3)
desk.SetSystemTrayMenu(m)
}
w.SetContent(widget.NewLabel("Fyne System Tray"))
w.SetCloseIntercept(func() {
w.Hide()
})
w.ShowAndRun()
}
Here's my go.mod
module go-GUI
go 1.19
require fyne.io/fyne/v2 v2.3.3
require (
fyne.io/systray v1.10.1-0.20230312215936-7f71b037e260 // indirect
github.com/benoitkugler/textlayout v0.3.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fredbi/uri v0.1.0 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/fyne-io/gl-js v0.0.0-20220119005834-d2da28d9ccfe // indirect
github.com/fyne-io/glfw-js v0.0.0-20220120001248-ee7290d23504 // indirect
github.com/fyne-io/image v0.0.0-20220602074514-4956b0afb3d2 // indirect
github.com/go-gl/gl v0.0.0-20211210172815-726fda9656d6 // indirect
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20221017161538-93cebf72946b // indirect
github.com/go-text/typesetting v0.0.0-20221212183139-1eb938670a1f // indirect
github.com/godbus/dbus/v5 v5.1.0 // indirect
github.com/goki/freetype v0.0.0-20220119013949-7a161fd3728c // indirect
github.com/gopherjs/gopherjs v1.17.2 // indirect
github.com/jsummers/gobmp v0.0.0-20151104160322-e2ba15ffa76e // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/srwiley/oksvg v0.0.0-20220731023508-a61f04f16b76 // indirect
github.com/srwiley/rasterx v0.0.0-20210519020934-456a8d69b780 // indirect
github.com/stretchr/testify v1.8.0 // indirect
github.com/tevino/abool v1.2.0 // indirect
github.com/yuin/goldmark v1.4.13 // indirect
golang.org/x/image v0.0.0-20220601225756-64ec528b34cd // indirect
golang.org/x/mobile v0.0.0-20211207041440-4e6c2922fdee // indirect
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
golang.org/x/text v0.6.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
honnef.co/go/js/dom v0.0.0-20210725211120-f030747120f2 // indirect
)
答案1
得分: 0
如今在Slack上发布的消息和GitHub问题中提到,你需要调用Menu.Refresh
来刷新显示的菜单。
在你上面的代码中,应该是m.Refresh()
。
英文:
As was posted on Slack earlier today, and on the GitHub issue, you need to call Menu.Refresh
to refresh the menu that is shown.
In your code above that would be m.Refresh()
.
答案2
得分: 0
这是一个正常工作的代码:
package main
import (
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/driver/desktop"
"fyne.io/fyne/v2/widget"
)
var menu1 *fyne.MenuItem
var menu *fyne.Menu
func main() {
a := app.New()
w := a.NewWindow("SysTray")
if desk, ok := a.(desktop.App); ok {
menu1 = fyne.NewMenuItem("start", func() {
if menu1.Label == "start" {
fmt.Println("started")
menu1.Label = "stop"
} else {
fmt.Println("stopped")
menu1.Label = "start"
}
// 更改菜单标签后,使用此方法刷新菜单
// menu 是返回的
menu.Refresh()
})
menu2 := fyne.NewMenuItem("menu1", nil)
menu2.ChildMenu = fyne.NewMenu("",
fyne.NewMenuItem("submenu1", func() {
fmt.Println("submenu1")
}),
fyne.NewMenuItem("submenu2", func() {
fmt.Println("submenu2")
}),
)
menu3 := fyne.NewMenuItem("show2", func() {
fmt.Println("menu2")
})
menu = fyne.NewMenu("MyApp", menu1, menu2, menu3)
desk.SetSystemTrayMenu(menu)
}
w.SetContent(widget.NewLabel("Fyne System Tray"))
w.SetCloseIntercept(func() {
w.Hide()
})
w.ShowAndRun()
}
希望对你有帮助!
英文:
This is the code that works fine
package main
import (
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/driver/desktop"
"fyne.io/fyne/v2/widget"
)
var menu1 *fyne.MenuItem
var menu *fyne.Menu
func main() {
a := app.New()
w := a.NewWindow("SysTray")
if desk, ok := a.(desktop.App); ok {
menu1 = fyne.NewMenuItem("start", func() {
if menu1.Label == "start" {
fmt.Println("started")
menu1.Label = "stop"
} else {
fmt.Println("stopped")
menu1.Label = "start"
}
// after changing the menu label, use this to refresh the menu
// menu is return
menu.Refresh()
})
menu2 := fyne.NewMenuItem("menu1", nil)
menu2.ChildMenu = fyne.NewMenu("",
fyne.NewMenuItem("submenu1", func() {
fmt.Println("submenu1")
}),
fyne.NewMenuItem("submenu2", func() {
fmt.Println("submenu2")
}),
)
menu3 := fyne.NewMenuItem("show2", func() {
fmt.Println("menu2")
})
menu = fyne.NewMenu("MyApp", menu1, menu2, menu3)
desk.SetSystemTrayMenu(menu)
}
w.SetContent(widget.NewLabel("Fyne System Tray"))
w.SetCloseIntercept(func() {
w.Hide()
})
w.ShowAndRun()
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论