0

I'm trying to create buttons that use the image scrapped off a website. A function is called to scrape the image url and replace the current button image with the one at the other end of the url. However, the images appear as their background colour. How do I make it so the scraped image is the image for the button?

class ImageButton(CreateButton):
    def __init__(self, parent, image, command, location): 
        Button.__init__(self, parent, image=image, command=command)
        self.image = image
        self.style = {"bg":"#7D7D7D","bd":0,"highlightbackground":"#7D7D7D","highlightthickness":0}
        self.place(x=location[0], y=location[1])
        self.config(self.style)

self.charts = []
self.charts.append(ImageButton(self, None, self.change_image, [15,100]))
self.charts.append(ImageButton(self, None, self.change_image, [15,100]))
self.charts.append(ImageButton(self, None, self.change_image, [15,100]))
self.charts.append(ImageButton(self, None, self.change_image, [15,100]))
self.charts.append(ImageButton(self, None, self.change_image, [15,100]))
self.charts.append(ImageButton(self, None, self.change_image, [15,100]))
self.charts.append(ImageButton(self, None, self.change_image, [15,100]))
self.charts.append(ImageButton(self, None, self.change_image, [15,100]))
self.charts.append(ImageButton(self, None, self.change_image, [15,100]))
self.charts.append(ImageButton(self, None, self.change_image, [15,100]))

self.image_scrape()

def image_scrape(self):
    source = requests.get('https://www.metoffice.gov.uk/weather/maps-and-charts/surface-pressure').text
    soup = BeautifulSoup(source, 'lxml')
    spc1 = soup.find_all('img', attrs = {"alt": "Surface pressure - Analysis chart"})
    spc2 = soup.find_all('img', attrs = {"alt": "Surface pressure - Forecast chart"})
    for images in spc1:
        url = images.get('src')
        if 'colour' in url:
            img_byt = urlopen(url).read()
            img_b64 = base64.encodebytes(img_byt)
            p_img = PhotoImage(data=img_b64)
            r_img = p_img.subsample(2,2)
            self.charts[0].configure(image=r_img)

    for images in spc2:
        url = images.get('src')
        if 'colour' in url:
            img_byt = urlopen(url).read()
            img_b64 = base64.encodebytes(img_byt)
            p_img = PhotoImage(data=img_b64)
            r_img = p_img.subsample(2,2)
            self.charts[self.count].configure(image=r_img)
            self.count = self.count + 1
golem
  • 25
  • 6

0 Answers0