Display Item Details – Fyne Golang GUI Tutorial 60

Display Item Details – Fyne Golang GUI Tutorial 60

<div>package main</div>
<div>// import fyne</div>
<div>import (</div>
<div>    "encoding/json"</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/widget"</div>
<div>func main() {</div>
<div>    // student struct, you can use any name</div>
<div>    type Student struct {</div>
<div>        Name  string // name N is capital</div>
<div>        Phone string</div>
<div>    }</div>
<div>    // Now creat a slice/ array to store data</div>
<div>    var myStudentData []Student</div>
<div>    // welcome again</div>
<div>    //lets read data from file and display it in list</div>
<div>    data_from_file, _ := ioutil.ReadFile("myFile_name.txt")</div>
<div>    // file name with extension .txt or .json</div>
<div>    // unmarshall/parse data received from file and save/push in slice</div>
<div>    // 2 argument 1. data source, 2. data slice to store data</div>
<div>    json.Unmarshal(data_from_file, &amp;myStudentData)</div>
<div>    // now we can use this data in our list</div>
<div>    // lets create our list</div>
<div>    // new app</div>
<div>    a := app.New()</div>
<div>    // new title and window</div>
<div>    w := a.NewWindow("CRUD APP")</div>
<div>    // resize window</div>
<div>    w.Resize(fyne.NewSize(400, 400))</div>
<div>    // New label to dispaly name and phone number details</div>
<div>    l_name := widget.NewLabel("...")</div>
<div>    l_name.TextStyle = fyne.TextStyle{Bold: true}</div>
<div>    // for phone number</div>
<div>    l_phone := widget.NewLabel("...")</div>
<div>    // entry widget for name</div>
<div>    e_name := widget.NewEntry()</div>
<div>    e_name.SetPlaceHolder("Enter name here...")</div>
<div>    // entry widget for phone</div>
<div>    e_phone := widget.NewEntry()</div>
<div>    e_phone.SetPlaceHolder("Enter phone here...")</div>
<div>    // submit button</div>
<div>    submit_btn := widget.NewButton("Submit", func() {</div>
<div>        // logic part- store our data in json format</div>
<div>        // let create a struct for our data</div>
<div>        // Get data from entry widget and push to slice</div>
<div>        myData1 := &amp;Student{</div>
<div>            Name:  e_name.Text, // data from name entry widget</div>
<div>            Phone: e_phone.Text,</div>
<div>        }</div>
<div>        // append / push data to our slice</div>
<div>        myStudentData = append(myStudentData, *myData1)</div>
<div>        // * star is very important</div>
<div>        // convert / parse data to json format</div>
<div>        // 3 arguments</div>
<div>        // 1st is our slice data</div>
<div>        // ignore 2nd</div>
<div>        // 3rd is identation, we are using space to indent our data</div>
<div>        final_data, _ := json.MarshalIndent(myStudentData, "", " ")</div>
<div>        // writing data to file</div>
<div>        // it take 3 things</div>
<div>        //file name .txt or .json or anything you want to use</div>
<div>        // data source, final_data in our case</div>
<div>        // writing/reading/execute permission</div>
<div>        ioutil.WriteFile("myFile_name.txt", final_data, 0644)</div>
<div>        /// we are done :)</div>
<div>        // lets test</div>
<div>        // empty input field after data insertion</div>
<div>        e_name.Text = ""</div>
<div>        e_phone.Text = ""</div>
<div>        // refresh name &amp; phone entry object</div>
<div>        e_name.Refresh()</div>
<div>        e_phone.Refresh()</div>
<div>    })</div>
<div>    // list widget</div>
<div>    list := widget.NewList(</div>
<div>        // first argument is item count</div>
<div>        // len() function to get myStudentData slice len</div>
<div>        func() int { return len(myStudentData) },</div>
<div>        // 2nd argument is for widget choice. I want to use label</div>
<div>        func() fyne.CanvasObject { return widget.NewLabel("") },</div>
<div>        // 3rd argument is to update widget with our new data</div>
<div>        func(lii widget.ListItemID, co fyne.CanvasObject) {</div>
<div>            co.(*widget.Label).SetText(myStudentData[lii].Name)</div>
<div>        },</div>
<div>    )</div>
<div>    // update on clicked/ selected</div>
<div>    list.OnSelected = func(id widget.ListItemID) {</div>
<div>        l_name.Text = myStudentData[id].Name</div>
<div>        l_name.Refresh()</div>
<div>        // for phone number</div>
<div>        l_phone.Text = myStudentData[id].Phone</div>
<div>        l_phone.Refresh()</div>
<div>    }</div>
<div>    // show and run</div>
<div>    w.SetContent(</div>
<div>        // lets create Hsplite container</div>
<div>        container.NewHSplit(</div>
<div>            // first argument is list of data</div>
<div>            list,</div>
<div>            // 2nd is</div>
<div>            // vbox container</div>
<div>            // show both label</div>
<div>            container.NewVBox(l_name, l_phone, e_name, e_phone, submit_btn),</div>
<div>        ),</div>
<div>    )</div>
<div>    w.ShowAndRun()</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: