【问题标题】:using entry from a class in another classes function在另一个类函数中使用一个类的条目
【发布时间】:2013-05-15 10:01:28
【问题描述】:

变量函数是通过类函数框架给出的。尝试在 add_function 中使用它时出现此错误。

AttributeError: 类 FunctionFrame 没有属性'function'

class FunctionFrame(Frame):
    """a simple application to allow a user ti enter an
    expressio
    n and evaluate it
    """
    def __init__(self, master):
        """
        a a=simple expression evaluator
        """
        Frame.__init__(self, master, relief=SUNKEN, bg='#A5A5A5', pady=3)
        Label(self, text='Function in x: ', bg='#A5A5A5').pack(side=LEFT)
        function = Entry(self, width=35).pack(side=LEFT, padx=2)
        Button(self, text='Select', command=self.select).pack(side=RIGHT, padx=4)
        colour = Entry(self, width=15).pack(side=RIGHT)
        Label(self, text='Function Colour: ', bg='#A5A5A5').pack(side=RIGHT, padx=2)

    def select(self):
        (rgb, hx)= askcolor()

class ButtonFrame(Frame):
    """a simple application to allow a user ti enter an
    expression and evaluate it
    """
    def __init__(self, master):
        """
        a a=simple expression evaluator
        """
        Frame.__init__(self, master, bg='#CECEF6')
        Button(self, text='Add Function', command=self.add_function).pack(side=LEFT)
        Button(self, text='Redraw All', command=self.redraw_all).pack(side=LEFT)
        Button(self, text='Remove Last Function', command=self.remove_last).pack(side=LEFT)
        Button(self, text='Remove All Functions', command=self.remove_all).pack(side=LEFT)
        Button(self, text='Exit', command=self.exit_app).pack(side=LEFT)


    def add_function(self):
        make_function(FunctionFrame.function)

【问题讨论】:

  • 'function' 作为属性/变量名在任何语言中似乎都是一个危险的选择

标签: python python-2.7 tkinter


【解决方案1】:

function 被定义为__init__ 内部的局部变量:

def __init__(self, master):
    function = Entry(self, width=35).pack(side=LEFT, padx=2)

要在__init__ 之外使用function,您需要将其设为实例属性:

def __init__(self, master):
    self.function = Entry(self, width=35).pack(side=LEFT, padx=2)

然后,在ButtonFrame 中,您需要创建FunctionFrame实例

def add_function(self):
    make_function(FunctionFrame(self).function)

【讨论】:

    【解决方案2】:

    您需要在实例上设置 function

    self.function = Entry(self, width=35).pack(side=LEFT, padx=2)
    

    没有self.function 只是您的__init__ 方法中的一个本地名称,并在该方法完成时被丢弃。

    您可能想对colour 做同样的事情。

    【讨论】:

      【解决方案3】:

      FunctionFrame.function 未在您的代码中定义。

      快速解决方法是:

      class FunctionFrame(Frame):
      
          def __init__(self, master):
              ...
              self.function = Entry(self, width=35).pack(side=LEFT, padx=2)
              ...
      
      
      
      class ButtonFrame(Frame):
          ...
          def add_function(self):
              make_function(FunctionFrame(self.master).function)
      

      不确定如何访问 Frame 的主属性

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-02-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-10
        相关资源
        最近更新 更多