2016-07-23 36 views
0

我与网格布局挣扎 - 基本上我想在这样的循环打印一些数据: enter image description hereTkinter的网格布局在环

但不能由我自己看着办吧。我设法使之成为第一个进入正常,但只是工作 - 我的代码:

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

和结果:

enter image description here

我也试过这样:

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

由于图片应该占据两行(我们称之为1和2),“左上角”应该在第1列的第1行,第2列的“右上角”以及第2行的较低位置。

+0

似乎你已经错误地使用了循环。你真的在循环后面用过“我”吗?首先,让我们学习如何使用循环创建标签。 –

+0

我知道我需要**我**,但我不知道如何正确地做到这一点,所以图像将占用两行+一列,但仍然能够填充两行作为附加的图片。 – PotatoBox

回答

1

这是你在找什么:

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) 

+0

非常好,加入_pady_一切都很好! – PotatoBox