PotatoBox
PotatoBox

Reputation: 608

Tkinter grid layout in loop

I'm struggling with grid layout - basically I want to print some data in the loop like this: enter image description here

But can't figure it out by myself. I managed to make it work properly but just for the first entry - my code:

from tkinter import *
from PIL import Image, ImageTk
import urllib.request
import io

app = Tk()
images = []

for i in range(0, 8):
    text1 = Label(font=("Helvetica",10), text="top, left", cursor="hand2")
    text2 = Label(font=("Helvetica",10), text="top, right")
    text3 = Label(font=("Helvetica",10),text="lower")

    image = "https://www.gravatar.com/avatar/b9630126afbff209bb068195307a5e4c?s=328&d=identicon&r=PG"
    get_image = urllib.request.urlopen(image).read()
    im1 = Image.open(io.BytesIO(get_image))
    im_small = im1.resize((70, 70))
    im = ImageTk.PhotoImage(im_small)
    image1 = Label(app, image=im)
    images.append(im)

    image1.grid(rowspan=2, column=0, sticky=W)
    text1.grid(row=0, column=1, sticky=W)
    text2.grid(row=0, column=2, sticky=W)
    text3.grid(row=1, column=1, sticky=W)

app.mainloop()

and the result:

enter image description here

I also tried this:

from tkinter import *
from PIL import Image, ImageTk
import urllib.request
import io

app = Tk()
images = []

for i in range(0, 8):
    text1 = Label(font=("Helvetica",10), text="top, left", cursor="hand2")
    text2 = Label(font=("Helvetica",10), text="top, right")
    text3 = Label(font=("Helvetica",10),text="lower")

    image = "https://www.gravatar.com/avatar/b9630126afbff209bb068195307a5e4c?s=328&d=identicon&r=PG"
    get_image = urllib.request.urlopen(image).read()
    im1 = Image.open(io.BytesIO(get_image))
    im_small = im1.resize((70, 70))
    im = ImageTk.PhotoImage(im_small)
    image_cover = Label(app, image=im)
    images.append(im)

    image_cover.grid(rowspan=2, column=0, sticky=W)
    text1.grid(row=i+1, column=1, sticky=W)
    text2.grid(row=i+1,column=2)
    text3.grid(row=i+2, column=1, sticky=W)

app.mainloop()

Since the picture should occupy two rows (let's call it 1 and 2), "top left" should be in row number 1 in column 1, "top right" in column number two, and lower in row 2.

Upvotes: 0

Views: 3182

Answers (1)

Parviz Karimli
Parviz Karimli

Reputation: 1287

Is this what you're looking for:

from tkinter import *
root = Tk()
root.geometry("500x500")

imgvar = PhotoImage(file="world.gif")

for i in range(5):
    Label(root, image=imgvar, bg='red', bd=10, relief='groove').grid()
    Label(root, text="Upper", bg='blue', bd=5, relief='groove').grid(column=1, row=i, sticky=N)
    Label(root, text="Lower", bg='green', bd=5, relief='groove').grid(column=1, row=i, sticky=S)

?

Upvotes: 1

Related Questions