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.
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() {}
a := app.New()
w := a.NewWindow("Quotes and Tips")
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.", }
quoteLabel := widget.NewLabel("")
quoteIndex := 0
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))
w.Resize(fyne.NewSize(300, 100))
w.CenterOnScreen()
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!