从另一个函数Python中调用函数

时间:2016-12-19 19:40:08

标签: python python-2.7 python-3.x

我有一个类,我在__init__函数中进行了一些查询。在很多步骤中都需要这种查询,这就是为什么我有一个通用的Query_Execution函数。我无法打电话给它。我有另一个函数function 2进行一些计算并创建一个列表。我想在那里调用Query_Execution函数。我该怎么做呢?

class Something:

def __init__(self, master):

    self.top_frame = Frame(master, bg = 'green')
    self.top_frame.pack(fill = BOTH)
    self.middle_frame = Frame(master, bg = 'blue')
    self.middle_frame.pack(side = TOP, fill = BOTH)
    self.bottom_frame = Frame(master, bg = 'red')
    self.bottom_frame.pack(side=BOTTOM, fill = BOTH)

    # A generic function to create lists
    def Query_Execution(execution_string, selection_parameter):
        cursor.execute(execution_string)
        results = cursor.fetchall()
        result_list = [selection_parameter, 'All']
        for result in results:
            result_list.append(result[0])
        return result_list

    #The list of things in the first drop down
    execution_string = 'SELECT DISTINCT item_list FROM some db;'
    selection_paramter = 'item list'
    self.business_unit_list = Query_Execution(execution_string, selection_paramter)


    #This creates the drop down
    *The code to create Drop Down-Option Menu from tkinter*

    #The list of things in the States drop down
    execution_string = 'SELECT DISTINCT item_2 FROM some_db0;'
    selection_paramter = 'item 2'
    self.state_list = Query_Execution(execution_string, selection_paramter)

    #This creates the drop down
    *Code for creating drop down*


#Some Other function
def fucntion2(self, value):
    execution_string = 'SELECT DISTINCT item_3 FROM soem db WHERE item_1 = \'%s\' ;' %(self.var_business_unit.get())
    selection_parameter = 'Product Category'
    self.product_category_list = self.Query_Execution(execution_string, selection_parameter)    

*Here I want to call the Query_Execution function*

有更好的方法吗?

0 个答案:

没有答案