0

I have tried using a formatted code that already existed where I imported 'PIL' from 'ImageTk' however, that has not worked. The image and script is in the same file directory yet the image data is not recognizable. I changed the string to 'raw string' by adding the 'r' in front of the file path and I even tried using a different image and it still outputs the same error. This line linebackground_image = tk.PhotoImage(file=r'C:\Users\kevan\OneDrive\Desktop\Programming for me\Python\Tkinker\bgfortest.png') of the code is the source of error.

Your help would be appreciated

import tkinter as tk

HEIGHT = 700

WIDTH = 800

root = tk.Tk()

canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH)

canvas.pack()

background_image = tk.PhotoImage(file=r'C:\Users\kevan\OneDrive\Desktop\Programming for me\Python\Tkinker\bgfortest.png')

background_label = tk.Label(root, image=background_image)

background_label.place(x=0, y=0, relheight=1, relwidth=1)

frame = tk.Frame(root, bg='#80c1ff', bd=5)

frame.place(relx=0.5, rely=0.1, relheight=0.1, relwidth=0.75, anchor='n')

button = tk.Button(frame, text="Hello",  font=40)

button.place(relx=0.7, relheight=1, relwidth=0.3)

entry = tk.Entry(frame, bg='white', font=40)

entry.place(relheight=1, relwidth=0.65)

lower_frame = tk.Frame(root, bg='#80c1ff', bd=10)

lower_frame.place(relx=0.5, rely=0.25, relheight=0.6, relwidth=0.75, anchor='n')

label = tk.Label(lower_frame, text="This is a python GUI.", bg='cadet blue', fg="Black")

label.place(relwidth=1, relheight=1)


root.mainloop()
acw1668
  • 40,144
  • 5
  • 22
  • 34
Kevan
  • 63
  • 1
  • 6
  • What do you mean by "unrecognizable"? Do you see an image that is garbage? Is it the same image but resized? Is it part of the image? – Tim Roberts Apr 01 '21 at 03:30
  • [tkinter-photoimage-doesnt-not-support-png-image](https://stackoverflow.com/questions/27599311/tkinter-photoimage-doesnt-not-support-png-image) may help. – acw1668 Apr 01 '21 at 03:42
  • @TimRoberts The image does not appear at all. – Kevan Apr 01 '21 at 06:47
  • @acw1668 I tried the corrections from that initial script. – Kevan Apr 01 '21 at 06:48
  • It may be your tk version because PNG is supported after version 8.6. Or the image is not PNG actually. You can try using `Pillow` module instead. – acw1668 Apr 01 '21 at 07:06

0 Answers0