2015-04-28 71 views
0

我試圖從菜單中加載圖像,然後進行縮放和圖像。縮放和菜單問題(Tkinter)

問題是我的程序不執行菜單並直接啓動圖像加載。我如何解決它?

謝謝!

from Tkinter import * 
import Image, ImageTk,tkFileDialog 

class LoadImage: 
    def __init__(self,root): 
     frame = Frame(root) 
     self.canvas = Canvas(frame,width=900,height=900) 
     self.canvas.pack() 
     frame.pack() 
     options={} 
     options['defaultextension']='.gif' 
     options['filetypes']=[('GIF','.gif'),('JPEG','.jpg'),('TIFF','.tif')] 
     options['title']='Select image file' 
     File=tkFileDialog.askopenfilename(**options) 
     self.orig_img = Image.open(File) 
     self.img = ImageTk.PhotoImage(self.orig_img) 
     self.canvas.create_image(0,0,image=self.img, anchor="nw") 
     self.zoomcycle = 0 
     self.zimg_id = None 
     root.bind("<MouseWheel>",self.zoomer) 
     self.canvas.bind("<Motion>",self.crop) 

    def zoomer(self,event): 
     if (event.delta > 0): 
      if self.zoomcycle != 4: self.zoomcycle += 1 
     elif (event.delta < 0): 
      if self.zoomcycle != 0: self.zoomcycle -= 1 
     self.crop(event) 

    def crop(self,event): 
     if self.zimg_id: self.canvas.delete(self.zimg_id) 
     if (self.zoomcycle) != 0: 
      x,y = event.x, event.y 
      if self.zoomcycle == 1: 
       tmp = self.orig_img.crop((x-45,y-30,x+45,y+30)) 
      elif self.zoomcycle == 2: 
       tmp = self.orig_img.crop((x-30,y-20,x+30,y+20)) 
      elif self.zoomcycle == 3: 
       tmp = self.orig_img.crop((x-15,y-10,x+15,y+10)) 
      elif self.zoomcycle == 4: 
       tmp = self.orig_img.crop((x-6,y-4,x+6,y+4)) 
      size = 300,200 
      self.zimg = ImageTk.PhotoImage(tmp.resize(size)) 
      self.zimg_id = self.canvas.create_image(event.x,event.y,image=self.zimg) 

root = Tk() 
root.title("Crop Test") 
menubar = Menu(root) 
filemenu = Menu(menubar, tearoff=0) 
filemenu.add_command(label="Abrir", command=LoadImage(root)) 
menubar.add_cascade(label="Archivo", menu=filemenu) 
root.config(menu=menubar) 
root.mainloop() 

回答

0
filemenu.add_command(label="Abrir", command=LoadImage(root)) 

command=LoadImage(root)將實例你的LoadImage類,並調用文件對話框。你想你的類在一個單獨的方法

  • 獨立的文件打開操作(說「abrir」)
  • 從GUI功能分別實例化的類(如:李=的LoadImage())
  • Connectthe 'abrir' 方法將Abrir按鈕,在菜單

這樣的分離:

li = LoadImage(root) 
filemenu.add_command(label = "Abrir", command = li.abrir) 

這是ALWA最好分開分開創建類。 下面是更正後的代碼:

from Tkinter import * 
import Image, ImageTk,tkFileDialog 

class LoadImage: 
    def __init__(self,root): 
     frame = Frame(root) 
     self.canvas = Canvas(frame,width=900,height=900) 
     self.canvas.pack() 
     frame.pack() 
     self.zoomcycle = 0 
     self.zimg_id = None 
     root.bind("<MouseWheel>", self.zoomer) 
     self.canvas.bind("<Motion>", self.crop) 

    def abrir(self): 
     File = "nlx019PQVT1sidekro1_540.jpg" 
     options={} 
     options['defaultextension']='.gif' 
     options['filetypes']=[('GIF','.gif'),('JPEG','.jpg'),('TIFF','.tif')] 
     options['title']='Select image file' 
     File=tkFileDialog.askopenfilename() 
     self.orig_img = Image.open(File) 
     self.img = ImageTk.PhotoImage(self.orig_img) 
     self.canvas.create_image(0,0,image=self.img, anchor="nw") 

    def zoomer(self,event): 
     if (event.delta > 0): 
      if self.zoomcycle != 4: self.zoomcycle += 1 
     elif (event.delta < 0): 
      if self.zoomcycle != 0: self.zoomcycle -= 1 
     self.crop(event) 

    def crop(self, event): 
     if self.zimg_id: self.canvas.delete(self.zimg_id) 
     if (self.zoomcycle) != 0: 
      x,y = event.x, event.y 
      if self.zoomcycle == 1: 
       tmp = self.orig_img.crop((x-45,y-30,x+45,y+30)) 
      elif self.zoomcycle == 2: 
       tmp = self.orig_img.crop((x-30,y-20,x+30,y+20)) 
      elif self.zoomcycle == 3: 
       tmp = self.orig_img.crop((x-15,y-10,x+15,y+10)) 
      elif self.zoomcycle == 4: 
       tmp = self.orig_img.crop((x-6,y-4,x+6,y+4)) 
      size = 300,200 
      self.zimg = ImageTk.PhotoImage(tmp.resize(size)) 
      self.zimg_id = self.canvas.create_image(event.x,event.y,image=self.zimg) 

root = Tk() 
root.title("Crop Test") 
menubar = Menu(root) 
filemenu = Menu(menubar, tearoff=0) 

li = LoadImage(root) 
filemenu.add_command(label = "Abrir", command = li.abrir) 
menubar.add_cascade(label = "Archivo", menu = filemenu) 
root.config(menu = menubar) 
root.mainloop() 

注:如果代碼的其餘部分作品我沒有檢查 - 但該文件公開問題在這裏解決:)

+0

我想從GUI分別實例化類函數(例如,li = LoadImage())。我的問題是菜單選項「LoadImage」出現在菜單之前。 OMG! – Sadae

+0

非常感謝! :DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD – Sadae