2016-11-09 103 views
0

我需要一些小工具來更新我的kivy屏幕,隨着線程thread.eveything與部件的異常完美的作品在屏幕上進行更新屏幕在kivy拒絕更新

<ViewScreen>: 
    id:grid 
    pic:pic 
    cols:2 
    spinner:spinner 

    MDSpinner: 
     active:False 

class ViewScreen(Screen): 
    grid = ObjectProperty(None) 
    pic = ObjectProperty(None) 
    spinner = ObjectProperty(None) 


    def load_real(self): 
     _thread.start_new_thread(self.list_files, ('name',)) 

    def list_files(self, name): 
     time.sleep(1) 
     self.ids.spinner.active = True 
     self.ids.grid.clear_widgets() 
     url = 'http://po.com/{}'.format(str(self.code_url[0])) 

     r = requests.get(url) 
     data = r.text 
     soup = BeautifulSoup(data) 
     for link in soup.find_all('a'): 
      if link.get('href').endswith('.png'): 
       print(link.get('href')) 
       self.passed.append(link.get('href')) 
       print(self.passed) 

      for photo in self.passed: 
       src = 'http://po.com/{}/{}'.format(self.code_url[0],photo) 
       print(src) 
       album = AsyncImage(source=src,) 
       self.ids.grid.add_widget(album) 
       self.spinner.active = False 
       self.code_url.pop(0) 
       print(self.code_url) 

回答

0

Kivy不是線程安全的。如果你想觸摸任何與小部件相關的東西,你必須從kivy的循環中調用它。一個線程只能用於數據收集。

所以:

  1. 從外部源收集數據。
  2. 完成後,使用Clock.schedule_once(lambda dt: callback(data))刷新GUI,其中callback是一個窗口小部件操作函數,data是要顯示的收集數據。