2012-10-11 26 views
0

下面的代碼應該在按下按鈕時拋出指定的tkMessagebox(例如,如果在選擇某個項目之前按下update_cost按鈕,將會拋出一個錯誤。但是,目前僅顯示出來後,我按update_cost後按OK按鈕。Python - 操作不正常

任何人都可以看到我做了什麼錯?乾杯。(下面的代碼示例)。

def update_name(self, new_name): 
    key=self.get_key_from_selection() 
    if key == None: 
     tkMessageBox.showerror("Update Name","No item selected.") 
    else: 
     self.products.get_item(key).set_name(new_name) 
     self.refresh_listbox() 


def update_cost(self, new_cost): 
    key=self.get_key_from_selection() 
    if key!=None: 
     item=self.products.get_item(key) 
     if item.get_type()==PART: 
      if new_cost.isdigit(): 
       item.set_cost(int(new_cost)) 
       self.refresh_listbox() 
      else: 
       tkMessageBox.showerror("Update Cost","Your input should be a number.") 
     else: 
      tkMessageBox.showerror("Update Cost","Action: 'Update_Cost' cannot be performed on a compound item.") 
    else: 
     tkMessageBox.showerror("Update Cost","No item selected.") 

def update_items(self, new_items_list): 
    key=self.get_key_from_selection() 
    if key!=None: 
     item=self.products.get_item(key) 
     if item.get_type()==COMPOUND: 
      # In order to use the internal methods of compound class to check the format and dependence, a temporary compound will recieve new list first before add to the products list.. 
      temp=Compound('temp','',self.products,[]) 
      try: 
       temp.set_items(new_items_list) 
      except: 
       tkMessageBox.showerror("Update Items","Invalid Items List.") 
       return 
      do=True 
      # Check all the items in the new list to ensure them not conflict any conditions below, or the add operation will not be done. 
      for sub_item in temp.get_depend(): 
       if sub_item not in self.products.get_keys(): 
        tkMessageBox.showerror("Update Items","There is at least one item not in the products list.") 
        do=False 
        break 
       elif sub_item==item.get_ID(): 
        tkMessageBox.showerror("Update Items","The item could not refer to the compound itself.") 
        do=False 
        break 
      if do: 
       item.set_items(new_items_list) 
       self.refresh_listbox() 
     else: 
      tkMessageBox.showerror("Update Items","Invalid Items List.") 
    else: 
     tkMessageBox.showerror("Update Items","No item selected.") 

def push_remove_item(self): 
    key=self.get_key_from_selection() 
    if key!=None: 
     if not self.products.check_depend(key): 
      self.products.remove_item(key)  
      self.refresh_listbox() 
     else: 
      tkMessageBox.showerror("Remove Item","Selected item contained in compound item.")  
    else: 
     tkMessageBox.showerror("Remove Item","No item selected.") 

# Controller part: 

class Controller(object): 
    # This class organise all the controllers in the window. 
    def __init__(self, window): 
     # Initialise the View object. 
     self.view=View(window) 
     self.view.pack(side=TOP, ipady=130) 

     # Initialise the menu. 
     self.menu_bar=Menu(window) 
     window['menu']=self.menu_bar 
     self.file_menu=Menu(self.menu_bar) 
     self.menu_bar.add_cascade(label='File', menu=self.file_menu) 
     self.file_menu.add_command(label='Open Products File', command=self.view.push_open_file) 
     self.file_menu.add_command(label='Save Products File', command=self.view.push_save_file) 
     self.file_menu.add_command(label='Exit', command=exit) 

     # Initialise the function buttons, using a Frame to container layout them. 
     self.control_bar=Frame(window, width=150) 
     self.control_bar.pack(side=TOP, pady=10) 
     self.add_part_button=Button(self.control_bar, text="Add Part", command=self.push_add_part) 
     self.add_part_button.pack(side=LEFT, padx=20, anchor=CENTER) 
     self.add_compound_button=Button(self.control_bar, text="Add Compound", command=self.push_add_compound) 
     self.add_compound_button.pack(side=LEFT, padx=20, anchor=CENTER) 
     self.update_name_button=Button(self.control_bar, text="Update Name", command=self.push_update_name) 
     self.update_name_button.pack(side=LEFT, padx=20, anchor=CENTER) 
     self.update_cost_button=Button(self.control_bar, text="Update Cost", command=self.push_update_cost) 
     self.update_cost_button.pack(side=LEFT, padx=20, anchor=CENTER) 
     self.update_items_button=Button(self.control_bar, text="Update Items", command=self.push_update_items) 
     self.update_items_button.pack(side=LEFT, padx=20, anchor=CENTER) 
     self.remove_item_button=Button(self.control_bar, text="Remove Item", command=self.view.push_remove_item) 
     self.remove_item_button.pack(side=LEFT, padx=20, anchor=CENTER) 

     # Initialise the entry area in the botttom, using a Frame to container layout them. 
     self.entry_area=Frame(window, width=150) 
     self.entry_area.pack(side=BOTTOM, pady=10) 
     self.status=Label(self.entry_area, width=20) 
     self.status.pack(side=LEFT, padx=0, anchor=CENTER) 
     self.entry=Entry(self.entry_area, width=80) 
     self.entry.pack(side=LEFT, padx=0, anchor=CENTER) 
     self.OK_button=Button(self.entry_area, text="OK", command=self.push_OK) 
     self.OK_button.pack(side=LEFT, padx=20, anchor=CENTER) 

    # The next 5 methods will be activated after their corresponded buttons are pushed, the actions of them are to change the commandID variable and label text. 
    # The function of this variable is to set the function statue before typing in the entry box. 

    def push_add_part(self): 
     self.commandID=ADD_PART 
     self.status.config(text="Add Part ID") 

    def push_add_compound(self): 
     self.commandID=ADD_COMPOUND 
     self.status.config(text="Add Compound ID") 

    def push_update_name(self): 
     self.commandID=UPDATE_NAME 
     self.status.config(text="Update Name") 

    def push_update_cost(self): 
     self.commandID=UPDATE_COST 
     self.status.config(text="Update Cost") 

    def push_update_items(self): 
     self.commandID=UPDATE_ITEMS 
     self.status.config(text="Update Compound Items")   

    # The operations setted beofre will only be done after OK button pushed. 
    def push_OK(self): 
     if self.commandID==ADD_PART: 
      self.view.add_part(self.entry.get()) 
     elif self.commandID==ADD_COMPOUND: 
      self.view.add_compound(self.entry.get()) 
     elif self.commandID==UPDATE_NAME: 
      self.view.update_name(self.entry.get()) 
     elif self.commandID==UPDATE_COST: 
      self.view.update_cost(self.entry.get()) 
     elif self.commandID==UPDATE_ITEMS: 
      self.view.update_items(self.entry.get()) 
     # Clear the label box. 
     self.status.config(text='') 
     # Clear the entry box. 
     self.entry.delete(0, END) 
     # Reset the command statue. 
     self.commandID=None 
+0

頂部是否有一些缺失的代碼?那些第一個函數看起來是一個類的方法。 – BobS

+1

您需要將此降低至_bare minimum_,以顯示您的問題。當你不需要通過這麼多不相干的東西時,關注這個問題就容易多了。如果您的示例包含足夠的代碼來運行,那更有用。 –

+0

據我所知,它實際上做了你告訴他要做的事情。當你按下其他任何按鈕時,然後按OK按鈕,例如update_cost,你只需設置commandID,但不能調用視圖中的任何方法,這實際上會執行驗證並引發錯誤。只有在push_OK方法內按下OK按鈕時,纔會調用View方法。 – andrean

回答

0

你已經爲你的「 update_cost「按鈕,如下所示:

self.update_cost_button=Button(..., command=self.push_update_cost) 
... 
def push_update_cost(self): 
    self.commandID=UPDATE_COST 
    self.status.config(text="Update Cost") 

請注意,push_update_cost()中沒有任何地方確實會調用會生成錯誤的代碼。該錯誤是由視圖上的update_cost方法生成的,但該方法僅從push_OK調用,而push_OK僅作爲對OK按鈕的響應進行調用。