0

下面的代码应该在按下按钮时抛出指定的 tkMessagebox (例如,如果在选择项目之前按下 update_cost 按钮,它将抛出一个错误。但是,它目前仅在我按下 OK 后显示按下 update_cost 后的按钮。

谁能看到我做错了什么?干杯。(下面的代码示例)。

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  
4

1 回答 1

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 按钮的响应而被调用。

于 2012-10-11T14:26:32.043 回答