在Golang应用程序中使用Fyne创建一个详细的控制台。

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

Create a verbose console inside Golang app using Fyne

问题

从使用Python和PyQt的GUI过渡到现在,我习惯在我的程序中添加一种控制台。目的是向用户显示有关正在进行的进程、遇到的执行错误等信息。

在Python/PyQt中,我使用QLineEdit来实现这一点。它非常容易使用。只需在我的GUI中创建并插入小部件,并通过调用appen()为每个信息添加一行。

例如,控制台可以在加载esedb文件时显示“esedb loading”,加载完成后显示“esedb file loaded”,然后在下一步显示“esedb parsing”等等。

现在,我正在学习Golang和Fyne,并且正在寻找一种类似的方法。

我找到了widget.NewTextGrid(),但它的工作方式不符合我的期望。
我不能只是追加新行。如果我理解正确,我必须将文本存储在一个字符串变量中。

你能给我一些建议吗?

谢谢!

package main

import (
	// "fmt"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/canvas"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/layout"
	"fyne.io/fyne/v2/theme"
	"fyne.io/fyne/v2/widget"
)

func main() {

	myapp := app.New()

	myappGui := myapp.NewWindow("Example")
	myappGui.Resize(fyne.NewSize(400, 600))

	textConsole := widget.NewTextGrid()
英文:

Comming from Python with PyQt gui, I was used to add kind of console in my programm. The purpose was to indicate to the user information on the processes in progress, on the execution errors encountered, etc.

In Python/PyQt, I was using QLineEdit to do that. It was pretty easy to use. Just create and insert the widget in my gui and add a row for each information by calling appen().

For example, the console could say "esedb loading" when loading an esedb file, then "esedb file loaded" when finished, then "esedb parsing" for the next step, etc...

Now, I'm learning Golang with Fyne and I'm looking for a way to do something similar.

I found widget.NewTextGrid() but it doesn't work as I expect.
I can't just append new line. If I understand well, I have to store text in a string variable

Could you advice me about the way to do that ?

Thanks!

package main

import (
	//"fmt"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/canvas"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/layout"
	"fyne.io/fyne/v2/theme"
	"fyne.io/fyne/v2/widget"
)

func main() {

	myapp := app.New()

	myappGui := myapp.NewWindow("Example")
	myappGui.Resize(fyne.NewSize(400, 600))

	textConsole := widget.NewTextGrid()

答案1

得分: 1

TextGrid是一个复杂的组件,用于在等宽排列中管理特定字符的字体样式(例如终端等)。

为了提高性能,我建议在一个滚动小部件中使用VBox,其中每一行都是一个附加的标签(您也可以将它们设置为等宽字体样式)。如果您希望文本具有交互性,那么根据其他答案所说,NewMultiLineEntry可能适合您。

文本是复杂的,我们正在努力优化更多复杂用法和大文件处理,所以在以后的版本中它会变得更流畅...

英文:

TextGrid is a complex component designed for managing character specific font styles in a monospace arrangement (like a terminal etc).

For performance I would recommend a VBox in a Scroll widget where each line is another appended Label (you can set them to monospace text style as well). If you want the text to be interactive then as other answers have said the NewMultiLineEntry is likely for you.

Text is complex and we are working hard to optimise more of the complex usages and large file handling, so it will get smoother in later releases…

答案2

得分: 0

widget.TextGrid没有一个方法来追加一行,但是它支持使用TextGrid.Text()查询其当前内容。所以你可以设置一个新的文本,它是当前内容和新行的连接,例如:

textConsole.SetText(textConsole.Text() + "\n" + line)

但要知道widget.TextGrid不支持滚动:它的大小将由其字符串内容决定。当然,你可以通过使用container.Scroll使其可滚动。

例如:

func main() {
    myapp := app.New()

    w := myapp.NewWindow("Example")
    w.Resize(fyne.NewSize(500, 300))

    textConsole := widget.NewTextGrid()

    scrollPane := container.NewScroll(textConsole)
    w.SetContent(scrollPane)
    go func() {
        for {
            textConsole.SetText(textConsole.Text() + time.Now().String() + "\n")
            scrollPane.ScrollToBottom()
            time.Sleep(time.Second)
        }
    }()

    w.ShowAndRun()
}

或者你可以使用多行widget.Entry。它也支持选择其中的任何部分,并且默认情况下是可编辑的。当然,你可以禁用编辑。它默认支持滚动。

看看这个例子:

func main() {
    myapp := app.New()

    w := myapp.NewWindow("Example")
    w.Resize(fyne.NewSize(500, 300))

    textConsole := widget.NewMultiLineEntry()
    textConsole.Disable() // 禁用编辑

    w.SetContent(textConsole)
    go func() {
        for {
            textConsole.SetText(textConsole.Text + time.Now().String() + "\n")
            time.Sleep(time.Second)
        }
    }()

    w.ShowAndRun()
}
英文:

widget.TextGrid does not have a method to append a line, but it does support querying its current content using TextGrid.Text(). So what you may do is set a new text that is its current content and the new line concatenated, e.g.:

textConsole.SetText(textConsole.Text() + "\n" + line)

But know that widget.TextGrid does not support scrolling: its size will be dictated by its string content. You can make it scrollable of course by using a container.Scroll.

For example:

func main() {
	myapp := app.New()

	w := myapp.NewWindow("Example")
	w.Resize(fyne.NewSize(500, 300))

	textConsole := widget.NewTextGrid()

	scrollPane := container.NewScroll(textConsole)
	w.SetContent(scrollPane)
	go func() {
		for {
			textConsole.SetText(textConsole.Text() + time.Now().String() + "\n")
			scrollPane.ScrollToBottom()
			time.Sleep(time.Second)
		}
	}()

	w.ShowAndRun()
}

Alternatively you may use a multiline widget.Entry. It also supports selecting any part of it, and by default it's also editable. You may disable editing of course. It supports scrolling by default.

See this example:

func main() {
	myapp := app.New()

	w := myapp.NewWindow("Example")
	w.Resize(fyne.NewSize(500, 300))

	textConsole := widget.NewMultiLineEntry()
	textConsole.Disable() // Disable editing

	w.SetContent(textConsole)
	go func() {
		for {
			textConsole.SetText(textConsole.Text + time.Now().String() + "\n")
			time.Sleep(time.Second)
		}
	}()

	w.ShowAndRun()
}

huangapple
  • 本文由 发表于 2022年12月6日 16:45:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/74699595.html
匿名

发表评论

匿名网友

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

确定