wxPython获取远程映像和线程

wxPython获取远程映像和线程,python,wxpython,urllib2,python-multithreading,Python,Wxpython,Urllib2,Python Multithreading,我正在尝试用wxPython编写一个应用程序,以显示来自Amazon商店的一些商品(目前仅以JPG的形式显示书籍封面)。我正在使用urllib2,并将它们显示在位图按钮上,并列出它们以供进一步操作,但在编码之后,主窗口/应用程序似乎只有在获取所有URL/图像后才会加载。通过谷歌搜索,我了解到,在应用程序主代码运行时,我们必须使用线程来中断操作,但这是我第一次尝试wxpython,我读过的所有例子让我更加困惑 我已经在下面提到了我正在为任何专家工作的代码,他们可以给我一些想法,如何确保每个URL在

我正在尝试用wxPython编写一个应用程序,以显示来自Amazon商店的一些商品(目前仅以JPG的形式显示书籍封面)。我正在使用urllib2,并将它们显示在位图按钮上,并列出它们以供进一步操作,但在编码之后,主窗口/应用程序似乎只有在获取所有URL/图像后才会加载。通过谷歌搜索,我了解到,在应用程序主代码运行时,我们必须使用线程来中断操作,但这是我第一次尝试wxpython,我读过的所有例子让我更加困惑

我已经在下面提到了我正在为任何专家工作的代码,他们可以给我一些想法,如何确保每个URL在读取时被抓取和显示。。。下面的代码是这里和网络上各种示例的合并,因此请原谅我的技能不足

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

您应该使用线程获取url,然后使用wx.CallAfter返回主线程以显示数据。。。下面是如何更改代码的

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

您应该使用线程获取url,然后使用wx.CallAfter返回主线程以显示数据。。。下面是如何更改代码的

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

非常感谢你,我现在对这个电话有点了解了,希望这也能帮助到其他人!我有一个关于上述解决方案的后续问题,我在下面的帖子中又提出了一个问题,非常感谢,我现在对这个电话有了更好的理解,希望这对其他人也有帮助!我有一个关于上述解决方案的后续问题,我在下面的帖子中提出了另一个问题,