本文介绍: Go 语言生态中,GUI 一直是短板,更别说跨平台的 GUI 了。fyne向前迈了一大步。fyne 是 Go 语言编写跨平台的 UI 库,它可以很方便地移植手机设备上。fyne使用上非常简单,同时它还提供fyne命令打包静态资源应用程序我们简单介绍基本控件布局然后介绍如何发布一个fyne应用程序

Fyne (go 跨平台 GUI ) 入门中文文档1开始

hello word

A simple app starts by creating an app instance with app.New() and then opening a window with app.NewWindow(). Then a widget tree is defined that is set as the main content with SetContent() on a window. The app UI is then shown by calling ShowAndRun() on the window.

一个简单应用程序首先使用 app.New() 去创建一个应用实例然后使用app.NewWindow() 打开一个窗口然后定义一个部件树再在窗口使用SetContent() 将其设置为主要内容通过ShowAndRun() 在窗口显示应用ui

package main


import (

"fyne.io/fyne/v2/app"

"fyne.io/fyne/v2/widget"

)


func main() {

a := app.New()

w := a.NewWindow("Hello World")


w.SetContent(widget.NewLabel("Hello World!"))

w.ShowAndRun()

}

运行结果

 

Application and RunLoop应用事件 runloop

For a GUI application to work it needs to run an event loop (sometimes called a runloop) that processes user interactions and drawing events. In Fyne this is started using the or functions. One of these must be called from the end of your setup code in thfunction.App.Run()Window.ShowAndRun()main()

An application should only have one runloop and so you should only call once in your code. Calling it a second time will cause errors.Run()

gui 应用工作需要运行一个事件循环(有时也叫 runloop)来处理用户交互绘图事件。在Fyne中这是使用App.Run()Window.ShowAndRun()函数开始的。并且他们必须设置在代码末尾

一个应用程序应该需要一个事件循环runloop)所以你应该在你的代码中只调用一次run()函数。在第二次调用时候可能会发生错误

package main


import (

"fmt"


"fyne.io/fyne/v2/app"

"fyne.io/fyne/v2/widget"

)


func main() {

myApp := app.New()

myWindow := myApp.NewWindow("Hello")

myWindow.SetContent(widget.NewLabel("Hello"))


myWindow.Show()

myApp.Run()

tidyUp()

}


func tidyUp() {

fmt.Println("Exited")

}

运行结果

 

点击x 图标打印 exited

For desktop runtimes an app can be quit directly by calling (mobile apps do not support this) – normally not needed in developer code. An application will also quit once all the windows are closed. See also that functions executed after will not be called until the application exits.

App.Quit() Run()

对于桌面应用运行时,可以通过直接调用 App.Quit() 直接退出应用程序移动应用程序并不支持)-在开发人员代码中并不需要,在关闭所有窗口后应用程序也将会退出。请看run() 在应用程序退出前不会调用之后执行函数

更新内容Updating Content

Having completed the hello world tutorial or other examples you will have created a basic user interface. In this page we see how the content of a GUI can be updated from your code.

The first step is to assign the widget you want to update to a variable. In the hello world tutorial we passed directly into , to update it we change that to two different lines, such as:widget.NewLabelSetContent()

完成 hello world 教程或者其他示例后,你将创建一个基本的用户界面,我们会看到如何将去更新代码里的gui 内容

一步是将要更新的小部件分配给一个变量,在hello world 教程里我们通过直接设置,为了更新他我们改变为两个不同的行,例如:widget.NewLabelSetContent()

clock := widget.NewLabel("")

w.SetContent(clock)

Once the content has been assigned to a variable we can call functions like . For our example we will set the content of our label to the current time, with the help of . SetText(“new text“) Time.Format

内容分配给变量后我们可以如同调用函数那样调用他,例如,我们可以将标签内容设置为当前时间使用 SetText(“new text“)

formatted := time.Now().Format("Time: 03:04:05")

clock.SetText(formatted)

package main



import (

   "time"


   "fyne.io/fyne/v2/app"

   "fyne.io/fyne/v2/widget"

)


func main() {

   myApp := app.New()

   myWindow := myApp.NewWindow("Hello")

   //第一步是将要更新的小部件分配给变量。

   clock := widget.NewLabel("")

   myWindow.SetContent(clock)

   //将标签内容设置为当前时间 Time.Format

   formatted := time.Now().Format("Time: 03:04:05")

   clock.SetText(formatted)

   myWindow.Show()

   myApp.Run()

}

运行结果

 

That is all we need to do to change content of a visible item (see below for the full code). However, we can go further and update content on a regular basis

这就是更改内容所需的全部工作,然而我们也可以更进一步定时更改内容

后台运行更新 GUI 的内容 running in the background

 Most applications will need to have processes that run in the background, for example downloading data or responding to events. To simulate this we will extend the code above to run every second.

Like with most go code we can create a goroutine (using the keyword) and run our code there. If we move the text update code to a new function it can be called on initial display as well as on a timer for regular updating. By combining a goroutine and the inside a for loop we can update the label every second.gotime.Tick

大多数应用程序需要后台运行进程例如下载数据或者响应事件。 我们将会拓展上面的的代码每秒运行一次

与大多数go代码一样,我们会创建 goroutine 使用关键字,并在哪里运行代码。如果我们将文本更新代码移动到一个新的函数,他可以在初始化或者像一个定时器有规律的被调用通过结合 goroutinegotime.tick 的 for 循环,我们可以每秒都更新这个标签

go func() {

for range time.Tick(time.Second) {

updateTime(clock)

}

}()

It is important to place this code before or calls because they will not return until the application closes. With all of this together the code will run and update the user interface each second, creating a basic clock widget. The full code is as follows: ShowAndRun Run

这个代码放在ShowAndRun 和Run 调用前是很重要的,因为他们在应用关闭之前是不会返回的。这些代码每秒都会运行和更新用户界面创建一个基本的时钟部件代码如下

package main


import (

"time"


"fyne.io/fyne/v2/app"

"fyne.io/fyne/v2/widget"

)


func updateTime(clock *widget.Label) {

formatted := time.Now().Format("Time: 03:04:05")

clock.SetText(formatted)

}


func main() {

a := app.New()

w := a.NewWindow("Clock")


clock := widget.NewLabel("")

updateTime(clock)


w.SetContent(clock)

go func() {

for range time.Tick(time.Second) {

updateTime(clock)

}

}()

w.ShowAndRun()

}

运行效果 时间每秒都在变化

 

Window Handling 窗口处理

Windows are created using and need to be shown using the function. The helper method on allows you to show your window and run the application at the same time.App.NewWindow()Show()ShowAndRun()fyne.Window

窗口是使用App.NewWindow()创建的,并且需要show()函数显示出来。这些帮助方法将允许你同时创建自己的窗口和运行应用程序

By default a window will be the right size to show its content by checking the function (more on that in later examples). You can set a larger size by calling the method. Into this is passed a which contains a width and height using device independent pixels (meaning that it will be the same across different devices), for example to make a window square by default we could:MinSize()Window.Resize()fyne.Size

默认情况下,窗口的正确大小显示的内容是通过检查MinSize()函数实现的,在后面更多的实例看到,你可以设置一个较大的大小通过Window.Resize()

函数来实现。传入 fyne.Size {其中包含着使用设备独立像素的宽和高},例如创建一个正方型的窗口我们可以通过默认的代码创建如下

w.Resize(fyne.NewSize(100, 100))

Be aware that the desktop environment may have constraints that cause windows to be smaller than requested. Mobile devices will typically ignore this as they are only displayed at fullscreen.

需要的是,在桌面开发环境可能导致窗口小于请求窗口的限制移动设备一般会忽略这一点,因为他们通常只选择全屏显示

If you wish to show a second window you must only call the function. It can also be helpful to split from if you want to open multiple windows when your application starts. The example below shows how to load two windows when starting.Show()Window.Show()App.Run()

如果你希望显示第二个窗口,你必须只使用Show()方法,如果你想要打开多个窗口在应用程序运行时候,分开对此很有帮助。 下面的例子将会演示如何加载两个窗口。

运行效果

 

原文地址:https://blog.csdn.net/qq_51768842/article/details/129867352

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_33934.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注