2017-01-14 100 views
0

我有一個背景圖像使用tkinter畫布, ,我添加圖像的頂部。 到目前爲止它的效果非常好。但我想要做的是能夠按需刪除一些地形圖像。當我刪除它們中的一些時,我希望看到它們背後的背景,就像在它上面添加這些背景圖像之前一樣。蟒蛇tkinter圖像層(粘貼/ unpaste圖像的背景)

這將是︰粘貼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

+0

你試圖調用畫布的'delete'方法? –

+0

感謝您的回覆,但是我應該在背景上還是在白色圓圈之一上調用刪除?哼,所以我把每個圓圈對象放在一個變量中,然後我可以調用canvas.delete(變量)? – freeaks

+0

你爲什麼不試試看看會發生什麼? –

回答

2

的情況下,它可能會幫助別人這裏是一個可行的解決方案。 我添加了一個按鈕,可以逐個刪除放置在畫布上的每個對象。 (感謝您的幫助,布萊恩奧克利)

#!/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() 
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()