My code:
![[Image: d6a4a5c68148a348a654a0a4a7774cfb.png]](https://i.gyazo.com/d6a4a5c68148a348a654a0a4a7774cfb.png)
The error:
![[Image: 67e616fd9ee26818bfe80edb628e9ede.png]](https://i.gyazo.com/67e616fd9ee26818bfe80edb628e9ede.png)
the image:
![[Image: d6a4a5c68148a348a654a0a4a7774cfb.png]](https://i.gyazo.com/d6a4a5c68148a348a654a0a4a7774cfb.png)
The error:
![[Image: 67e616fd9ee26818bfe80edb628e9ede.png]](https://i.gyazo.com/67e616fd9ee26818bfe80edb628e9ede.png)
the image:
|
Invalid argument: 'images\x08ackground.jpg'
|
|
My code:
![]() The error: ![]() the image:
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
I welcome all feedback.
The only dumb question, is one that doesn't get asked. My Github How to post code using bbtags Download my project scripts
Jun-18-2023, 12:05 PM
(This post was last modified: Jun-18-2023, 12:05 PM by Gribouillis.)
Try
'images/background.jpg' or r'images\background.jpg' or 'images\\background.jpg'
Jun-18-2023, 12:42 PM
\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.
Jun-19-2023, 09:28 AM
thank you guys!
|
|
|
| Possibly Related Threads… | |||||
| Thread | Author | Replies | Views | Last Post | |
| [Errno 22] Invalid argument | satyaneel | 11 | 136,318 |
Jul-14-2020, 08:47 AM Last Post: lichunming |
|
| zlib decompress error: invalid code lengths set / invalid block type | DreamingInsanity | 0 | 11,175 |
Mar-29-2020, 12:44 PM Last Post: DreamingInsanity |
|
| SyntaxError: positional argument follows keyword argument | syd_jat | 3 | 9,390 |
Mar-03-2020, 08:34 AM Last Post: buran |
|
| Invalid argument error thrown. | pyseeker | 4 | 10,533 |
Sep-10-2019, 07:03 PM Last Post: pyseeker |
|
| OSError: [Errno 22] Invalid argument - wasn't there last time I ran the code! | meganhollie | 2 | 12,155 |
Jun-11-2018, 06:01 PM Last Post: meganhollie |
|
| [SOLVED] OSError: [Errno 22] Invalid argument | Panda | 13 | 55,526 |
Jun-04-2018, 08:33 PM Last Post: volcano63 |
|
| [Errno 22] Invalid argument | Asafb | 8 | 17,652 |
Dec-19-2017, 12:52 PM Last Post: Larz60+ |
|