在 go 中使用 electron api 创建 gui-ag捕鱼王app官网

在 go 中使用 electron api 创建 gui

作者:迹忆客 最近更新:2023/04/27 浏览次数:

本篇文章介绍如何在 go 语言中使用 electron api 创建 gui。


在 go 中使用 electron api 创建 gui

electron api 或 astilectron 用于为 golang 创建 gui; astilectron 是由 electron 驱动的封装。 这个 api 在 github 上提供,它是 electron api 的一个版本,可以使用 html、css、javascript 和 golang 生成应用程序。

在使用 electron 包之前,我们需要从 github 上获取它。 让我们尝试获取并运行 electron 包的官方演示。

获取 electron 包并运行演示

按照以下步骤安装并运行 astilectron 演示:

  • 第一步是导入 astilectron。 在你的项目 src 目录下的 cmd 中运行以下命令:
    $ go get -u github.com/asticode/go-astilectron
    
  • 下载 go-astilectron 后,下一步就是获取演示。 运行以下命令获取演示:
    $ go get -u github.com/asticode/go-astilectron-demo/...
    
    get electron demo
  • 下载 electron 后,下一步是从 yourproject/src/github.com/asticode/go-astilectron-demo/ 目录中删除 bind.go 文件。 删除该文件,以便我们稍后绑定它。
  • 下一步是安装 astilectron 捆绑器。 运行以下命令下载并安装捆绑器:
    $ go get -u github.com/asticode/go-astilectron-bundler/...
    $ go install github.com/asticode/go-astilectron-bundler/astilectron-bundler
    
    go install bundler
  • 现在,转到 c:\users\sheeraz\go\src\github.com\asticcode\go-astilectron-demo 目录并运行以下命令: ```bash #go to the directory cd c:\users\sheeraz\go\src\github.com\asticode\go-astilectron-demo

astilectron-bundler

![astilectron bundler](/uploads/allimg/230427/1-23042ft1422l.png)
* 一旦 bundler 命令成功,我们就可以测试演示了 进入 c:\users\sheeraz\go\src\github.com\asticcode\go-astilectron-demo\output\windows-amd64 目录,运行 astilectron demo.exe 文件:
![astilectron demo](/uploads/allimg/230427/1-23042ft224p4.gif)
* 截至目前,我们可以看到 astilectronwindows 上的演示,它显示了打开文件夹的内存结构,但我们也可以捆绑其他操作系统的演示 将以下部分添加到 go-astilectron-demo 目录中的 bundler.json 文件中:
![bundler json](/uploads/allimg/230427/1-23042ft34j40.png)
* 添加环境后,我们必须重复该过程,为所需的操作系统创建新的 astilectron demo.exe 文件
***
## 使用 astilectronhtml 示例
在安装 astilectron 的上述步骤之后,我们现在可以为应用程序创建我们自己的 gui 让我们尝试一个使用 htmlcss 的基本示例:
用于构建和运行 html 应用程序的 golang 代码:
```go
package main
import (
    "fmt"
    "log"
    "github.com/asticode/go-astikit"
    "github.com/asticode/go-astilectron"
)
func main() {
    // set the logger
    demologger := log.new(log.writer(), log.prefix(), log.flags())
    // create astilectron with the application name and base directory
    demoastilectron, apperror := astilectron.new(demologger, astilectron.options{
        appname:           "jiyik",
        basedirectorypath: "demo",
    })
    if apperror != nil {
        demologger.fatal(fmt.errorf("main: creating the astilectron is failed: %w", apperror))
    }
    defer demoastilectron.close()
    // handle the signals
    demoastilectron.handlesignals()
    // start creating the app
    if apperror = demoastilectron.start(); apperror != nil {
        demologger.fatal(fmt.errorf("main: starting the astilectron is failed: %w", apperror))
    }
    // create a new window
    var win *astilectron.window
    if win, apperror = demoastilectron.newwindow("index.html", &astilectron.windowoptions{
        center: astikit.boolptr(true),
        height: astikit.intptr(800),
        width:  astikit.intptr(800),
    }); apperror != nil {
        demologger.fatal(fmt.errorf("main: new window creation is failed: %w", apperror))
    }
    if apperror = win.create(); apperror != nil {
        demologger.fatal(fmt.errorf("main: new window creation is failed: %w", apperror))
    }
    // blocking pattern
    demoastilectron.wait()
}

上面的代码还使用了 github.com/asticcode/go-astikit,它会在运行应用程序时由 ide 下载。 html 的代码是:

html>
<html lang="en" >
<head>
    <meta charset="utf-8">
    <title>jiyik login formtitle>
<style>
body {
    background-color: lightgreen;
    font-size: 1.6rem;
    font-family: "open sans", sans-serif;
    color: #2b3e51;
}
h2 {
    font-weight: 300;
    text-align: center;
}
p {
    position: relative;
}
input {
    display: block;
    box-sizing: border-box;
    width: 100%;
    outline: none;
    height: 60px;
    line-height: 60px;
    border-radius: 4px;
}
input[type="text"],
input[type="email"] {
    width: 100%;
    padding: 0 0 0 10px;
    margin: 0;
    color: #8a8b8e;
    border: 1px solid #c2c0ca;
    font-style: normal;
    font-size: 16px;
    position: relative;
    display: inline-block;
    background: none;
}
input[type="submit"] {
    border: none;
    display: block;
    background-color: lightblue;
    color: #fff;
    font-weight: bold;
    text-transform: uppercase;
    font-size: 18px;
    text-align: center;
}
#login-form {
    background-color: #fff;
    width: 35%;
    margin: 30px auto;
    text-align: center;
    padding: 20px 0 0 0;
    border-radius: 4px;
    box-shadow: 0px 30px 50px 0px rgba(0, 0, 0, 0.2);
}
#create-account {
    background-color: #eeedf1;
    color: #8a8b8e;
    font-size: 14px;
    width: 100%;
    padding: 10px 0;
    border-radius: 0 0 4px 4px;
}
style>
head>
<body>
<div id="login-form">
    <h2>jiyik.com loginh2>
    <form >
        <p>
            <input type="text" id="username" name="username" placeholder="username" required><i class="validation"><span>span><span>span>i>
        p>
        <p>
            <input type="email" id="email" name="email" placeholder="email address" required><i class="validation"><span>span><span>span>i>
        p>
        <p>
            <input type="submit" id="login" value="login">
        p>
    form>
        <div id="create-account">
            <p>don't have an account? <a href="#">register herea><p>
        div>
div>
body>
html>

上面的 go 代码将在 go-astilectron 窗口中运行给定的 html 代码。

上一篇:

下一篇:卸载 golang

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

发布时间:2023/04/27 浏览次数:101 分类:go

本篇文章介绍了在 golang 的 html 模板中使用 if-else 和 switch 循环。因此,只要输出是 html,就应该始终使用 html 模板包而不是文本模板。

发布时间:2023/04/27 浏览次数:184 分类:go

本篇文章介绍 nil 在 golang 中的含义,nil 是 go 编程语言中的零值,是众所周知且重要的预定义标识符。

golang 中的 lambda 表达式

发布时间:2023/04/27 浏览次数:691 分类:go

本篇文章介绍如何在 golang 中创建 lambda 表达式。lambda 表达式似乎不存在于 golang 中。 函数文字、lambda 函数或闭包是匿名函数的另一个名称。

发布时间:2023/04/27 浏览次数:136 分类:go

当我们尝试生成对象的副本时,深层副本会准确复制原始对象的所有字段。 此外,如果它有任何对象作为字段,也会制作这些对象的副本。本篇文章介绍如何在 golang 中进行深度复制。

发布时间:2023/04/27 浏览次数:104 分类:go

像错误一样,panic 发生在运行时。 换句话说,当您的 go 程序中出现意外情况导致执行终止时,就会发生 panics。让我们看一些例子来捕捉 golang 中的panics。

go 中的日志级别

发布时间:2023/04/27 浏览次数:585 分类:go

本篇文章介绍如何在 golang 中创建和使用日志级别。go 中的日志级别。golang提供了一个日志包,名为log,是一个简单的日志包。 这个包不提供分级日志; 如果我们想要分级日志记录,我们必须

在 go 中使用断言

发布时间:2023/04/27 浏览次数:585 分类:go

本篇文章介绍了 assert 在 golang 中的使用。在 go 语言中使用断言:golang 不提供对断言的任何内置支持,但我们可以使用来自 testify api 的广泛使用的第三方包断言。

go 中的随机数生成

发布时间:2023/04/27 浏览次数:206 分类:go

本篇文章介绍如何在 go 语言中使用随机数生成功能。go 中的随机数生成 go 语言为随机数生成功能提供内置支持。 内置包 math 有方法 rand(),用于随机数生成。

golang 电子邮件验证器

发布时间:2023/04/27 浏览次数:300 分类:go

本篇文章介绍如何在 go 语言中验证电子邮件。电子邮件需要特定格式; 否则,它们将无法工作。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便
网站地图