When I click the button to change the image, the window refresh does not work. I don't know how to implement it properly. How can I refresh the api with the button. I'm new to pysimplegui; can somebody point me in the correct direction?
from io import BytesIO
import PySimpleGUI as sg
from PIL import Image
import requests, json
def image_to_data(im):
with BytesIO() as output:
im.save(output, format="PNG")
data = output.getvalue()
return data
cutURL = 'https://meme-api.herokuapp.com/gimme/wholesomememes'
imageURL = json.loads(requests.get(cutURL).content)["url"]
data = requests.get(imageURL).content
stream = BytesIO(data)
img = Image.open(stream)
giy=image_to_data(img)
control_col = sg.Column([
[sg.Button('next meme', key = '_3_')],])
image_col = sg.Column([[sg.Image(giy, key = '-IMAGE-')]])
layout = [[control_col,image_col]]
window = sg.Window('meme', layout,finalize=True)
while True:
event, values = window.read()
if event is None:
break
if '_3_' in event:
window.refresh()
window.close()
CodePudding user response:
Method window.refresh()
called to update the GUI if you do some changes to element(s) and you want to update it immediately before the script execute next window.read()
which also update the GUI.
It is necessary to download next picture from website again by your code, window.refresh()
won't automatically update new image.
from io import BytesIO
import PySimpleGUI as sg
from PIL import Image
import requests, json
def image_to_data(im):
with BytesIO() as output:
im.save(output, format="PNG")
data = output.getvalue()
return data
def get_data():
cutURL = 'https://meme-api.herokuapp.com/gimme/wholesomememes'
imageURL = json.loads(requests.get(cutURL).content)["url"]
data = requests.get(imageURL).content
stream = BytesIO(data)
img = Image.open(stream)
giy = image_to_data(img)
return giy
control_col = sg.Column([[sg.Button('next meme', key = '_3_')],])
image_col = sg.Column([[sg.Image(get_data(), key = '-IMAGE-')]])
layout = [[control_col,image_col]]
window = sg.Window('meme', layout,finalize=True)
while True:
event, values = window.read()
if event is None:
break
if event == '_3_':
window['-IMAGE-'].update(get_data())
window.close()