简体   繁体   中英

How to use small single image and repeat it all over the window to make it a background image using tkinter GUI?

I have a very small 10 x 10 pixel jpg image that I want to repeat it in the whole background using tkinter.

from tkinter import *
from PIL import Image, ImageTk
 
root = Tk()
img = Image.open(resource_path("FILE LOCATION//filename.jpg"))
for i in range(0,1000):
    image1 = ImageTk.PhotoImage(i)
    label1 = Label(root, image=image1)
    label1.pack()

root.mainloop()

This code this iterating but not sequentially.. I am noob in tkinter GUI development. Please help.

You need to keep a reference to the image, or it gets garbage collected.

Create rows of canvases, and put in how many pictures you want in each row.

from tkinter import *
from PIL import Image
from PIL import ImageTk

root = Tk()
img = Image.open(resource_path("FILE LOCATION//filename.jpg"))
image1 = ImageTk.PhotoImage(img)
reference_to_image = Canvas(root)
reference_to_image.image = image1

for count in range(10):
    canvas = Canvas(root)
    canvas.pack(side = TOP)
    for counter in range(10):
        label1 = Label(canvas, image=image1,borderwidth = 0, highlightthickness=0)
        label1.pack(side=LEFT)

root.mainloop()

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