【问题标题】:is there anyway to create a confirmation popup like this无论如何要创建这样的确认弹出窗口
【发布时间】:2021-06-03 13:29:33
【问题描述】:

我正在尝试制作一个弹出窗口,该弹出窗口可以将 true 或 false 返回到按下按钮时调用它的位置

```python
import kivy


def confirmation_popup(title_text):
    btn1 = kivy.uix.button.Button(text="Yes")
    btn2 = kivy.uix.button.Button(text="no")

    btn1.bind(on_release=return True)#to the function itself
    btn2.bind(on_release=return False)

    Boxed_layout= kivy.uix.boxlayout.BoxLayout(orientation = "horizontal")
    Boxed_layout.add_widget(btn1)
    Boxed_layout.add_widget(btn2)

    pop = kivy.uix.popup.Popup(title=title_text,content=Boxed_layout)

    pop.open()

if confirmation_popup("are you sure you want to delete your user"):
    #delete user from database```

【问题讨论】:

    标签: python user-interface kivy


    【解决方案1】:

    与其试图从Popup 获得回报,不如让Popup 调用一个执行确认操作的方法。如果你像这样定义你的confirmation_popup

    def confirmation_popup(title_text, doit):
        btn1 = Button(text="Yes")
        btn2 = Button(text="no")
        Boxed_layout= BoxLayout(orientation = "horizontal")
        Boxed_layout.add_widget(btn1)
        Boxed_layout.add_widget(btn2)
    
        pop = Popup(title=title_text,content=Boxed_layout)
    
        btn1.bind(on_release=partial(doit, pop)) # bind to whatever action is being confiirmed
        btn2.bind(on_release=pop.dismiss)
    
        pop.open()
    
    # a method that does the action being confirmed
    def doit(popup, button):
        popup.dismiss()
        print('do whatever action is confirmed')
    

    然后您可以通过以下方式使用它:

    confirmation_popup('This is a Test', doit)
    

    【讨论】:

      猜你喜欢
      • 2012-07-17
      • 2018-02-10
      • 2023-04-03
      • 1970-01-01
      • 2012-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-28
      相关资源
      最近更新 更多