使用Tkinter,我如何发布两次相同的图像

时间:2015-04-20 02:48:51

标签: python tkinter

import urllib, io
from Tkinter import *
from PIL import Image, ImageTk
from bs4 import BeautifulSoup
import Tkinter as tk

root = Tk()
root.geometry("1000x800+20+20")

im=[]
t1=[]
z1=[]
x=var=0


def Search():

    global t1
    global var

    var = IntVar()
    show = "http://blog.acorn-is.com/wp-content/uploads/apple-full2.jpg"  #random google image
    fd = urllib.urlopen(show)
    imgFile = io.BytesIO(fd.read())
    im.append (ImageTk.PhotoImage(Image.open(imgFile)))
    image = Label(root, image = im[0])

    t1.append(image)  
    z1.append(image)
    #both t1 and z1 should contain the image..

    r1 = Checkbutton(root, variable=var, command=Queue)
    r1.place(bordermode=INSIDE, x=0, y=90)


def Queue():

    if (var.get() == 1):
            z1[0].place(bordermode=INSIDE, x=0, y=300)   #x=0
            t1[0].place(bordermode=INSIDE, x=550, y=300) #x=550



tx1 = StringVar()
b1 = Button(root, text="Search", command=Search, width="20")
b1.grid(row=0, column=0)
root.mainloop()

上面的示例代码。你会在搜索功能中注意到,t1和z1都将图像附加到他们自己的列表中,但是当我"放置" z1和t1,图像只显示一次 - 仅显示第二张图像。我怎样才能让它显示两次相同的图像?

1 个答案:

答案 0 :(得分:0)

请参阅以下更改。这将使它工作!

import urllib, io
from Tkinter import *
from PIL import Image, ImageTk
from bs4 import BeautifulSoup
import Tkinter as tk

root = Tk()
root.geometry("1000x800+20+20")

im=[]
t1=[]
z1=[]
x=var=0


def Search():

    global t1
    global var

    var = IntVar()
    show = "http://blog.acorn-is.com/wp-content/uploads/apple-full2.jpg"  #random google image
    fd = urllib.urlopen(show)
    imgFile = io.BytesIO(fd.read())
    im.append (ImageTk.PhotoImage(Image.open(imgFile)))
    image = Label(root, image = im[0])

    t1.append(image)

    # New added line
    image = Label(root, image = im[0])

    z1.append(image)
    #both t1 and z1 should contain the image..

    r1 = Checkbutton(root, variable=var, command=Queue)
    r1.place(bordermode=INSIDE, x=0, y=90)


def Queue():

    if (var.get() == 1):
            z1[0].place(bordermode=INSIDE, x=0, y=300)   #x=0
            t1[0].place(bordermode=INSIDE, x=550, y=300) #x=550



tx1 = StringVar()
b1 = Button(root, text="Search", command=Search, width="20")
b1.grid(row=0, column=0)
root.mainloop()