Python Forum

Full Version: Invalid argument: 'images\x08ackground.jpg'
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
My code:
[Image: d6a4a5c68148a348a654a0a4a7774cfb.png]

The error:
[Image: 67e616fd9ee26818bfe80edb628e9ede.png]

the image:
[Image: d5ab3688d10d955feda970819a273e7d.png]
You should post actual code and not images of code. Make sure your image folder is at the project root.

An example

import os, sys
import tkinter as tk
from PIL import Image, ImageTk

# Get executing file path and split at /
file_path = sys.argv[0].split('/')

# pop the file part off
file_path.pop()

# Join the path with /
folder_path = '/'.join(file_path)


class LoginSystem:
    def __init__(self, parent):
        parent.title('Login System')
        parent.geometry('1350x700')

        # Using tk.PhotoImage
        img = tk.PhotoImage(file=f'{folder_path}/images/ratt.png')

        # We need this as not to loose image in the garbage collection
        img.img = img

        # Using PIL
        img2 = ImageTk.PhotoImage(Image.open(f'{folder_path}/images/ratt.png'))

        # As above needed so not to loose image in garbage collection
        img2.backup = img2

        # For the icon
        parent.iconphoto(True, img)

        font = ('None', 16, 'bold')

        # Creating some label frames
        labelframe = tk.LabelFrame(parent, text='Using a Canvas and tk.PhotoImage')
        labelframe['font'] = font
        labelframe.pack(side='left', expand='True', fill='both')

        labelframe2 = tk.LabelFrame(parent, text='Using a Label and tk.PhotoImage')
        labelframe2['font'] = font
        labelframe2.pack(side='left', expand='True', fill='both')

        labelframe3 = tk.LabelFrame(parent, text='Using a Canvas and PIL')
        labelframe3['font'] = font
        labelframe3.pack(side='left', expand='True', fill='both')

        # Creating a canvas for an image
        canvas = tk.Canvas(labelframe)
        canvas.pack(side='left', expand=1, fill='both')

        canvas.create_image(0, 0, anchor='nw', image=img)

        # Using a lable for image
        label = tk.Label(labelframe2, image=img, anchor='nw')
        label.pack(side='left', expand=True, fill='both')

        # Creating a canvas for the third image
        canvas = tk.Canvas(labelframe3)
        canvas.pack(side='left', expand=True, fill='both')

        canvas.create_image(0,0, anchor='nw', image=img2)

        
root = tk.Tk()
LoginSystem(root)
root.mainloop()

More can be found here on tkinter and images
Try 'images/background.jpg' or r'images\background.jpg' or 'images\\background.jpg'
\b is an escape sequence for entering the non-viisible ASCII backspace character, thus your background string name is " images<backspace> ackground.jpg".

Of Gribouillis' suggestions I like the forward slash the best. Python will replace with backslashes when it opens the file. The other two suggestions, the double backslash or raw string, are ways of turning off the special meaning a backslash has in a string literal.
thank you guys!