赋值前引用的 Tkinter 局部变量 'calcButton'

Tkinter local variable 'calcButton' referenced before assignment

我有这个代码:

#!/usr/bin/python
-*- coding: utf-8 -*-

from Tkinter import *
from ttk import Frame, Button, Style


class Example(Frame):

def __init__(self, parent):
    Frame.__init__(self, parent)   

    self.parent = parent

    self.initUI()

def initUI(self):

    self.parent.title("Multiplication")
    self.style = Style()
    self.style.theme_use("clam")

    self.pack(fill=BOTH, expand=1)

    E1 = Entry(bd =5)
    E1.place(x=0, y=0)
    E2 = Entry(bd=5)
    E2.place(x=125, y=0)
    E3 = Entry(bd=5)
    E3.place(x=62.5, y=25)
    calcButton = Button(self, text="Button", command=calcButton.calculate)
    calcButton.place(x=50, y=50)
def calculate(calcButton):
    a = E1.get()
    b = E2.get()
    c = E3.get()


def main():

    root = Tk()
    root.geometry("250x150+300+300")
    app = Example(root)
    root.mainloop()  


if __name__ == '__main__':
    main()

我收到了这个错误:

Traceback (most recent call last): File "E:\Python27\tkinter", line 48, in <module> main() File "E:\Python27\tkinter", line 43, in main app = Example(root) File "E:\Python27\tkinter", line 15, in __init__ self.initUI() File "E:\Python27\tkinter", line 31, in initUI calcButton = Button(self, text="Button", command=calcButton.calculate) UnboundLocalError: local variable 'calcButton' referenced before assignment

对于缩进不正确,我深表歉意,此代码粘贴很困难。 我查看了重复的问题并尝试了他们所说的内容,但没有任何效果。 任何帮助表示赞赏! 谢谢。

您在这一行中使用:

calcButton = Button(self, text="Button", command=calcButton.calculate)

调用 calcButton.calculate 将其分配给 calcButton 但尚未声明。

这应该是:

calcButton = Button(self, text="Button", self.calculate)