在Fyne TextGrid中自动滚动日志文本

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

Scrolling log text in a Fyne TextGrid automatically

问题

我正在尝试使用一个小部件来显示文本,随着添加更多文本,文本将垂直滚动。

然而,我无法弄清楚如何使文本滚动。我最接近的方法是启用滚动,但是当文本被添加时,你必须手动向下滚动。我还没有找到一种"锁定"调整大小的方法;如果不将TextGrid放入滚动容器中,应用程序似乎会超出监视器的边界。

有没有办法让TextGrid自动滚动?

appClient := app.New()
winWindow := appClient.NewWindow("Test")

txtEntry := widget.NewEntry()
txtStatus := widget.NewLabel("Status")
txtResults := widget.NewTextGrid()
txtResults.ShowLineNumbers = true

btnQuit := widget.NewButton("Quit", func() {
    appClient.Quit()
})
btnQuit.Resize(fyne.NewSize(300, 300))

cntScrolling := container.NewScroll(txtResults)
cntButtons := container.NewGridWithColumns(4, layout.NewSpacer(), layout.NewSpacer(), layout.NewSpacer(), btnQuit)
cntContent := container.NewGridWithRows(4, txtEntry, txtStatus, cntScrolling, cntButtons)

winWindow.Resize(fyne.NewSize(1200, 600))
winWindow.SetContent(cntContent)

go func() {
    for {
        select {
        case strMessage := <-chnSendMessageToClientWindow:
            txtResults.SetText(strings.TrimPrefix(txtResults.Text()+"\n"+strMessage, "\n"))
            cntScrolling.Refresh()
        }
    }
}()

winWindow.ShowAndRun()
英文:

I'm trying to use a widget to display text which will scroll vertically as more text is added.

I'm unable to figure out how to get the text to scroll, though. The closest I've come is enabling scrolling, but you have to manually scroll down as text is appended. I haven't found a way to "lock" resizing; if don't put the TextGrid into a scroll container, the application seems to jump in size outside the boundary of the monitor.

Is there a way to get the TextGrid to scroll automatically?

	appClient := app.New()
	winWindow := appClient.NewWindow(&quot;Test&quot;)

	txtEntry := widget.NewEntry()
	txtStatus := widget.NewLabel(&quot;Status&quot;)
	txtResults := widget.NewTextGrid()
	txtResults.ShowLineNumbers = true

	btnQuit := widget.NewButton(&quot;Quit&quot;, func() {
		appClient.Quit()
	})
	btnQuit.Resize(fyne.NewSize(300, 300))

	cntScrolling := container.NewScroll(txtResults)
	cntButtons := container.NewGridWithColumns(4, layout.NewSpacer(), layout.NewSpacer(), layout.NewSpacer(), btnQuit)
	cntContent := container.NewGridWithRows(4, txtEntry, txtStatus, cntScrolling, cntButtons)

	winWindow.Resize(fyne.NewSize(1200, 600))
	winWindow.SetContent(cntContent)

	go func() {
		for {
			select {
			case strMessage := &lt;-chnSendMessageToClientWindow:
				txtResults.SetText(strings.TrimPrefix(txtResults.Text()+&quot;\n&quot;+strMessage, &quot;\n&quot;))
				cntScrolling.Refresh()
			}
		}
	}()

	winWindow.ShowAndRun()

答案1

得分: 1

你可能正在寻找 cntScrolling.ScrollToBottom() 这行代码。当你将项目放在滚动容器中时,无法在添加新内容时自动向下或向右滚动,但是每次添加内容时,这行代码都会实现这个功能。

我还没有找到一种“锁定”大小的方法

在fyne中,项目通常要么是最小尺寸(自适应内容),要么是扩展以填充空间。在中间某个位置“锁定大小”是不明智的,因为这样UI在不同屏幕和窗口大小上无法良好缩放。

英文:

You’re probably looking for cntScrolling.ScrollToBottom()
When you wrap items in a scroll container there is no way to automatically scroll down or right when new content is added, but that line will do that each time you add content.

> I haven't found a way to "lock" resizing

Items in fyne are typically either minimum size (pack the content) or expand to fill the space. It is unwise to “lock size” somewhere in between because then the UI does not scale well across screens and window sizes.

huangapple
  • 本文由 发表于 2021年10月23日 12:07:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/69685039.html
匿名

发表评论

匿名网友

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

确定