fyne的对话框

对话框

import "fyne.io/fyne/v2/dialog"

dialog包 定义了应用程序GUI的标准对话框窗口。

NewError

NewError()为应用程序错误在指定的窗口上创建一个对话框。该消息是从提供的错误中提取的(不应为nil)。创建后,您应该调用Show()。

func NewError(err error, parent fyne.Window) Dialog {
    return createTextDialog("Error", err.Error(), theme.ErrorIcon(), parent)
}

ShowError

func ShowError(err error, parent fyne.Window) {
    NewError(err, parent).Show()
}

ShowError调用的是NewError。

示例代码:

package main

import (
	"fmt"
	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/dialog"
)

func main() {
	myApp := app.New()
	myWindow := myApp.NewWindow("VPN")
	err := fmt.Errorf("can't generate a new token.")
	dialog.NewError(err, myWindow).Show()
	myWindow.Resize(fyne.NewSize(400, 300))
	myWindow.Show()
	myApp.Run()
}

效果图:

在这里插入图片描述

NewInformation

NewInformation在指定的窗口上为用户信息创建一个对话框。标题用于对话框窗口,消息是内容。创建后,您应该调用Show()。

func NewInformation(title, message string, parent fyne.Window) Dialog {
    return createTextDialog(title, message, theme.InfoIcon(), parent)
}

ShowInformation

ShowInformation在指定的窗口上显示一个对话框以获取用户信息。标题用于对话框窗口,消息是内容。

func ShowInformation(title, message string, parent fyne.Window) {
    NewInformation(title, message, parent).Show()
}

ShowInformation调用了NewInformation。

示例代码:

package main

import (
	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/dialog"
)

func main() {
	myApp := app.New()
	myWindow := myApp.NewWindow("VPN")
	title := "address"
	msg := "show information"
	dialog.NewInformation(title, msg, myWindow).Show()
	myWindow.Resize(fyne.NewSize(400, 300))
	myWindow.Show()
	myApp.Run()
}

效果图:

在这里插入图片描述

### Fyne Go Framework Dialog Component Usage and Examples In the context of developing applications using the Fyne framework, dialogs serve as essential components for interacting with users by presenting information or requesting input. The `dialog` package within Fyne provides various types of dialog boxes that can be utilized to enhance user interaction. #### Creating a Simple Message Dialog A message dialog is one of the most straightforward ways to display messages to users. This type of dialog does not require any specific callback function but allows adding buttons like "OK". ```go package main import ( "fyne.io/fyne/v2" "fyne.io/fyne/v2/app" "fyne.io/fyne/v2/container" "fyne.io/fyne/v2/widget" ) func showMessageDialog(w fyne.Window) { dialog.ShowInformation("Info", "This is an informational message.", w) } func main() { myApp := app.New() w := myApp.NewWindow("Message Dialog Example") button := widget.NewButton("Show Info Dialog", func() { showMessageDialog(w) }) w.SetContent(container.NewVBox(button)) w.Resize(fyne.Size{Width: 200, Height: 150}) w.ShowAndRun() } ``` #### Opening Files Using File Open Dialogs For scenarios where file selection from the filesystem is necessary, such as loading configuration files into your application, utilizing the `FileOpen` method proves beneficial. It returns data through its parameter `fyne.URIReadCloser`, which contains both URI reference and content stream[^1]. ```go package main import ( "io/ioutil" "fyne.io/fyne/v2" "fyne.io/fyne/v2/app" "fyne.io/fyne/v2/dialog" "fyne.io/fyne/v2/storage" "fyne.io/fyne/v2/widget" ) func openFile(w fyne.Window) { dialog.FileOpen(func(r fyne.URIReadCloser, err error) { if r != nil { defer r.Close() data, _ := ioutil.ReadAll(r) widget.ShowAlertDialog("File Content", string(data), w) } else if err != nil { widget.ShowError(err, w) } }, storage.NewFileFilter([]string{"*.txt"}))(w) } func main() { a := app.New() w := a.NewWindow("File Open Example") openBtn := widget.NewButton("Select Text File", func() { openFile(w) }) w.SetContent(openBtn) w.Resize(fyne.Size{Width: 300, Height: 180}) w.ShowAndRun() } ``` #### Handling User Input via Entry Widgets Inside Custom Dialogs When more complex interactions are required, creating custom dialogs becomes useful. For instance, when asking users to enter text before proceeding further in the program flow: ```go package main import ( "fyne.io/fyne/v2" "fyne.io/fyne/v2/app" "fyne.io/fyne/v2/container" "fyne.io/fyne/v2/widget" ) func showInputDialog(w fyne.Window) { entry := widget.NewEntry() dlg := dialog.NewCustom("Input Required", "Submit", container.NewVBox(entry), func() { textValue := entry.Text widget.ShowAlert("Your Input:", textValue, w) }, w, ) dlg.Resize(fyne.NewSize(300, 100)) dlg.Show() } func main() { appInstance := app.New() window := appInstance.NewWindow("Input Dialog Demo") inputButton := widget.NewButton("Enter Some Text", func() { showInputDialog(window) }) window.SetContent(inputButton) window.Resize(fyne.Size{Width: 250, Height: 150}) window.ShowAndRun() } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

shulu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值