如何在python的tkinter的网格中显示图像?

Sre*_*Len 1 tkinter python-3.x

我想在python的tkinter中将照片插入到网格中,然后按行和列显示这些照片。(行数和列数取决于您输入的内容)

我尝试使用附加列表将图片插入列表,但我只能显示其中的一列。

from tkinter import *

w = Tk()

w.geometry('1000x800')

p = 'picture.png'

img = PhotoImage(file=p)

for i in range(0, 5):
    photo_list.append(Label(w, image=img))

for j in range(0, len(photo_list)):
    photo_list[j].pack()
Run Code Online (Sandbox Code Playgroud)

我想在行和列中显示这些图片。例如,如果 row=3, column=3,则输出应为:

[图片][图片][图片]

[图片][图片][图片]

[图片][图片][图片]

Val*_*ino 5

尝试:

nr = 5 #number of rows
nc = 2 #number of columns

photo_list = []

for i in range(nr*nc):
    photo_list.append(Label(w, image=img))
    photo_list[-1].grid(row=i//nc, column=i%nc)
Run Code Online (Sandbox Code Playgroud)

在这里,我使用了网格几何管理器,它允许比包几何管理器更容易地做你想做的事。图像逐行放置。
grid接受两个被调用的参数rowcolumn它们是理想地绘制在容器表面上的 2D 网格中小部件的索引(从 0 开始)。tkinter可以自动确定网格的尺寸并管理尺寸。