0

I have a Kivy application.

From the main GUI, I want to open a new message box and force the main GUI to await the result of an action box interaction.

I saw that Qt4 message box has support for this blocking call type, but I haven't found the equivalent functionality in Kivy. Does such a feature exist?

Jay Kominek
  • 8,674
  • 1
  • 34
  • 51
Thelinh Truong
  • 564
  • 1
  • 8
  • 19

2 Answers2

2

The Popup widget is used to create modal popups. By default, the popup will cover the whole “parent” window. When you are creating a popup, you must at a minimum set a Popup.title and a Popup.content widget.

modal means blocking :)

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

Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
  • what i mean is: after open pop up. I want main flow capture user actions on popup. For example: popup has 3 buttons: button A, button B and Button C. I want main flow waiting for which button click on popup. this is the same process for alert function in javascript. Please help me. Thanks. – Thelinh Truong Apr 25 '13 at 04:03
  • I was requested to not use callback function to notify change from popup messagebox to main gui. – Thelinh Truong Apr 25 '13 at 04:09
  • http://kivy.org/docs/api-kivy.uix.modalview.html#kivy.uix.modalview.ModalView may be what you want ... a modal view will block until it is told to close ... or maybe i dont understand what you are asking for .... – Joran Beasley Apr 25 '13 at 04:14
0

Here is a code snippet which does the job though it is actually not really blocking. You need to define one or two alternatives to jump to in order to continue working with the program. Thats the pseudo-blocking trick.

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()