Create a Quotes Application with Fyne GUI in Go – Label Widget Tutorial

In this tutorial, we will dive into building a simple yet powerful Quotes Application using the Fyne GUI framework in Go (Golang). You’ll learn how to utilize the Label widget to display quotes dynamically, enhancing your understanding of Fyne’s capabilities. This video will guide you step-by-step through the process of creating a functional GUI application.

STEP:1 Define the main package.
package main

STEP:2 importing all the packages

import (
"fyne.io/fyne/v2" // Import the Fyne package for GUI development
"fyne.io/fyne/v2/app" // Import the app package to create a new application
"fyne.io/fyne/v2/container" // Import the container package for layout management
"fyne.io/fyne/v2/widget" // Import the widget package for UI components
"time" // Import the time package for handling time-related functions
)

STEP:3 Define the main function.

func main() {}
STEP:4 Create a new application instance
a := app.New()
STEP:5  Create a new window with the title “Quotes and Tips”
w := a.NewWindow("Quotes and Tips")
STEP:6 Define a slice of quotes to display
quotes := []string{
"Stay positive!",
"Believe in yourself.",
"Keep going, you're doing great!",
"Every day is a new opportunity.",
"Don't watch the clock; do what it does. Keep going.",
}
STEP:7 Create a label to display the quotes
quoteLabel := widget.NewLabel("")
STEP:8 Initialize the index for the current quote
 quoteIndex := 0
STEP:9 Start a goroutine to rotate quotes every 5 seconds
go func() {
for {
// Set the text of the label to the current quote
quoteLabel.SetText(quotes[quoteIndex])
// Update the index to the next quote, wrapping around if necessary
quoteIndex = (quoteIndex + 1) % len(quotes)
// Pause for 5 seconds before showing the next quote
time.Sleep(5 * time.Second)
}
}()

STEP:10 Organize the quote label in a vertical box layout

w.SetContent(container.NewVBox(quoteLabel))
STEP:11 Set the initial size of the window
w.Resize(fyne.NewSize(300, 100))
STEP:12 Center the window on the screen
w.CenterOnScreen()
STEP:13 Show the window and run the application
w.ShowAndRun()

 

SOURCE CODE:


package main
import (
"fyne.io/fyne/v2" // Import the Fyne package for GUI development
"fyne.io/fyne/v2/app" // Import the app package to create a new application
"fyne.io/fyne/v2/container" // Import the container package for layout management
"fyne.io/fyne/v2/widget" // Import the widget package for UI components
"time" // Import the time package for handling time-related functions
)
func main() {
// Create a new application instance
a := app.New()
// Create a new window with the title "Quotes and Tips"
w := a.NewWindow("Quotes and Tips")
// Define a slice of quotes to display
quotes := []string{
"Stay positive!",
"Believe in yourself.",
"Keep going, you're doing great!",
"Every day is a new opportunity.",
"Don't watch the clock; do what it does. Keep going.",
}
// Create a label to display the quotes
quoteLabel := widget.NewLabel("")
// Initialize the index for the current quote
quoteIndex := 0
// Start a goroutine to rotate quotes every 5 seconds
go func() {
for {
// Set the text of the label to the current quote
quoteLabel.SetText(quotes[quoteIndex])
// Update the index to the next quote, wrapping around if necessary
quoteIndex = (quoteIndex + 1) % len(quotes)
// Pause for 5 seconds before showing the next quote
time.Sleep(5 * time.Second)
}
}()
// Organize the quote label in a vertical box layout
w.SetContent(container.NewVBox(quoteLabel))
// Set the initial size of the window
w.Resize(fyne.NewSize(300, 100))
// Center the window on the screen
w.CenterOnScreen()
// Show the window and run the application
w.ShowAndRun()
}

Don’t forget to like, subscribe, and hit the notification bell for more tutorials on Go and Fyne!

 

#Golang #Fyne #GUITutorial #GoProgramming #LabelWidget #QuotesApp #Programming #Coding #SoftwareDevelopment #LearnToCode

Feel free to modify any part of this to better fit your style or content!

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.