多个kivy弹出应用程序

2024-03-28 18:17:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试构建一个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

Tags: textinpyselfapplibpackagesline