0

我有一个 Kivy 应用程序。

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

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

4

2 回答 2

2

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

模态意味着阻塞:)

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

于 2013-04-25T03:34:00.330 回答
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()
于 2014-11-12T00:03:20.423 回答