問題描述
有誰知道為什么 Python 的 2.7 Spyder 只成功初始化 'Hello World' Kivy 應用程序一次,即按 F5 會帶來窗口應用程序,但是當我關閉它并再次按 F5 時,它會顯示以下錯誤:
Does anyone know why Python's 2.7 Spyder is successfully initializing the 'Hello World' Kivy app just once, i.e. hitting F5 brings the window app, but when I close it and hit F5 again, it says the following error:
[INFO ] [Base ] Start application main loop
[ERROR ] [Base ] No event listeners have been created
[ERROR ] [Base ] Application will leave
但是,通過 Anacondas 命令提示符初始化時沒有錯誤.
However, there is no error when initialized through Anacondas Command Prompt.
這是代碼(與網站相同):
Here's the code (same as website):
from kivy.app import App
from kivy.uix.button import Button
class TestApp(App):
def build(self):
return Button(text='Hello World')
TestApp().run()
if __name__ == '__main__':
TestApp().run()
推薦答案
其實這個示例程序只是一個最小的結構,讓你嘗試如何以如此簡單的方式創建交互式 UI.
Actually the sample program is just a minimum structure for you to try out how the interactive UI can be created in such a simple way.
而在TestApp
中,它實際上并沒有實現event listeners
來處理close事件.當您創建實際項目時,您應該始終注意這一點.實際上,如果您仔細查看 logging
,您會注意到錯誤已經在您關閉 TestApp
時發生,而不是在您重新啟動"您 TestApp
:
And the in TestApp
, it actually didn't implment the event listerners
to handle the close event. And when you create your actual project, you should always take care of that. Acually if you look at the logging
carefully, you would notice that the error happens already when you close the TestApp
, not when you "re-start" you TestApp
:
[INFO ] [Base ] Leaving application in progress...
INFO:kivy:[Base ] Leaving application in progress...
[INFO ] [Base ] Start application main loop
INFO:kivy:[Base ] Start application main loop
[ERROR ] [Base ] No event listeners have been created
ERROR:kivy:[Base ] No event listeners have been created
[ERROR ] [Base ] Application will leave
ERROR:kivy:[Base ] Application will leave
因此,對于您的情況,一個簡單的解決方法是在 Console
面板中轉到 Run->Configure
,而不是選擇 Execute in current Python or IPython console
,你只需選擇第二個選項,即Execute in a new dedicated Python console
.在這種情況下,當您完成代碼時,Python 將關閉當前內核.并且每當你運行你的代碼時,Spyder
會自動為這個特定的腳本創建一個新的專用內核.
So for your case, the one simple work-around is that you go to Run->Configure
, in the Console
panel, instead of you choose to Execute in current Python or IPython console
, you just choose the second option, which is Execute in a new dedicated Python console
. In this case, where time your finished the code, the Python will close the current kernel. And whenever you run your code, Spyder
will automatically create a new dedicated kernel for this particular script.
這篇關于Python Spyder 初始化 Hello World Kivi 應用程序一次?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!