首页 > 解决方案 > 多个 kivy 弹出应用程序

问题描述

我正在尝试构建一个 python-kivy 应用程序,它可以创建弹出窗口并根据用户的输入返回结果。Kivy 应用程序应该一直运行,直到弹出窗口消失。我可以创建第一个应用程序并正确获取弹出结果,但在尝试创建第二个应用程序后出现错误。我不能在 Kivy 中创建多个(非同时)应用程序吗?

from kivy.app import App
from kivy.uix.popup import Popup
from kivy.uix.floatlayout import FloatLayout
from kivy.core.window import Window
from kivy.lang.builder import Builder

Builder.load_string("""
<PopupYesNo>:
    Label:
        id: label
        text: "Hello"
        pos_hint: {'center_x':0.5, 'center_y':0.65}

    Button:
        text: "Yes"
        size_hint: 0.3, 0.2
        pos_hint: {'center_x':0.25, 'top':0.25}
        on_release: root.dismiss('Yes')

    Button:
        text: "No"
        size_hint: 0.3, 0.2
        pos_hint: {'center_x':0.75, 'top':0.25}
        on_release: root.dismiss('No')
""")


class MyPopupApp(App):

    def __init__(self, popup_text):
        super(MyPopupApp, self).__init__()
        self.popup_text = popup_text
        self.result = ''

    def build(self):
        Window.size = 300, 200
        Window.borderless = True

        self.title = "Input"

        popup = PopupYesNo()
        popup.set_text(self.popup_text)
        popup = Popup(title='Input', size=(300, 200), size_hint=(None, None), content=popup)
        return popup

class PopupYesNo(FloatLayout):

    def set_text(self, text):
        self.ids['label'].text = text

    def dismiss(self, value):
        app = App.get_running_app()
        app.result = value
        app.stop()


if __name__ == '__main__':

    app = MyPopupApp("Are you reading this?")
    app.run()
    print('First popup result: '+ app.result)
    app = MyPopupApp("Choose yes or no")
    app.run()
    print('First popup result: '+ app.result)

我得到的错误::(

   File "C:\Python27\lib\site-packages\kivy\app.py", line 855, in run
     runTouchApp()
   File "C:\Python27\lib\site-packages\kivy\base.py", line 506, in runTouchApp
     stopTouchApp()
   File "C:\Python27\lib\site-packages\kivy\base.py", line 521, in stopTouchApp
     EventLoop.close()
   File "C:\Python27\lib\site-packages\kivy\base.py", line 172, in close
     self.stop()
   File "C:\Python27\lib\site-packages\kivy\base.py", line 184, in stop
     provider.stop()
   File "C:\Python27\lib\site-packages\kivy\input\providers\wm_pen.py", line 111, in stop
     SetWindowLong_WndProc_wrapper(self.hwnd, self.old_windProc)
   File "C:\Python27\lib\site-packages\kivy\input\providers\wm_common.py", line 122, in _closure
     oldAddr = func(hWnd, GWL_WNDPROC, cast(wndProc, c_void_p).value)
 ctypes.ArgumentError: argument 3: <type 'exceptions.TypeError'>: wrong type

标签: pythonpopupkivy

解决方案


As already said you typically only use one "App"-Class running continuously. For example:

if __name__ == "__main__":
MyApp().run()

To create a popup i would advise you to use a class which is being called by a function.

  1. create your class

    class P(FloatLayout): pass

or in your example the existing class should already be good:

class PopupYesNo(FloatLayout):

def set_text(self, text):
    self.ids['label'].text = text

def dismiss(self, value):
    app = App.get_running_app()
    app.result = value
    app.stop()
  1. create a function to call that class (i dont know why it doesnt show, but this is code as well)

    def show_popup(): show = P()

    popupWindow = Popup(title="Popup Window", content=show, size_hint=(None,None), size=(400,400)) popupWindow.open()

or in your case change the line

show = P()

to

show = PopupYesNo()

This should work since you already have Popup imported.

Now you should be able to call your popup in the position you want with

if thingshappen:
    show_popup()

I hope this was helpfull, fell free to ask me to make my answer more detailed if it suits your question.


推荐阅读