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 the function.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 使用关键字,并在哪里运行代码。如果我们将文本更新代码移动到一个新的函数,他可以在初始化或者像一个定时器有规律的被调用。通过结合 goroutine 和gotime.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 full-screen.
需要的是,在桌面开发环境可能导致窗口小于请求窗口的限制,移动设备一般会忽略这一点,因为他们通常只选择全屏显示。
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()方法,如果你想要打开多个窗口在应用程序运行的时候,分开对此很有帮助。 下面的例子将会演示如何加载两个窗口。
运行效果
版权归原作者 行 待 所有, 如有侵权,请联系我们删除。