从另一个functionPython内部调用函数

2024-03-29 12:24:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个类,在这个类中,我在__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*

有没有更好的办法?你知道吗


Tags: the函数selfstringfunctionresultitemquery