python - 如何使用小的单个图像并在整个窗口中重复它以使用 tkinter GUI 使其成为背景图像?

标签 python python-3.x user-interface tkinter tkinter-canvas

我有一个非常小的 10 x 10 像素 jpg 图像,我想使用 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()

这段代码是迭代的,但不是按顺序的。我是 tkinter GUI 开发的菜鸟。请帮忙。

最佳答案

您需要保留对图像的引用,否则它会被垃圾收集。

创建多行 Canvas ,并在每行中放入您想要的图片数量。

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()

关于python - 如何使用小的单个图像并在整个窗口中重复它以使用 tkinter GUI 使其成为背景图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64958250/

相关文章:

c++ - Qt 样式表和 "one argument"问题

JavaFX:如何将文本字段 "style property"绑定(bind)到匹配某些模式的文本属性

python - 如何将 pandas 中的 TimeSeries 对象转换为整数?

python-3.x - 如何检查列表理解中的 np.nan ?

Python 是,== 运算符优先级

python - “name is not defined” 内部函数

ios - NSTextContainer 的 usedRectForTextContainer 返回不正确的高度

python - 当我必须在长 pandas 系列(> 15 M 元素)中搜索时,如何优化 'enumerate'?

python - SICK 激光扫描仪不以 ROS 启动

python MySQL : Lost connection to MySQL server during query