简体   繁体   中英

Image Background Does not Initialize in Tkinter (Using Python)

I am trying to get set an image background for an app using Tkinter. However, I am getting an error code I don't understand. It seems there is something wrong with the PhotoImage method in the Tkinter class, but I feel that's unlikely and is something on my part. Code:

import tkinter as tk

HEIGHT = 700
WIDTH = 800



root = tk.Tk()

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

#Background Picture
background_img = tk.PhotoImage(file = 
"C:\\Users\\jcoli\\Downloads\\Wallpapers\\GundamNarrative2.jpg")
background_label = tk.Label(root, image=background_img)
background_label.place(relwidth=1, relheight=1)

#Background
background = tk.Frame(root, bg = "blue")
background.place(relx=.5, rely=.1, relwidth=0.75, relheight=0.5, anchor="n")

# Buttons
record_button = tk.Button(background, text = "Record", bg = "green")
record_button.place(relx = 0, rely = .85, relwidth = 0.15, relheight = 0.15)

skeleton_button = tk.Button(background, text = "Skeleton", bg = "red")
skeleton_button.place(relx = .4, rely = .85, relwidth = 0.15, relheight = 0.15)

frame_button = tk.Button(background, text = "Frame", bg = "yellow")
frame_button.place(relx = .8, rely = .85, relwidth = 0.15, relheight = 0.15)

#Label
video_label = tk.Label(background, text = "Enter video name below")
video_label.place(relx = 0.40, rely=0, relwidth=.25, relheight=.15)

#Entry Area
entry = tk.Entry(background, bg = 'gray')
entry.place(relx = 0.4, rely=.15, relwidth=.25, relheight=.05)

video_label = tk.Label(background, text = "Please enter video name")


root.mainloop()

As acw1668 said, the PhotoImage method does not support a JPG image. I personally converted the JPG to PNG to achieve success.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM