I've been trying to figure out what is causing this error... Might just be me, but maybe you can see what i can't. Hopefully you can give me a helping hand.
Whenever I run my code it gives this error
Fyne error: Attempt to access current Fyne app when none is started
go/pkg/mod/fyne.io/fyne/[email protected]/app.go:92
Any idea? Here is the code. and It runs*
// GoMark project main.go
// Version 1.3
package main
import (
"fmt"
"image/color"
"log"
"os"
"runtime"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
)
var text = widget.NewMultiLineEntry() //Text entry
var textTitle = widget.NewLabel("")
var preview = widget.NewRichTextFromMarkdown("") //Preview side
var myWindow fyne.Window
var down_dir = ""
var filePath = "" // THIS WILL NOT EDIT PRESET!!! Look further down...
var savename = ""
var savenametxt = ""
var myApp = app.New()
func main() {
myWindow = myApp.NewWindow("GoMark 1.3")
// - - - Text editor / Preview - - -
textTitle.SetText("UNSAVED DOCUMENT")
left := container.NewBorder(nil, nil, nil, nil, text)
middle := container.NewHSplit(left, preview)
text.OnChanged = func(string) { //When someting is written or canged in the text entry, update with this function
preview.ParseMarkdown(text.Text)
}
// - - - END - - -
// - - - TOP SECTION - - -
//Line creation
line := canvas.NewLine(color.Black)
line.StrokeWidth = 4
savename := widget.NewEntry()
savename.SetPlaceHolder("ABC123")
//- - - Settings toolbar - - -
settings := widget.NewToolbar(
widget.NewToolbarAction(theme.ContentClearIcon(), func() {
dialog.ShowConfirm("Do you want to clear document?", "", callback, myWindow)
}),
widget.NewToolbarSeparator(),
widget.NewToolbarAction(theme.FolderNewIcon(), func() {
log.Println("Save as Doc")
dialog.ShowFileSave(DownloadDirectory, myWindow)
}),
widget.NewToolbarAction(theme.DocumentSaveIcon(), func() {
log.Println("Save Doc")
form_savename := widget.NewFormItem("", savename)
form := dialog.NewForm("Save Name to S:", "Save", "Cancel", []*widget.FormItem{form_savename}, func(b bool) {
if b == true {
fmt.Println("Save selected")
savenametxt = savename.Text
SaveToS(myWindow)
savename.SetText(savenametxt)
}
}, myWindow)
form.Resize(fyne.NewSize(400, 200))
form.Show()
}),
)
//Combiner
top := container.NewBorder(nil, line, nil, settings, textTitle)
// - - - END - - -
//Run the main fyne window
main := container.NewBorder(top, nil, nil, nil, middle)
myWindow.Resize(fyne.NewSize(1200, 800))
myWindow.SetContent(main)
myWindow.ShowAndRun()
}
//Clear text confirmation
func callback(yes bool) {
fmt.Println("Running Callback")
if yes == true {
fmt.Println("Text CLEAR")
clean()
} else {
fmt.Println("Text STAY")
}
}
//Clear text box and preview
func clean() {
text.Text = ""
preview.ParseMarkdown(text.Text)
text.Refresh()
textTitle.Text = "UNSAVED DOCUMENT"
textTitle.Refresh()
dialog.ShowInformation("Text has been cleared", "", myWindow)
}
//Choose a Download folder
func DownloadDirectory(savedir fyne.URIWriteCloser, err error) {
fmt.Println("Dialog")
fmt.Println(savedir.URI())
}
//- - - Save to S: - - -
func SaveToS(myWindow fyne.Window) {
if runtime.GOOS == "Windows" {
filePath = "S:\\TMP\\DocMove" //EDIT DIR PRESET FOR WINDOWS
filePath = filePath "\\"
} else {
filePath = "/mnt/s/TMP/DocMove" // EDIT DIR PRESET FOR LINUX
filePath = filePath "/"
}
fmt.Println(runtime.GOOS)
file, errC := os.Create(filePath savenametxt ".md")
if errC != nil {
dialog.ShowError(errC, myWindow)
fmt.Println(errC)
return
} else {
_, errWriting := file.Write([]byte(text.Text))
if errWriting != nil {
dialog.ShowError(errWriting, myWindow)
fmt.Println(errWriting)
return
} else {
textTitle.SetText(savenametxt)
dialog.ShowInformation("Sucess", savenametxt " Has been saved", myWindow)
fmt.Println(filePath savenametxt ".md")
}
}
}
CodePudding user response:
You are trying to create widgets before the fyne app is initiated.
This order gave me no errors:
var down_dir = ""
var filePath = "" // THIS WILL NOT EDIT PRESET!!! Look further down...
var savename = ""
var savenametxt = ""
var myApp = app.New()
var text = widget.NewMultiLineEntry() //Text entry
var textTitle = widget.NewLabel("")
var preview = widget.NewRichTextFromMarkdown("") //Preview side
var myWindow fyne.Window