1

Open Text Files – Fyne GUI Golang tutorial 45

Open Text Files – Fyne GUI Golang tutorial 45

Want to read and write files in Fyne? Fortunately it is very easy.

This tutorial is about how to read a text file from your harddrive.

 

Open Text Files – Fyne GUI Golang tutorial 45

Source Code


package main
// import fyne
import (
    "io/ioutil"
    "fyne.io/fyne/v2"
    "fyne.io/fyne/v2/app"
    "fyne.io/fyne/v2/container"
    "fyne.io/fyne/v2/dialog"
    "fyne.io/fyne/v2/storage"
    "fyne.io/fyne/v2/widget"
)
func main() {
    // New app
    a := app.New()
    //New title and window
    w := a.NewWindow("Open file in FYNE")
    // resize window
    w.Resize(fyne.NewSize(400, 400))
    // New Buttton
    btn := widget.NewButton("Open .txt files", func() {
        // Using dialogs to open files
        // first argument func(fyne.URIReadCloser, error)
        // 2nd is parent window in our case "w"
        // r for reader
        // _ is ignore error
        file_Dialog := dialog.NewFileOpen(
            func(r fyne.URIReadCloser, _ error) {
                // read files
                data, _ := ioutil.ReadAll(r)
                // reader will read file and store data
                // now result
                result := fyne.NewStaticResource("name", data)
                // lets display our data in label or entry
                entry := widget.NewMultiLineEntry()
                // string() function convert byte to string
                entry.SetText(string(result.StaticContent))
                // Lets show and setup content
                // tile of our new window
                w := fyne.CurrentApp().NewWindow(
                    string(result.StaticName)) // title/name
                w.SetContent(container.NewScroll(entry))
                w.Resize(fyne.NewSize(400, 400))
                // show/display content
                w.Show()
                // we are almost done
            }, w)
        // fiter to open .txt files only
        // array/slice of strings/extensions
        file_Dialog.SetFilter(
            storage.NewExtensionFileFilter([]string{".txt"}))
        file_Dialog.Show()
        // Show file selection dialog.
    })
    // lets show button in parent window
    w.SetContent(container.NewVBox(
        btn,
    ))
    w.ShowAndRun()
}

 

[easy_media_download url=”http://blogvali.com/wp-content/uploads/fyne-golang-downloads/main45.go” text=”Download Code” color=”red_darker”]

Fyne Golang GUI Course

Tony BB
 

TonyBB is a Coach , marketer, hypnotist and a founder of RSKVF Production who specializes in providing simple, affordable, and easy to use solutions for Life.

Click Here to Leave a Comment Below 1 comments
CASwow - a couple of years ago

Short and simple

Reply

Leave a Reply: