为了账号安全,请及时绑定邮箱和手机立即绑定

为什么不改变 gxui 中进度条的映射?

为什么不改变 gxui 中进度条的映射?

Go
白衣非少年 2021-12-27 17:02:25
我想使用gxui的进度条,但没有达到我的预期。该示例正常工作,但更改它我没有成功。这是代码:package mainimport (    "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 仅在主线程之后执行打印。我做错了什么以及如何解决?
查看完整描述

1 回答

?
狐的传说

TA贡献1804条经验 获得超3个赞

不同之处在于您的 goroutine 进入执行 UI 例程的队列,如文档中所述:


// 调用队列 f 在 UI go-routine 上运行,在 f 可能被调用之前返回。


UI-routine 执行一个循环,所以不能同时处理改变磁带的ProgressBar。为了得到想要的结果,需要在单独的 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)

}


查看完整回答
反对 回复 2021-12-27
  • 1 回答
  • 0 关注
  • 154 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号