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

huangapple go评论100阅读模式
英文:

Why not changing the mapping of the progress bar in gxui?

问题

我想使用gxui中的进度条,但结果与我预期的不同。
示例按预期工作,但我无法进行更改。以下是代码:

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都是在主线程之后才执行打印操作。
我做错了什么,如何修复?

英文:

I wanted to use the Progress bar from gxui, but get not what I expected.
The example works as it should,but change it I did not succeed. Here is the code:

package main
import (
&quot;fmt&quot;
&quot;io/ioutil&quot;
&quot;log&quot;
&quot;net/http&quot;
&quot;time&quot;
&quot;github.com/google/gxui&quot;
&quot;github.com/google/gxui/drivers/gl&quot;
&quot;github.com/google/gxui/math&quot;
&quot;github.com/google/gxui/samples/flags&quot;
)
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(&quot;Start&quot;)
t0 := time.Now()
button.OnClick(func(gxui.MouseEvent) {
progressBar.SetTarget(100)
N := 100
for count := 0; count &lt; N; count++ {
resp, err := http.Get(&quot;http://example.com&quot;)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
if count%10 == 0 {
go func() {
driver.Call(func() {
fmt.Println(&quot;Tuk&quot;)
progressBar.SetProgress(count * 100 / N)
})
}()
fmt.Println(count)
fmt.Println(ioutil.ReadAll(resp.Body))
fmt.Printf(&quot;Elapsed time: %v\n&quot;, time.Since(t0))
}
}
progressBar.SetProgress(50)
})
layout.AddChild(button)
layout.AddChild(progressBar)
window := theme.CreateWindow(500, 100, &quot;Test&quot;)
window.SetScale(flags.DefaultScaleFactor)
window.AddChild(layout)
window.OnClose(driver.Terminate)
}
func main() {
gl.StartDriver(appMain)
}

Since I used goroutine, it is assumed that the output text will alternate, but all goroutine performed print only after the main thread.
What am I doing wrong and how to fix it?

1: https://github.com/google/gxui "gxui"
2: https://github.com/google/gxui/blob/master/samples/progress_bar/main.go "example"

答案1

得分: 1

区别在于你的goroutine进入队列执行UI例程,正如文档中所写:

// 调用将f排队在UI go例程上运行,返回在f可能已被调用之前。

UI例程执行循环,因此无法同时处理更改的进度条。为了获得所需的结果,需要在单独的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)
}
英文:

The difference is that your goroutine enter the queue executing the UI routine, as written in the documentation:

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

UI-routine executes a loop, so can not simultaneously handle the change
tape ProgressBar. To get the desired result, it is necessary run handling function in a separate goroutine. Modified code:

package main
import (
&quot;fmt&quot;
&quot;io/ioutil&quot;
&quot;log&quot;
&quot;net/http&quot;
&quot;time&quot;
&quot;github.com/google/gxui&quot;
&quot;github.com/google/gxui/drivers/gl&quot;
&quot;github.com/google/gxui/math&quot;
&quot;github.com/google/gxui/samples/flags&quot;
)
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(&quot;Start&quot;)
t0 := time.Now()
button.OnClick(func(gxui.MouseEvent) {
go func() {
N := 100
for count := 0; count &lt; N; count++ {
resp, err := http.Get(&quot;http://example.com&quot;)
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(&quot;Elapsed time: %v\n&quot;, time.Since(t0))
}
progressBar.SetTarget(100)
}()
})
layout.AddChild(button)
layout.AddChild(progressBar)
window := theme.CreateWindow(500, 100, &quot;Test&quot;)
window.SetScale(flags.DefaultScaleFactor)
window.AddChild(layout)
window.OnClose(driver.Terminate)
}
func main() {
gl.StartDriver(appMain)
}

huangapple
  • 本文由 发表于 2016年3月26日 02:47:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/36226039.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定