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

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

 

Download Code

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 - last year

Short and simple

Reply

Leave a Reply: