简体   繁体   中英

Setting an image as a tkinter window background

I am Trying to set an image as the background of my Tkinter window and everything I try doesn't seem to work. Here's the code I have:

import random
import Tkinter # note use of caps
from Tkinter import *
from PIL import Image, ImageTk
import os.path


window = Tk()
window.title('Ask If You Dare')
window.configure(background = '#007501')

If you want the background to be an image (without using canvas), use labels:

import random
import Tkinter # note use of caps
from Tkinter import *
from PIL import Image, ImageTk
import os.path

window = Tk()
window.title('Ask If You Dare')

photo=PhotoImage(file="path/to/your/file")
l=Label(window,image=photo)
l.image=photo       #just keeping a reference
l.grid()

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