前提・実現したいこと
Pythonでニュースを表示するアプリを作っています。
スレッド処理で回しているためStringVarにニュースを書き込んでいるのですが、
下記ソースコードのfor文の部分をもう少し簡略化したいです。
配列等使用できるのでしょうか?できるなら方法教えていただきたいです。
該当のソースコード
Python3
1from tkinter import * 2import urllib.request 3from bs4 import BeautifulSoup 4import threading 5 6NewsURL = "https://news.yahoo.co.jp/pickup/rss.xml" 7def get_news(): 8 response = urllib.request.urlopen(NewsURL) 9 html = BeautifulSoup(response, 'html.parser') 10 topics = html.find_all("title") 11 link = html.find_all("link") 12 i = 0 13 for item in topics: 14 if i > 5: 15 break 16 if i == 1: 17 news1.set(" {0}".format(item.string)) 18 if i == 2: 19 news2.set(" {0}".format(item.string)) 20 if i == 3: 21 news3.set(" {0}".format(item.string)) 22 if i == 4: 23 news4.set(" {0}".format(item.string)) 24 if i == 5: 25 news5.set(" {0}".format(item.string)) 26 if i == 0: 27 news_title.set(item.string) 28 i+=1 29 time.sleep(3600) 30 31flg = threading.Event() 32flg.set() 33root = Tk() 34root.attributes("-fullscreen", True) 35 36 37news_title = StringVar() 38news_title.set("") 39news1 = StringVar() 40news1.set("取得中") 41news2 = StringVar() 42news2.set("取得中") 43news3 = StringVar() 44news3.set("取得中") 45news4 = StringVar() 46news4.set("取得中") 47news5 = StringVar() 48news5.set("取得中") 49Label(root,textvariable = news_title).pack() 50Label(root,textvariable = news1).pack() 51Label(root,textvariable = news2).pack() 52Label(root,textvariable = news3).pack() 53Label(root,textvariable = news4).pack() 54Label(root,textvariable = news5).pack() 55 56t2 = threading.Thread(target = get_news) 57t2.start() 58 59root.mainloop()
補足情報(FW/ツールのバージョンなど)
Python3.6.8
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/12/08 16:00