In this video, I demonstrate how to create a simple countdown timer using the Fyne library in Go. Follow along as I explain the code step-by-step, showing you how to set up the timer, sync it with a label, and display the countdown in a graphical window. This tutorial is perfect for beginners looking to learn more about Go and the Fyne library. Don’t forget to like, comment, and subscribe for more programming tutorials!

STEP: 1 main package
package main

STEP:2 import all necessary packages

import (
“fmt” // Importing fmt package for formatted I/O
“fyne.io/fyne/v2” // Importing fyne package for GUI
“fyne.io/fyne/v2/app” // Importing fyne app package
“fyne.io/fyne/v2/container” // Importing fyne container package for layout
“fyne.io/fyne/v2/widget” // Importing fyne widget package for UI elements
“time” // Importing time package for timer functionality
)

STEP:3 create main function

func main() {}

STEP:4 Creating a new Fyne application
a := app.New()

STEP:5 Creating a new window with the title “Timer Example”
w := a.NewWindow(“Timer Example”)

STEP:6 Creating a label to display the remaining time
timerLabel := widget.NewLabel(“Time remaining: 10 seconds”)

STEP:7 Create a timer function
go func() {
STEP:A Initial remaining time set to 10 seconds
remaining := 10
for remaining > 0 {
STEP:B Wait for 1 second
time.Sleep(1 * time.Second)
STEP:C Decrement the remaining time by 1
remaining–
STEP:D Update the label with the remaining time
timerLabel.SetText(fmt.Sprintf(“Time remaining: %d seconds”, remaining))

}
STEP:E Display “Time’s Up!” When the timer reacher zero
timerLabel.SetText(“Time’s up!”)
}()

STEP:8 Organize components in a vertical box layout
w.SetContent(
//Set the content of the window to a vetical box containing the timer label
container.NewVBox(timerLabel))

STEP:9 Resize the window to 400×400 pixels
w.Resize(fyne.NewSize(400, 400))

STEP:10 Display the window and start the application
w.ShowAndRun()

SOURCE CODE

// Main package
package main

import (
    "fmt"                   // Importing fmt package for formatted I/O
    "fyne.io/fyne/v2"       // Importing fyne package for GUI
    "fyne.io/fyne/v2/app"   // Importing fyne app package
    "fyne.io/fyne/v2/container" // Importing fyne container package for layout
    "fyne.io/fyne/v2/widget"    // Importing fyne widget package for UI elements
    "time"                  // Importing time package for timer functionality
)

func main() {
    a := app.New() // Creating a new Fyne application
    w := a.NewWindow("Timer Example") // Creating a new window with the title "Timer Example"

    timerLabel := widget.NewLabel("Time remaining: 10 seconds") // Creating a label to display the remaining time

    // Create a timer function
    go func() {
        remaining := 10 // Initial remaining time set to 10 seconds
        for remaining > 0 {
            time.Sleep(1 * time.Second) // Wait for 1 second
            remaining-- // Decrement the remaining time by 1
            timerLabel.SetText(fmt.Sprintf("Time remaining: %d seconds", remaining)) // Update the label text with the remaining time
        }
        timerLabel.SetText("Time's up!") // Display "Time's up!" when the timer reaches 0
    }()

    // Organize components in a vertical box layout
    w.SetContent(container.NewVBox(timerLabel)) // Set the content of the window to a vertical box containing the timer label
    w.Resize(fyne.NewSize(400, 400)) // Resize the window to 400x400 pixels
    w.ShowAndRun() // Display the window and start the application
}

#GoLang #FyneLibrary #Programming #CodingTutorial #GoProgramming #GUI #SoftwareDevelopment #CountdownTimer #TechTutorial #LearnProgramming

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: