NotePad Demo Source Code – Fyne Golang GUI tutorial 71

NotePad Demo Source Code – Fyne Golang GUI tutorial 71

Here is a simple note pad coded in fyne Gui and golang.

Features of Noted Pad

You can create new file

Save Files

Save Existing Files

Modify text files

 

Source Code

<div>
<div>package main</div>
<div>import (</div>
<div>    "fmt"</div>
<div>    "io/ioutil"</div>
<div>    "os"</div>
<div>    "strconv"</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>var count int = 1</div>
<div>var filepath string</div>
<div>func main() {</div>
<div>    a := app.New()</div>
<div>    w := a.NewWindow("Fyne Note Pad")</div>
<div>    input := widget.NewMultiLineEntry()</div>
<div>    input.SetPlaceHolder("Enter text...")</div>
<div>    input.Move(fyne.NewPos(0, 0))</div>
<div>    input.Resize(fyne.NewSize(500, 500))</div>
<div>    new1 := fyne.NewMenuItem("New", func() {</div>
<div>        filepath = ""</div>
<div>        w.SetTitle("Fyne Note Pad")</div>
<div>        input.Text = ""</div>
<div>        input.Refresh()</div>
<div>    })</div>
<div>    save1 := fyne.NewMenuItem("Save", func() {</div>
<div>        if filepath != "" {</div>
<div>            f, err := os.OpenFile(filepath, os.O_WRONLY|os.O_CREATE, 0666)</div>
<div>            if err != nil {</div>
<div>                //handle error</div>
<div>            }</div>
<div>            defer f.Close()</div>
<div>            //f.Write([]byte(input.Text))</div>
<div>            f.WriteString(input.Text)</div>
<div>        } else {</div>
<div>            saveFileDialog := dialog.NewFileSave(</div>
<div>                func(r fyne.URIWriteCloser, _ error) {</div>
<div>                    textData := []byte(input.Text)</div>
<div>                    r.Write(textData)</div>
<div>                    filepath = r.URI().Path()</div>
<div>                    w.SetTitle(filepath)</div>
<div>                }, w)</div>
<div>            saveFileDialog.SetFileName("New File" + strconv.Itoa(count-1) + ".txt")</div>
<div>            saveFileDialog.Show()</div>
<div>        }</div>
<div>    })</div>
<div>    saveAs1 := fyne.NewMenuItem("Save as..", func() {</div>
<div>        saveFileDialog := dialog.NewFileSave(</div>
<div>            func(r fyne.URIWriteCloser, _ error) {</div>
<div>                textData := []byte(input.Text)</div>
<div>                r.Write(textData)</div>
<div>                filepath = r.URI().Path()</div>
<div>                w.SetTitle(filepath)</div>
<div>            }, w)</div>
<div>        saveFileDialog.SetFileName("New File" + strconv.Itoa(count-1) + ".txt")</div>
<div>        saveFileDialog.Show()</div>
<div>    })</div>
<div>    open1 := fyne.NewMenuItem("Open", func() {</div>
<div>        openfileDialog := dialog.NewFileOpen(</div>
<div>            func(r fyne.URIReadCloser, _ error) {</div>
<div>                data, _ := ioutil.ReadAll(r)</div>
<div>                result := fyne.NewStaticResource("name", data)</div>
<div>                input.SetText(string(result.StaticContent))</div>
<div>                fmt.Println(result.StaticName + r.URI().Path())</div>
<div>                filepath = r.URI().Path()</div>
<div>                w.SetTitle(filepath)</div>
<div>            }, w)</div>
<div>        openfileDialog.SetFilter(</div>
<div>            storage.NewExtensionFileFilter([]string{".txt"}))</div>
<div>        openfileDialog.Show()</div>
<div>    })</div>
<div>    menuItem := fyne.NewMenu("File", new1, save1, saveAs1, open1)</div>
<div>    menux1 := fyne.NewMainMenu(menuItem)</div>
<div>    w.SetMainMenu(menux1)</div>
<div>    w.SetContent(</div>
<div>        container.NewWithoutLayout(</div>
<div>            input,</div>
<div>        ),</div>
<div>    )</div>
<div>    w.Resize(fyne.NewSize(500, 500))</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 0 comments

Leave a Reply: