gpt4 book ai didi

go - 为什么不更改 gxui 中进度条的映射?

转载 作者:IT王子 更新时间:2023-10-29 02:23:22 24 4
gpt4 key购买 nike

我想使用 gxui 中的进度条,但没有达到我的预期。example正常工作,但改变它我没有成功。这是代码:

package main

import (
"fmt"
"io/ioutil"
"log"
"net/http"
"time"

"github.com/google/gxui"
"github.com/google/gxui/drivers/gl"
"github.com/google/gxui/math"
"github.com/google/gxui/samples/flags"
)

func appMain(driver gxui.Driver) {
theme := flags.CreateTheme(driver)

layout := theme.CreateLinearLayout()
layout.SetHorizontalAlignment(gxui.AlignCenter)

progressBar := theme.CreateProgressBar()
progressBar.SetDesiredSize(math.Size{W: 480, H: 60})

button := theme.CreateButton()
button.SetText("Start")
t0 := time.Now()
button.OnClick(func(gxui.MouseEvent) {
progressBar.SetTarget(100)
N := 100

for count := 0; count < N; count++ {
resp, err := http.Get("http://example.com")
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()

if count%10 == 0 {

go func() {
driver.Call(func() {
fmt.Println("Tuk")
progressBar.SetProgress(count * 100 / N)
})
}()
fmt.Println(count)
fmt.Println(ioutil.ReadAll(resp.Body))
fmt.Printf("Elapsed time: %v\n", time.Since(t0))
}
}
progressBar.SetProgress(50)
})

layout.AddChild(button)
layout.AddChild(progressBar)

window := theme.CreateWindow(500, 100, "Test")
window.SetScale(flags.DefaultScaleFactor)
window.AddChild(layout)
window.OnClose(driver.Terminate)
}

func main() {
gl.StartDriver(appMain)
}

由于我使用了goroutine,假设输出文本会交替,但是所有goroutine只在主线程之后才执行打印。我做错了什么以及如何解决?

最佳答案

不同之处在于你的 goroutine 进入执行 UI 例程的队列,如 documentation 中所写:

// Call queues f to be run on the UI go-routine, returning before f may have been called.

UI-routine 执行一个循环,所以不能同时处理变化磁带进度条。为了获得期望的结果,有必要在单独的 goroutine 中运行处理函数。修改代码:

package main

import (
"fmt"
"io/ioutil"
"log"
"net/http"
"time"

"github.com/google/gxui"
"github.com/google/gxui/drivers/gl"
"github.com/google/gxui/math"
"github.com/google/gxui/samples/flags"
)

func appMain(driver gxui.Driver) {
theme := flags.CreateTheme(driver)

layout := theme.CreateLinearLayout()
layout.SetHorizontalAlignment(gxui.AlignCenter)

progressBar := theme.CreateProgressBar()
progressBar.SetDesiredSize(math.Size{W: 480, H: 60})
progressBar.SetTarget(100)
button := theme.CreateButton()
button.SetText("Start")
t0 := time.Now()
button.OnClick(func(gxui.MouseEvent) {
go func() {
N := 100
for count := 0; count < N; count++ {
resp, err := http.Get("http://example.com")
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()

driver.Call(func() {
progressBar.SetProgress(count * 100 / N)
})

fmt.Println(count)
fmt.Println(ioutil.ReadAll(resp.Body))
fmt.Printf("Elapsed time: %v\n", time.Since(t0))

}
progressBar.SetTarget(100)
}()
})

layout.AddChild(button)
layout.AddChild(progressBar)

window := theme.CreateWindow(500, 100, "Test")
window.SetScale(flags.DefaultScaleFactor)
window.AddChild(layout)
window.OnClose(driver.Terminate)

}

func main() {
gl.StartDriver(appMain)
}

关于go - 为什么不更改 gxui 中进度条的映射?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36226039/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com