Fyne Gui Goroutine在几个滴答后停止了

发布于 2025-01-23 12:14:11 字数 947 浏览 2 评论 0原文

我已经开始编写一个简单的GUI应用程序,该应用程序仅在左上角显示当前时间。我的问题是,在我的M1 Mac上2-3秒后,时钟刻度停止。有趣的是,当我在时间更新旁边添加一个打印语句时,它会完美运行。将其删除,然后在几个滴答后再次停止了goroutine。这是它的代码:

package main

import (
    // "fmt"
    "time"

    "fyne.io/fyne/v2"
    "fyne.io/fyne/v2/app"
    "fyne.io/fyne/v2/layout"
    "fyne.io/fyne/v2/widget"
)

func updateTime(clock *widget.Label) {
    formatted := time.Now().Format("15:04:05")
    clock.SetText(formatted)
}

func main() {
    a := app.New()
    w := a.NewWindow("Clock")
    w.Resize(fyne.NewSize(500, 400))

    row := fyne.NewContainerWithLayout(
        layout.NewVBoxLayout(),
    )

    clock := widget.NewLabel("Starting Clock")
    row.Add(clock)
    w.SetContent(row)

    go func() {
        for range time.Tick(time.Second) {
            // fmt.Println("Hello")
            updateTime(clock)
        }
    }()

    w.ShowAndRun()
}

我在这里缺少一些东西,还是Fyne只是一个问题?

I have started to write a simple Go GUI application which - for now - only displays the current time at the top left corner. My issue is that the Goroutine which makes the clock tick stops after 2-3 seconds on my M1 Mac. The interesting thing is, that when I add a print statement next to my time update one, it runs flawlessly. Remove it, and the Goroutine just halts again after a couple of ticks. Here's the code for it:

package main

import (
    // "fmt"
    "time"

    "fyne.io/fyne/v2"
    "fyne.io/fyne/v2/app"
    "fyne.io/fyne/v2/layout"
    "fyne.io/fyne/v2/widget"
)

func updateTime(clock *widget.Label) {
    formatted := time.Now().Format("15:04:05")
    clock.SetText(formatted)
}

func main() {
    a := app.New()
    w := a.NewWindow("Clock")
    w.Resize(fyne.NewSize(500, 400))

    row := fyne.NewContainerWithLayout(
        layout.NewVBoxLayout(),
    )

    clock := widget.NewLabel("Starting Clock")
    row.Add(clock)
    w.SetContent(row)

    go func() {
        for range time.Tick(time.Second) {
            // fmt.Println("Hello")
            updateTime(clock)
        }
    }()

    w.ShowAndRun()
}

Is there something I'm missing here, or could this just be an issue with Fyne?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文