编译错误原因:

walk.App()没有ActiveMainWindow方法,会导致编译错误。

解决方法:

将OnSizeChanged中的walk.App().ActiveMainWindow()改为walk.MainWindow()即可。

修改后的代码如下:

func main() { var ( entry *walk.LineEdit )

// 创建GUI界面
MainWindow{
    Title:   "证书下载和配置",
    MinSize: Size{Width: 300, Height: 100},
    Layout:  VBox{},
    Children: []Widget{
        LineEdit{
            AssignTo: &entry,
        },
        PushButton{
            Text: "下载证书并写入配置",
            OnClicked: func() {
                // 获取编辑框内容
                entryText := entry.Text()

                // 如果编辑框内容为空,则提示错误信息
                if entryText == "" {
                    dlgs.Error("错误", "请输入编号")
                    return
                }

                // 下载证书
                filepath := filepath.Join(os.Getenv("USERPROFILE"), "Desktop", "mitmproxy-ca-cert.p12")
                err := downloadFile("http://mitm.it/cert/p12", filepath)
                if err != nil {
                    dlgs.Error("错误", fmt.Sprintf("下载证书失败: %v", err))
                    return
                }

                // 写入配置文件
                err = writeIni(entryText)
                if err != nil {
                    dlgs.Error("错误", fmt.Sprintf("写入配置文件失败: %v", err))
                    return
                }

                // 提示设置完成信息
                dlgs.Info("信息", "设置完成,证书已下载到桌面请手动安装")
                // 关闭窗口
                walk.App().Exit(0)
            },
        },
    },
    OnSizeChanged: func() {
        // 将窗口居中
        mw := walk.MainWindowFromObject(walk.App().ActiveForm())
        if mw == nil {
            return
        }
        screen := mw.Screen()
        cx := screen.WorkArea().Width / 2
        cy := screen.WorkArea().Height / 2
        mw.SetBounds(walk.Rectangle{
            X:      cx - mw.Width()/2,
            Y:      cy - mw.Height()/2,
            Width:  mw.Width(),
            Height: mw.Height(),
        })
    },
}.Run()
编译错误guigo11430 walkAppActiveMainWindow undefined type walkApplication has no field or method ActiveMainWindowpackage mainimport fmt githubcomgen2braindlgs githubcomlxnwalk githubcomlxnwa

原文地址: https://www.cveoy.top/t/topic/eq5H 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录