go fyne v2.4 ShowPopUp vs ShowModalPopUp不阻止与底层元素的交互或覆盖半透明覆盖,在Win11中

olqngx59  于 2023-11-14  发布在  Go
关注(0)|答案(2)|浏览(178)

我希望弹出一个模态,这将不会让用户与应用程序交互,直到模态上的问题得到回答。通常背景是灰色的,因为我习惯于在C#表单。
我写了下面的代码试验ShowModalPopUp和ShowPopUp,两者都产生弹出窗口。但是widget.ShowModalPopUp产生一个普通的弹出窗口,而不是一个模态弹出窗口,它保持焦点并使背景变灰。Google search =“modal popup form”来看看我期望的是什么。

package main

import (
    "image/color"

    "fyne.io/fyne/v2"
    "fyne.io/fyne/v2/app"
    "fyne.io/fyne/v2/canvas"
    "fyne.io/fyne/v2/container"
    "fyne.io/fyne/v2/widget"
)

func main() {
    a := app.New()
    w := a.NewWindow("Pop up demo")
    w.Resize(fyne.NewSize(600, 600))

    //-------------------------------
    //func ShowModalPopUp(content fyne.CanvasObject, canvas fyne.Canvas)
    //func ShowPopUp(content fyne.CanvasObject, canvas fyne.Canvas)
    //https://developer.fyne.io/api/v2.4/widget/

    hello := widget.NewLabel("Demo Fyne show popup vs show modal popup")
    w.SetContent(container.NewVBox(
        hello,
        widget.NewButton("pop demo!", func() {
            hello.SetText("Show me a popup")
            myWindowPop := a.NewWindow("CanvasPop")
            myCanvasPop := myWindowPop.Canvas()
            red := color.NRGBA{R: 180, G: 0, B: 0, A: 255}
            rectPop := canvas.NewRectangle(&red)
            rectPop.SetMinSize(fyne.NewSize(200, 200))
            myCanvasPop.SetContent(rectPop)
            widget.ShowPopUp(rectPop, myCanvasPop)         // show popup
            myWindowPop.Resize(fyne.NewSize(300, 300))
            myWindowPop.Show()

        }),
        widget.NewButton("Modal demo", func() {
            hello.SetText("show me a ModalPopup")
            myWindow := a.NewWindow("CanvasModal")
            myCanvas := myWindow.Canvas()
            blue := color.NRGBA{R: 0, G: 0, B: 180, A: 255}
            rect := canvas.NewRectangle(&blue)
            rect.SetMinSize(fyne.NewSize(200, 200))
            myCanvas.SetContent(rect)
            widget.ShowModalPopUp(rect, myCanvas)        // show modal popup
            myWindow.Resize(fyne.NewSize(300, 300))
            myWindow.Show()
        }),
    ))

    w.ShowAndRun()
}

字符串

omvjsjqw

omvjsjqw1#

模态弹出窗口应该像你描述的那样工作。你可能对提供标准模态对话框的对话框包感兴趣。如果模态显示不工作,那么对话框也不工作?
我怀疑这个问题是因为你为模态创建了新的窗口,但是要成为一个模态,它必须和你想要它覆盖的内容在同一个窗口上。

vdzxcuhz

vdzxcuhz2#

经过很多doo最后我有模态弹出工作,也感谢Andy.xyz.我希望有更多的小代码的例子在fyne文档.我张贴我的工作解决方案在这里任何人评论和/或作出改进.与感谢.
代码有一个主窗口和一个按钮,可以打开一个带有关闭模式按钮的模式。

package main
    import (
        "image/color"
        "fyne.io/fyne/v2/app"
        "fyne.io/fyne/v2/canvas"
        "fyne.io/fyne/v2/container"
        "fyne.io/fyne/v2/widget"
    )
    func main() {
        // Create a new Fyne application
        a := app.New()
    
        // Create a new window with a title
        w := a.NewWindow("Pop up demo by DaHerb")
    
        // Create a label widget with a message
        hello := widget.NewLabel("Demo Fyne show popup vs show modal popup")
    
        // Create a button widget to trigger the pop-up
        showPopUpButton := widget.NewButton("Show Pop-up", nil)
    
        // Define content and canvas objects for the pop-up
        content := canvas.NewText("This is the content of the pop-up", 
        color.Black)
    
        // Declare a variable to hold the pop-up widget
        var popup *widget.PopUp
    
        // Define an action when the "Show Pop-up" button is tapped
        showPopUpButton.OnTapped = func() {
            // Create a container for the pop-up content, including a "Close" 
       button
            popUpContent := container.NewVBox(
                content,
                widget.NewButton("Close", func() {
                    popup.Hide() // Function to hide the pop-up
                }),
            )
    
            // Create a modal pop-up using the content and the main window's 
     canvas
            popup = widget.NewModalPopUp(popUpContent, w.Canvas())
    
            // Show the modal pop-up
            popup.Show()
        }
    
        // Set the main window's content to include the label and the Show-Pop- 
         up button
        w.SetContent(container.NewVBox(
            hello,
            showPopUpButton,
        ))
        // Show the main window and run the application
        w.ShowAndRun()
    }

字符串

相关问题