Golang中如何销毁zserge webview窗口

Golang中如何销毁zserge webview窗口 亲爱的Gopher们,

在我正在开发的一个实用工具中,我使用了zserge出色的webview代码 (https://github.com/zserge/webview/blob/master/examples/page-load-go/main.go) 来创建窗口通知。在我的工具中,我使用了runDataURL()来打开窗口。

这一切都运行得很好,但现在我希望让Go在x秒后自动关闭窗口, 而不是期望/等待最终用户交互式地关闭它。 这该如何实现? 我尝试使用goroutine和channel进行各种尝试,但到目前为止都没有成功。

对于这方面的任何指导,我将非常感激。


更多关于Golang中如何销毁zserge webview窗口的实战教程也可以访问 https://www.itying.com/category-94-b0.html

4 回复

再次问候,Curtis——非常感谢你为此付出的努力。虽然我曾希望能不调用任何外部组件就完成这个任务,但我还是会尝试一下,看看它是否满足实用需求。

再次感谢

更多关于Golang中如何销毁zserge webview窗口的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


根据你分享的示例和该包的文档来看,关闭窗口似乎只需要调用 Exit() 函数即可。因此你可以尝试用等待或其他方式阻塞程序运行指定时长,然后调用 Exit(),而不是使用 defer 语句。

我想到可以将其编写为命令,并使用 exec.Command 在需要时运行自定义窗口。如果你想查看的话,这里是代码库:

GitHub

CurtGreen/windows

头像

CurtGreen/windows

使用 zserge webview 实现自终止弹出窗口的示例命令

感谢您的建议,但我无法让它正常工作。窗口会一直保持打开状态直到主函数退出。以下是我的做法:

package main

import (
	"fmt"
	"time"
	"net/url"
	"github.com/zserge/webview"
)

func main() {
	go runDataURL(400, 400)
	fmt.Println("Immediately back in main, hopefully window will be closed in 2 seconds")
	fmt.Println("Meanwhile, sleeping for 10 secs ....")
	time.Sleep(10 * time.Second)
} 

func runDataURL(windowWidth int, windowHeight int) {
	msg := "Teardown in 2 sec"

	w := webview.New(webview.Settings{
		Width:     windowWidth,
		Height:    windowHeight,
		Title: "Some window title",
		URL:   "data:text/html," + url.PathEscape(msg),
	})
	fmt.Println("In func runDataURL, sleeping for 2 seconds ...")
	w.Run()
	time.Sleep(2 * time.Second)
	w.Exit()
	fmt.Println("Did I manage to close?")	
}
回到顶部