2016-12-19 8 views
0

Amazonストアからいくつかのアイテムを表示するためにwxPythonでアプリケーションを作成しようとしています(現在はJPGとしてブックカバーのみ)。私はurllib2を使用してビットマップボタンに表示し、それ以上のアクションのためにそれらをリストするが、それをコーディングした後、メインウィンドウ/アプリケーションは、すべてのURL /画像がフェッチされた後にロードされるようだ。アプリケーションのメインコードが実行されている間にスレッドを使用して操作を中断しますが、これはwxpythonでの最初の試みであり、私が読んだすべての例はさらに混乱します。wxPythonがリモートイメージとスレッドをフェッチする

私は、各URLが読み込まれるようにフェッチされて表示されるようにする方法について私にアイデアを与えることができる専門家のために、以下に述べるコードを述べました。下記のコードは、そしてウェブはそうplsはどのようにここにある...あなたは、URLフェッチのためのスレッドを使用して、データを表示するためにメインスレッドに戻るためにwx.CallAfterを使用する必要があります。..

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() 

答えて

2

のスキルの私の不足を言い訳しますコードを変更することができます

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

大変ありがとうございますもう少し良くなっています。うまくいけば、他の人にも役立ちます –

+0

私は次の投稿で別の質問を開いた上記の解決策について質問をしました。http://stackoverflow.com/questions/41339294/wxpython-threading-display-images-as-they-are-loaded –

関連する問題