Fyne错误:尝试在未启动应用程序时访问当前的Fyne应用程序。

huangapple go评论100阅读模式
英文:

Fyne error: Attempt to access current Fyne app when none is started

问题

我一直在努力找出导致这个错误的原因...可能只是我自己的问题,但也许你可以看出我看不到的地方。希望你能给我帮助。

每当我运行我的代码时,就会出现以下错误:

Fyne error: 尝试访问当前未启动的 Fyne 应用

go/pkg/mod/fyne.io/fyne/v2@v2.2.3/app.go:92

有什么想法吗?这是代码。而且它可以运行*。

// GoMark 项目 main.go
// 版本 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() // 文本输入框
var textTitle = widget.NewLabel("")
var preview = widget.NewRichTextFromMarkdown("") // 预览区域
var myWindow fyne.Window
var down_dir = ""
var filePath = "" // 这部分不会编辑预设!请往下看...
var savename = ""
var savenametxt = ""
var myApp = app.New()

func main() {
	myWindow = myApp.NewWindow("GoMark 1.3")
	// - - - 文本编辑器 / 预览 - - -

	textTitle.SetText("未保存的文档")
	left := container.NewBorder(nil, nil, nil, nil, text)
	middle := container.NewHSplit(left, preview)
	text.OnChanged = func(string) { // 当文本输入框中有内容被写入或更改时,使用此函数更新预览区域
		preview.ParseMarkdown(text.Text)
	}
	// - - - 结束 - - -

	// - - - 顶部部分 - - -
	// 创建线条
	line := canvas.NewLine(color.Black)
	line.StrokeWidth = 4
	savename := widget.NewEntry()
	savename.SetPlaceHolder("ABC123")

	//- - - 设置工具栏 - - -
	settings := widget.NewToolbar(
		widget.NewToolbarAction(theme.ContentClearIcon(), func() {
			dialog.ShowConfirm("是否清空文档?", "", callback, myWindow)
		}),
		widget.NewToolbarSeparator(),
		widget.NewToolbarAction(theme.FolderNewIcon(), func() {
			log.Println("另存为文档")
			dialog.ShowFileSave(DownloadDirectory, myWindow)
		}),
		widget.NewToolbarAction(theme.DocumentSaveIcon(), func() {
			log.Println("保存文档")

			form_savename := widget.NewFormItem("", savename)
			form := dialog.NewForm("保存名称到 S:", "保存", "取消", []*widget.FormItem{form_savename}, func(b bool) {
				if b == true {
					fmt.Println("选择保存")
					savenametxt = savename.Text
					SaveToS(myWindow)
					savename.SetText(savenametxt)
				}
			}, myWindow)
			form.Resize(fyne.NewSize(400, 200))
			form.Show()
		}),
	)

	// 组合
	top := container.NewBorder(nil, line, nil, settings, textTitle)
	// - - - 结束 - - -

	// 运行主要的 fyne 窗口
	main := container.NewBorder(top, nil, nil, nil, middle)
	myWindow.Resize(fyne.NewSize(1200, 800))
	myWindow.SetContent(main)
	myWindow.ShowAndRun()
}

// 清空文本确认
func callback(yes bool) {
	fmt.Println("运行回调函数")
	if yes == true {
		fmt.Println("清空文本")
		clean()
	} else {
		fmt.Println("文本保留")
	}
}

// 清空文本框和预览区域
func clean() {
	text.Text = ""
	preview.ParseMarkdown(text.Text)
	text.Refresh()
	textTitle.Text = "未保存的文档"
	textTitle.Refresh()
	dialog.ShowInformation("文本已清空", "", myWindow)
}

// 选择下载文件夹
func DownloadDirectory(savedir fyne.URIWriteCloser, err error) {
	fmt.Println("对话框")
	fmt.Println(savedir.URI())
}

//- - - 保存到 S: - - -
func SaveToS(myWindow fyne.Window) {
	if runtime.GOOS == "Windows" {
		filePath = "S:\\TMP\\DocMove" // 编辑 Windows 的预设目录
		filePath = filePath + "\\"
	} else {
		filePath = "/mnt/s/TMP/DocMove" // 编辑 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("成功", savenametxt+" 已保存", myWindow)
			fmt.Println(filePath + savenametxt + ".md")
		}
	}
}


英文:

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/v2@v2.2.3/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")
		}
	}
}


答案1

得分: 1

你正在尝试在初始化fyne应用程序之前创建小部件。

这个命令没有给我任何错误:

var down_dir = ""
var filePath = "" // 这不会编辑预设!请往下看...
var savename = ""
var savenametxt = ""
var myApp = app.New()
var text = widget.NewMultiLineEntry() // 文本输入
var textTitle = widget.NewLabel("")
var preview = widget.NewRichTextFromMarkdown("") // 预览侧边栏
var myWindow fyne.Window
英文:

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

huangapple
  • 本文由 发表于 2022年9月5日 16:17:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/73606435.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定