2017-04-12 19 views
0

我構建了一個python桌面webapp與一個相當簡單的燒瓶後端,目前我只通過Chrome服務。我試圖用pywebview打包它,但我遇到了一個困難的路障。Pywebview塊燒瓶應用程序,除非我打開兩個網頁瀏覽

我的代碼看起來像這樣在我的包的主文件:

import os 
import sys 
from flasher import app 

import httplib 

import webview 
import threading 


def start_prod_server(): 
    runserver(debug=False, reloader=False) 

def start_gui(): 
    webview.create_window("MyAppName", "http://localhost:5000", height=1000) 

def runserver(debug=True, reloader=False): 
    port = int(os.environ.get('PORT', 5000)) 
    url = "http://localhost:{}".format(port) 

    print("ready!") 
    app.run(host='::', port=port, debug=debug, use_reloader=reloader) 


if __name__ == '__main__': 
    t = threading.Thread(target=start_prod_server) 
    t.daemon = True 
    t.start() 

    # This never works 
    threading.Timer(1.5, start_gui).start() 

    sys.exit() 

這幾乎失敗系統,窗口顯示空白和後端沒有任何東西迴應(包括從請求單獨的桌面瀏覽器)。

我無意中發現,如果我啓動兩個線程的WebView,它始終工作:

if __name__ == '__main__': 
    t = threading.Thread(target=start_prod_server) 
    t.daemon = True 
    t.start() 

    # This works every time 
    threading.Timer(1.5, start_gui).start() 
    threading.Timer(1.5, start_gui).start() 

    sys.exit() 

可能是什麼造成的?我對Python中線程的理解是相當有限的,所以我不確定在哪裏尋找。這可能是pywebview中的錯誤,或者我的線程做錯了什麼?

我也開放給webview部分的替代方案的建議,但我想保留python/flask部分,因爲應用程序已經正常工作。

回答

0

那麼,經過一番挖掘,我想我自己找到了解決方案。

不管什麼原因,webview.create_window()沒有導航到頁面並卡住。打開第二個窗口導致兩個窗口都導航到該URL並允許應用程序繼續。 (我還沒有明白爲什麼)

的問題僅僅是解決了通過添加webview.load_url("http://localhost:5000")像這樣:

if __name__ == '__main__': 
    t = threading.Thread(target=start_prod_server) 
    t.daemon = True 
    t.start() 

    threading.Timer(1.5, start_gui).start() 
    webview.load_url("http://localhost:5000") 

    sys.exit() 

任何瞭解,爲什麼出現這種情況仍然歡迎,但...在源pywebview ,這兩種方法對self.browser.web_browser.Navigate(url)

進行完全相同的呼叫
相關問題