2017-06-23 91 views
0

我是新來的Python + Tkinter的將數據插入到For循環文本組件,Tkinter的python3

我試圖用FOR循環

的問題是值輸入到一個文本組件,文本組件不在For循環執行期間顯示任何內容當for循環結束時,它顯示所有值。

如何在for循環中顯示插入的值。

見的代碼

for item in liList: 
     listDict = {} 
     # get a tag href 
     listATag = item.find_all("a", attrs={"class": "product-image"})[0] 
     listATagHref = listATag['href'] 
     listDict["purchaseLink"] = listATagHref 

     imgPageRequest = requests.get(listATagHref) 
     imgPageData = imgPageRequest.text 
     imgPageSoup = BeautifulSoup(imgPageData, 'lxml') 
     try: 
      productImgDiv = imgPageSoup.find_all('div', attrs={"class": "product-image"})[0] 
      imgATag = productImgDiv.find_all('a')[0]['href'] 
      largeThumbFileName = (imgATag.split('/')[-1]) 
      tempImgNameList.append(largeThumbFileName) 
      print(listATagHref) 
      textBox.insert(END,listATagHref+'\n') 
      etc... 

回答

1

你需要調用的小部件update要添加新的數據,因爲它的刷新,每次迭代後顯示的新值的最後一行。這是因爲Tk mainloop通常會捕獲窗口小部件上的新信息並更新它,但是當您處於這樣的循環中時,它將無法檢查,直到循環結束。

如果您定義爲rootTk(),那麼您可以致電root.update(),或者根據實際的小部件本身。這將發生在您的for循環的末尾。