2017-04-10 32 views
0

這是我正在使用的代碼。我只是無法看到將圖像插入網格的位置。我知道fill' is for顏色should I use image.open`?如果是這樣的話,那麼在網格中呢?如何在tkinter網格中使用圖像?

class photo(): # take the photo 
    def __init__(self,filename=None): 
     with picamera.PiCamera() as cam: 
      cam.resolution= (640, 480) 
      cam.framerate = 60 
      cam.AWB_MODES 
      cam.EXPOSURE_MODES 
      for effect in cam.IMAGE_EFFECTS: 
       cam.image_effect=random 
      for i in range(1000): 
       cam.capture('/home/pi/Pictures/camFolder/img_%i.jpg') 
       time.sleep(2) 

class App(tk.Tk): # build the grid 
    def __init__(self, *args, **kwargs): 
     tk.Tk.__init__(self, *args, **kwargs) 

     self.canvas = tk.Canvas(self, width=1000, height=1000, borderwidth=0, highlightthickness=0) 
     self.canvas.pack(side="top",fill="both", expand="true") 
     self.rows= 10 
     self.columns =10 
     self.cellwidth =100 
     self.cellheight =100 
     self.rect = {} 
     self.oval = {} 
     for column in range(20): 
      for row in range(20): 
       x1 = column*self.cellwidth 
       y1= row * self.cellwidth 
       x2 = x1 + self.cellwidth 
       y2 = y1 + self.cellheight 
       self.rect[row,column]= self.canvas.create_rectangle(x1,y1,x2,y2, fill ="blue", tags="rect") 
       self.oval[row,column]= self.canvas.create_oval(x1+2,y1+2,x2-2,y2-2, fill="blue", tags="oval") 

     self.redraw(1000) 

    def redraw(self,delay): 
     self.canvas.itemconfig("rect", fill="blue") 
     self.canvas.itemconfig("oval", fill="yellow") 
     for i in range(10): 
      row = random.randint(0,19) 
      col = random.randint(0,19) 
      item_id = self.oval[row,col] 
      self.canvas.itemconfig(item_id, fill="blue") 
     self.after(delay, lambda: self.redraw(delay)) 

if __name__=="__main__": 
    app = App() 
    app.mainloop() 

我是新來的Python,我一直在飛行中學習。

回答

0

你應該使用tkinter的PhotoImage來添加圖像到你的程序。這是一個簡短的doc,告訴你如何。

您可以使用它像這樣:

from PIL import Image, ImageTk 

image = Image.open("lenna.jpg") 
photo = ImageTk.PhotoImage(image) 

label = Label(image=photo) 
label.image = photo 
label.pack() 

注:Tkinter的不支持JPG或PNG或多種類型,你將需要使用PIL或枕頭叉如上看起來。唯一支持的格式是GIF和PGM/PPM,其中您不需要使用PIL。

相關問題