Professional Documents
Culture Documents
Let us go step by step and grab our snippet from fyne.io to see how we
can create our first app
.
While building this application we only require our app, and window from
the above syntax.
For syntax, we’ll search “read the directory in go” in google. And take the
help of the code from there.
For the exact link click here.
We already have a package main, import function, and main function
available to us.
We will copy the three import files and paste them into our import
function. Then we will copy the main function and paste that into our
main function as well.
We imported “io/ioutil”, this particular line will help us read the directory,
and return two things i.e. files and errors.
If there is some error present while reading the directory then we will log
that error.
If there is no error present while reading the directory then we will run a
loop on the range of all the files that are available in the directory. Next,
we will print the file name and whether the file is a directory or not.
Now the output of this particular code will give us the “true” and “false”
values. The files which are in folder form will give us true as output and
the ones which are just single images will give us false as output.
Now we want to work only when our directory is giving false value, i.e.
we just want to display the files which have some kind of extension
attached to it be it .jpg, .jpeg, .png, etc. For that, we’ll keep a checker.
I.e. an if condition.
As y’all must’ve seen already, that the names of our files are split by a
dot(.) and then there’s an extension of that file.
For example pepcoding.PNG
Similarly, we need to split the command after the dot(.) and add an
extension to it in our code.
Since we are using strings here we will import the “string” library as well.
Notice how I’ve written [ 1 ] at the end, you must be curious about what’s
that for ???
The [ 1 ] at the end denotes that, we are getting an array because of our
split function and we have to start our array from index 1. I.e from our
first image itself.
Here I only wrote two extensions, you can write as many as you want as
per your files and their extension format.
For that, I’ll suggest you do a Lil hovering around the internet and find
the appropriate syntax. If you don’t get what you want then click here to
find the sample code for reference.
From this code, we’ll use some libraries and another syntax to open our
images.
Inside our, if condition we will create a variable called “image” and then
take files from the source and store it inside our image variable using the
following syntax:
For this, we’ll be needing to import the canvas library as well, don’t forget
to do that.
Now, what if we want our images in different tabs, for that we’ll set a
container in our tab outside the for loop.
After creating the container for our tabs we’ll append it and will pass the
file name(), and our images in it.
After completing our program till this we will have our gallery app looking
like this:
If you want to resize your app, then you can use the following command:
You can also use image.Fillmode commands to explore different
dimensions of images such as:
Now as you can see on page number six, the location of our tab is at the
top of our prompt app/container. If you want to change that then you can
set them in whatever way you want:
For example, if you want to set your tab location at the leading side, i.e.
on the left-hand side of your window then you can use the following
command:
After applying the above modification our gallery app will look like this:
package main
import (
"fmt"
"io/ioutil"
"log"
"strings"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
)
func main() {
a := app.New()
w := a.NewWindow("Hello")
w.Resize(fyne.NewSize(720, 360))
root_src := "C:\\Users\\lenovo\\Pictures"
files, err := ioutil.ReadDir(root_src)
if err != nil {
log.Fatal(err)
}
tabs := container.NewAppTabs()
for _, file := range files {
fmt.Println(file.Name(), file.IsDir())
if !file.IsDir() {
extension := strings.Split(file.Name(), ".")[1]
if extension == "jpeg" || extension == "PNG" {
image:=
canvas.NewImageFromFile(root_src+"\\"+file.Name());
image.FillMode = canvas.ImageFillStretch
tabs.Append(container.NewTabItem(file.Name(), image))
}
}
}
tabs.SetTabLocation(container.TabLocationLeading)
w.SetContent(tabs)
w.ShowAndRun()
}
You can find the same article in lecture form on our youtube channel.
For the YouTube video link of “Gallery App in Go and fyne” - click here