简体   繁体   中英

How can i use an image as a background in Tkinter?

So I want to use an image as my background in Tkinter kind of like how windows has background images in desktop. This is my code but it doesn't seem to work:

root = tk.Tk()
root.attributes("-fullscreen", True)
background_image=tk.PhotoImage("image")

The code runs but it shows up as a white background.

It is pretty simple: Here is how to do it:

from tkinter import *
root = Tk()
root.geometry("400x400")
bg = PhotoImage(file = "image.png")
label1 = Label(root, image = bg)
label1.place(x = 0, y = 0)
root.mainloop()

Just make sure to edit this part (file = "image.png") and use the pathname to your image. Preferably save it on the same folder as your program is stored, though it is not required.

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