如何在kivy中使用阻止消息框

时间:2013-04-25 03:26:06

标签: python qt4 kivy blocking

我有一个Kivy应用程序。

在主GUI中,我想打开一个新的消息框并强制主GUI等待动作框交互的结果。

我看到Qt4消息框支持这种阻塞调用类型,但我还没有找到Kivy中的等效功能。这样的功能是否存在?

2 个答案:

答案 0 :(得分:2)

弹出窗口小部件用于创建模态弹出窗口。默认情况下,弹出窗口将覆盖整个“父”窗口。在创建弹出窗口时,您必须至少设置一个Popup.title和一个Popup.content小部件。

模态意味着阻止:)

http://kivy.org/docs/api-kivy.uix.popup.html

答案 1 :(得分:0)

这是一个完成工作的代码片段,虽然它实际上并没有真正阻塞。 您需要定义一个或两个备选项以跳转到以继续使用 该程序。那是伪拦截技巧。

import kivy
from kivy.uix.popup import Popup
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.app import App

class MessageBoxApp(App):

    def build(self):
        return Button(text='Press for MessageBox!', on_press=self.callpopup)

    def callpopup(self, event):
        dlg = MessageBox(self, titleheader="Title Header", message="Any Message", 
        options={"YES": "printyes()", "NO": "printno()", "CANCEL": ""})
        print "Messagebox shows as kivy popup and we wait for the user action"

    def printyes(self):
        # routine for going yes
        print "You chose the Yes routine"

    def printno(self):
        # routine for going no
        print "You chose the No routine"

class MessageBox(MessageBoxApp):
    def __init__(self, parent, titleheader="Title", message="Message", options={"OK": ""}, size=(400, 400)):

        def popup_callback(instance):
            "callback for button press"
            self.retvalue = instance.text
            self.popup.dismiss()

        self.parent = parent
        self.retvalue = None
        self.titleheader = titleheader
        self.message = message
        self.options = options
        self.size = size
        box = GridLayout(orientation='vertical', cols=1)
        box.add_widget(Label(text=self.message, font_size=16))
        b_list =  []
        buttonbox = BoxLayout(orientation='horizontal')
        for b in self.options:
            b_list.append(Button(text=b, size_hint=(1,.35), font_size=20))
            b_list[-1].bind(on_press=popup_callback)
            buttonbox.add_widget(b_list[-1])
        box.add_widget(buttonbox)
        self.popup = Popup(title=titleheader, content=box, size_hint=(None, None), size=self.size)
        self.popup.open()
        self.popup.bind(on_dismiss=self.OnClose)

    def OnClose(self, event):
        self.popup.unbind(on_dismiss=self.OnClose)
        self.popup.dismiss()
        if self.retvalue != None and self.options[self.retvalue] != "":
            command = "self.parent."+self.options[self.retvalue]
            exec command

if __name__ == '__main__':
    MessageBoxApp().run()