Home > Blockchain >  Displaying an Image in PySimpleGUI
Displaying an Image in PySimpleGUI

Time:02-02

I'm trying to display an image in a simple GUI using PySimpleGui but it might be a bit more complicated than I thought.

This is what I'm currently attempting:

import io
import os
import PySimpleGUI as sg
from PIL import Image
import sys

# GUI #


layout = [

        [sg.Image('2C.png')]
        [sg.Submit(), sg.Cancel()]

         ]

window = sg.Window('My Program', layout)

button,values = window.read()

And this is the error Sublime is throwing out:

C:(Directory)file.py:12: SyntaxWarning: list indices must be integers or slices, not tuple; perhaps you missed a comma?
  [sg.Image('2C.png')]
Traceback (most recent call last):
  File "file.py", line 12, in <module>
    [sg.Image('2C.png')]
TypeError: list indices must be integers or slices, not tuple

Really not sure what to google as there are so few PySimpleGUI resources out there.

Thanks for reading,

Callan

CodePudding user response:

There is a missing comma after [sg.Image('2C.png')]

CodePudding user response:

You have missed comma. Please make sure the image is in Current Working Directory or supply full accessible path to image, Like this example.

image="/storage/emulated/0/Pycode2/folder.png"

or put pictures in this folders and change your Current directory by this code. This is not a good programming practice.

#choose directory to work use
os.chdir('/storage/emulated/0/Pycode2/')

Your correct code is this

layout = [

    [sg.Image('2C.png'),],
    [sg.Submit(), sg.Cancel(),],

     ]

Please note these Extra comma do NOT cause error and are very helpful to add new Layout items. A missing comma raises this error.

  •  Tags:  
  • Related