如何运行接受变量作为参数的方法?

时间:2018-12-15 03:47:26

标签: python-3.x oop visual-studio-code

下面,我有主类grid_gui和两个创建gui对象的子类。我的代码的问题在于,两个对象gui的创建按钮运行一个需要接受参数的命令。但是我不确定如何在主类中格式化我的method_to_run参数,以便获得这些参数。每次我运行下面的代码时,它都告诉我,在两个子类中,我都必须在要在其 init 方法中运行的方法中放入一个参数。但是,我需要在主类 init 函数中计算该参数。我曾尝试将变量放入子类 init 方法中,但由于我无法在调用主类之前定义变量,因此它从未起作用。我在StackOverflow和其他网站上到处都看过,但是没有一个给我有用的答案。任何反馈将不胜感激。

class grid_gui(Frame):
        def __init__(self, rownumber, columnnumber, color_pattern, button_names, method_to_run):
        self.rownumber = rownumber 
        self.columnnumber = columnnumber 
        self.color_pattern = color_pattern 
        self.button_names = button_names 
        self.buttons = [] 
        ### --- Window --- ###
            # Get Screen Dimensions #
        screen_width = root.winfo_screenwidth()
        screen_height = root.winfo_screenheight()
            # Get GUI Dimensions #
        width = screen_width
        height = screen_height/2
            # Center the GUI #
        posx = (screen_width/2) - (width/2)
        posy = 0
            # Place the GUI #
        root.geometry('%dx%d+%d+%d' % (width, height, posx, posy))
        root.resizable(False, False)
            # Configure the rows and columns #
                # Rows #
        for y in range(self.rownumber):
            root.rowconfigure(y, weight=1)
                # Columns #
        for x in range(self.columnnumber):
            root.columnconfigure(x, weight=1)

        ### --- Buttons --- ###
        for j in range(len(self.columnnumber)):
            self.buttons.append([])
            for i in range(self.rownumber):
                index = len(self.buttons)
                self.buttons[j].append(Button(root, text=self.button_names[index], bg=self.color_pattern[index%3], fg='white', command=lambda: method_to_run(index + 1)))
                self.buttons[j][i].grid(row=i, column=j, sticky=N+E+S+W)

class choice_2_gui(grid_gui):
    def __init__(self): # Create the choice 2 GUI
        grid_gui.__init__(3, 6, ['red', 'green', 'blue'], ['AREA OF PARK', 'POPULATION YEAR', 'SURVEY DATE', 'SPECIES NAME', 'UNKNOWN AGE AND SEX COUNT', 'ADULT MALE COUNT', 'ADULT FEMALE COUNT', 'ADULT UNKNOWN COUNT', 'YEARLING COUNT', 'CALF COUNT', 'SURVEY TOTAL', 'SIGHTABILITY CORRECTION FACTOR', 'ADDITIONAL CAPTIVE COUNT', 'ANIMALS REMOVED', 'FALL POPULATION ESTIMATE', 'SURVEY COMMENT', 'ESTIMATE METHOD', 'SAVE'], choice_2_gui.choice_2_input_choice_input())

    @staticmethod
    def choice_2_input_choice_input(var):
        global choice_2_choice
        choice_2_choice = var
        root.destroy()

class choice_3_gui(grid_gui):
    def __init__(self): # Create the choice 3 GUI #
        grid_gui.__init__(3, 5, ['red', 'green', 'blue'], ['SURVEY DATE', 'UNKNOWN AGE AND SEX COUNT', 'ADULT MALE COUNT', 'ADULT FEMALE COUNT', 'ADULT UNKNOWN COUNT', 'YEARLING COUNT', 'CALF COUNT', 'SURVEY TOTAL', 'SIGHTABILITY CORRECTION FACTOR', 'ADDITIONAL CAPTIVE COUNT', 'ANIMALS REMOVED PRIOR TO SURVEY', 'FALL POPULATION ESTIMATE', 'SURVEY COMMENT', 'ESTIMATE METHOD', 'EXIT'], choice_3_gui.option_3_index_choice())

    @staticmethod
    def option_3_index_choice(var):
        global option_3_choice
        option_3_choice = var
        root.destroy()

0 个答案:

没有答案