2016-11-14 4 views
0

定数Tkinterのウィンドウの更新

このアプリの問題は、「更新」ループは3秒ごとに油価を表示するだけなので、このループは絶えず実行されていることがわかりますが、ウィンドウ内のテキストを更新するだけでなく、それはシェルが油の価格をプリントしている間です。

私はマルチプロセッシングモジュールを使用しようとしましたが、それは何の違いが行われていません。

def window(): 
    root = Tk() 
    screen_width = root.winfo_screenwidth() 
    screen_height = root.winfo_screenheight() 
    mylabel = Label(root, text = "") 
    mylabel.pack() 

def update(): 
    while True: 
     global string_price 
     request = requests.get("http://www.biznesradar.pl/notowania/BRENT-OIL-ROPA-BRENT#1d_lin_lin") 
     content = request.content 
     soup = BeautifulSoup(content, "html.parser") 
     element = soup.find("span", { "class": "q_ch_act" }) 
     string_price = (element.text.strip()) 
     print(string_price) 
     mylabel.configure(text = str(string_price)) 

     time.sleep(3) 

root.after(400, update) 
mainloop() 

答えて

2

.after方法は、すでにあなたが同時にwhile Truesleepから望んありません。 sleepwhileの両方を削除し、継続的に呼び出すために別のafter内部を追加します。

def custom_update(): 
    global string_price 
    request = requests.get("http://www.biznesradar.pl/notowania/BRENT-OIL-ROPA-BRENT#1d_lin_lin") 
    content = request.content 
    soup = BeautifulSoup(content, "html.parser") 
    element = soup.find("span", {"class": "q_ch_act"}) 
    string_price = (element.text.strip()) 
    print(string_price) 
    mylabel.configure(text=str(string_price)) 
    root.after(3000, custom_update) #notice it calls itself after every 3 seconds 

custom_update() #since you want to check right after opening no need to call after here as Bryan commented 
+0

ありがとう!それは多くの助けになりました!アプリは右の開封後は原油価格をチェックするので、私はまた、「0」のためにあなたのコードの最下部に「root.after」で時間の値を変更しました。 –

+0

あなたはそれを呼び出す初めてafter' '使用する必要はありません。あなたは 'update()'を直接呼び出すことができます。しかし、おそらく関数には別の名前を付けるべきです。すべてのウィジェットは 'update'メソッドを持っていますので、独自の' update'メソッドを持つと混乱するかもしれません。 –

+0

@KarolMularski編集後にもご確認ください。ブライアンの提案に従っていくつかの変更が適用されました。 – Lafexlos

関連する問題