python tkinter图像图层(在背景上粘贴/取消图像)

时间:2017-01-14 14:35:59

标签: python-3.x tkinter tkinter-canvas

我有一个使用tkinter画布的背景图片, 我正在它上面添加图像。 到目前为止它很好用。但我想做的是能够按需删除一些forground图像。当我删除其中的一些时,我希望看到它们背后的背景,就像在它上面添加那些forground图像一样。

就像:粘贴5个前景图像然后删除其中的1个或2个。 所以这个程序我必须要远,在随机位置添加一些白色圆圈。 如果我在每个小白圈上保持一个句柄(我可以将它们放在变量中并将它们全部放在一个列表中,然后稍后获取它们的坐标)。我怎样才能删除其中的一部分,并在删除的白色圆圈后面看到我的背景? 它甚至可能吗?

#!/usr/bin/env python3
from tkinter import *
from PIL import Image, ImageTk
from random import *

class App(object):

    def __init__(self):
        self.root =  Tk()
        self.canvas = Canvas(self.root, height=222, width=227)
        self.canvas.grid()
        # small nature landscape
        self.backgnd = PhotoImage( file = "images/nature.png" )
        # small white circle 
        self.mycloud = PhotoImage( file = "images/white.png" )
        backgnd_width = (self.backgnd.width()/2)
        backgnd_height = (self.backgnd.height()/2)
        self.canvas.create_image(backgnd_width,backgnd_height,image=self.backgnd)

    def cloud(self):
        pos_x = randint(1,220)
        pos_y = randint(1,220)
        self.canvas.create_image(pos_x,pos_y, image=self.mycloud)


app = App()
app.cloud()
app.cloud()
app.cloud()
app.cloud()
app.cloud()
app.root.mainloop() 

enter image description here

2 个答案:

答案 0 :(得分:2)

如果它可以帮助其他人这是一个有效的解决方案。 我添加了一个按钮,可以删除放在画布上的每个对象,一次一个。 (感谢你的帮助,Bryan Oakley)

#!/usr/bin/env python3
from tkinter import *
from PIL import Image, ImageTk
from tkinter import ttk
from random import *

class App(object):

    def __init__(self):
        self.root =  Tk()
        self.canvas = Canvas(self.root, height=300, width=227)
        self.canvas.grid()
        self.mylist=[]

        self.backgnd = PhotoImage( file = "images/nature.png" )
        self.mycloud = PhotoImage( file = "images/white.png" )
        backgnd_width = (self.backgnd.width()/2)
        backgnd_height = (self.backgnd.height()/2)
        self.canvas.create_image(backgnd_width,backgnd_height,image=self.backgnd)

        # button to remove things on the canvas
        button_del = ttk.Button(self.root, text='Del')
        button_del['command'] = self.rem
        button_del.place(x=100, y=250)

    def cloud(self):
        # add 5 object at random position on the canvas
        for idx in range(5):
            pos_x = randint(1,220)
            pos_y = randint(1,220)
            self.mylist.append(self.canvas.create_image(pos_x,pos_y, image=self.mycloud))

    def rem(self):
        # delete elements placed on the canvas
        self.canvas.delete(self.mylist[-1])
        self.mylist.pop()


app = App()
app.cloud()
app.root.mainloop()

答案 1 :(得分:0)

进行了一些更改,使上面的代码与python 2兼容:

from Tkinter import *
from PIL import Image, ImageTk
import ttk
from random import *

class App(object):

    def __init__(self):
        self.root =  Tk()
        self.canvas = Canvas(self.root, height=300, width=227)
        self.canvas.grid()
        self.mylist=[]

        self.backgnd = ImageTk.PhotoImage( Image.open("sunshine.jpg") )
        self.mycloud = ImageTk.PhotoImage( Image.open("Y.png") )
        backgnd_width = (self.backgnd.width()/2)
        backgnd_height = (self.backgnd.height()/2)
        self.canvas.create_image(backgnd_width,backgnd_height,image=self.backgnd)

        # button to remove things on the canvas
        button_del = ttk.Button(self.root, text='Del')
        button_del['command'] = self.rem
        button_del.place(x=100, y=250)

    def cloud(self):
        # add 5 object at random position on the canvas
        for idx in range(5):
            pos_x = randint(1,220)
            pos_y = randint(1,220)
            self.mylist.append(self.canvas.create_image(pos_x,pos_y, image=self.mycloud))

    def rem(self):
        # delete elements placed on the canvas
        self.canvas.delete(self.mylist[-1])
        self.mylist.pop()


app = App()
app.cloud()
app.root.mainloop()