2016-12-19 2 views
0

J'essaye d'écrire une application dans wxPython pour afficher quelques éléments du magasin Amazon (pour l'instant seulement les couvertures de livres comme JPG). J'utilise urllib2 et les affiche sur un bouton bitmap et les énumérant pour d'autres actions, mais après le codage, la fenêtre principale/application ne semble charger après que toutes les URL/images ont été récupérées .. sur googling je comprends que nous devons utilisez Threads pour casser l'opération pendant que le code principal des applications s'exécute, mais ceci est ma première tentative avec wxpython et tous les exemples que j'ai lus me rendent encore plus confus.wxPython récupérer l'image distante et le filetage

J'ai mentionné ci-dessous le code que je travaille avec des experts qui peuvent me donner des idées sur la façon de s'assurer que chaque URL est lue et affichée comme elle les lit ... le code ci-dessous est un amalgame et le web pls excuse mon manque de compétences ..

import wx 
import os 
import sys 
import urllib2 
import cStringIO 

urls = ['https://images-na.ssl-images-amazon.com/images/I/51-u3J3mtTL._AC_US100_.jpg', 
     'https://images-na.ssl-images-amazon.com/images/I/51cRqX8DTgL._AC_US100_.jpg', 
     'https://images-na.ssl-images-amazon.com/images/I/515iBchIIzL._AC_US100_.jpg', 
     'https://images-na.ssl-images-amazon.com/images/I/511MaP7GeJL._AC_US100_.jpg', 
     'https://images-na.ssl-images-amazon.com/images/I/51jizRmRYYL._AC_US160_.jpg'] 

class Example(wx.Frame): 

    def __init__(self, *args, **kwargs): 
     super(Example, self).__init__(*args, **kwargs) 
     self.InitUI() 
     self.Ctrls() 
     self.makeButtons() 

    def makeButtons(self): 

     i = 0 

     for url in urls: 

      f = urllib2.urlopen(url) 
      data = f.read() 

      i += 1 
      print " url = ",url, " ",i 
      stream = cStringIO.StringIO(data) 
      bmp = wx.BitmapFromImage(wx.ImageFromStream(stream)) 
      button = wx.Button(self.panel, -1, "Book cover", style=wx.ALIGN_CENTER, size=wx.Size(100,100)) 
      button.SetToolTipString("wx.Button can how have an icon on the left, right,\n" 
          "above or below the label.") 
      button.SetBitmap(bmp, 
        wx.LEFT # Left is the default, the image can be on the other sides too 
        #wx.RIGHT 
        #wx.TOP 
        #wx.BOTTOM 
        ) 
      button.SetBitmapMargins((4,4)) 
      button.SetFont(wx.Font(8, wx.SWISS, wx.NORMAL, wx.BOLD, False)) 
      self.wrapSizer.Add(button, 1, wx.EXPAND) 
     self.Show(True) 
     self.panel.Layout() 

    def InitUI(self): 
     self.SetSize((800, 400)) 
     self.SetTitle('Dynamically Flow Buttons to Next Row on Window-Resize') 
     self.Centre() 


    def Sizers(self): 
     self.wrapSizer = wx.WrapSizer() 
     self.panel.SetSizer(self.wrapSizer) 

    def Ctrls(self): 
     self.panel = wx.Panel(parent=self,pos=wx.Point(0,0), size=wx.Size(750,550), style=wx.TAB_TRAVERSAL) 
     self.Sizers() 

def main(): 

    ex = wx.App() 
    Example(None) 
    ex.MainLoop() 

if __name__ == '__main__': 
    main() 

Répondre

2

Vous devez utiliser des fils pour la fetching url puis utilisez wx.CallAfter pour revenir au thread principal pour afficher les données ... voici comment votre le code peut être changé

import threading 

def makeButtons(self): 


    def _update_data(data): 

     stream = cStringIO.StringIO(data) 
     bmp = wx.BitmapFromImage(wx.ImageFromStream(stream)) 
     button = wx.Button(self.panel, -1, "Book cover", style=wx.ALIGN_CENTER, size=wx.Size(100,100)) 
     button.SetToolTipString("wx.Button can how have an icon on the left, right,\n" 
         "above or below the label.") 
     button.SetBitmap(bmp, 
       wx.LEFT # Left is the default, the image can be on the other sides too 
       #wx.RIGHT 
       #wx.TOP 
       #wx.BOTTOM 
       ) 
     button.SetBitmapMargins((4,4)) 
     button.SetFont(wx.Font(8, wx.SWISS, wx.NORMAL, wx.BOLD, False)) 
     self.wrapSizer.Add(button, 1, wx.EXPAND) 
     self.Show(True) 
     self.panel.Layout() 

    def f(): 
     f = urllib2.urlopen(url) 
     data = f.read() 
     wx.CallAfter(_update_data, data) 

    for url in urls: 
     threading.Thread(target=f).start() 
+1

Merci beaucoup, je comprends un peu l'appel peu mieux maintenant, j'espère que cela aide les autres aussi! –

+0

J'ai eu une question de suivi à la solution ci-dessus pour laquelle j'ai ouvert une autre question dans le post suivant, http://stackoverflow.com/questions/41339294/wxpython-threading-display-images-as-they-are-loaded –