简体   繁体   中英

Create a verbose console inside Golang app using Fyne

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()

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…

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, eg:

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()
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM