变量属性tkinter菜单问题

2024-09-30 04:29:15 发布

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

我从一个单独的类调用的变量化合物在调用它时不再有任何赋值,因此for循环没有任何迭代依据。我不知道如何引用变量(化合物),使其列表中仍有值

我已经尝试在CalculatorPage2类中创建一个单独的方法来确定选项,但是我也得到了一个属性错误。我相信这是因为我不能在不在__init__函数中引用self

该类包含菜单按钮初始化,它有更多,但我删除了它,以更简洁

class CalculatorPage2(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        Known_Menu = tk.Menubutton(self, text = 'Known compound:')
        Known_Menu.menu = tk.Menu(Known_Menu)
        Known_Menu["menu"] = Known_Menu.menu
        for i in Equation.compounds:
            Known_Menu.menu.add_command(label = Equation.compounds[i],
                                        command = lambda: print(i))
        Known_Menu.place(relx = 0.4, rely = 0.3)

包含复合变量的单独类中的初始化函数。我把你可以忽略的行注释掉了,只是留着以防万一

Class Equation():
    compounds = list()
    def __init__(equation):
        Equation.equation = equation
       #halves = equation.split(' = ')
       #reactant_half = halves[0]
       #Equation.reactants = reactant_half.split(' + ')
       #product_half = halves[1]
       #Equation.products = product_half.split(' + ')
        Equation.compounds = Equation.reactants + Equation.products
        GUI.show_frame(CalculatorPage2)

我希望菜单由Equation.components列表中的选项组成,但没有向菜单中添加任何选项。在它现在所处的状态下,不会出现错误消息,这是因为程序引用了Equation.components作为空列表,因此它没有任何迭代依据。为什么化合物列表是空的,我如何在这个结构中引用它,使它不是空的(从而将所述列表的值输入菜单按钮)


Tags: self列表init选项菜单tkmenuknown

热门问题