2015-01-15 4 views
0

У меня есть этот код:Tkinter локальная переменная 'calcButton' обращаться до присвоения

#!/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

Я извиняюсь за некорректное отступа, этот код вставки трудно. Я рассмотрел повторяющиеся вопросы и попробовал то, что они сказали, но ничего не получилось. Любая помощь приветствуется! Спасибо.

+2

Я думаю, что вы хотите 'команду = self.calculate' вместо команды' = calcButton.calculate'. –

+0

положите это как ответ –

+0

Я слишком ленив, соглашаюсь с ответом Ганеша Камата. –

ответ

2

в этой линии вы используете:

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

где calcButton.calculate вызывается, чтобы назначить его calcButton, но пока не объявлена.

Это, вероятно, следует:

calcButton = Button(self, text="Button", self.calculate) 
Смежные вопросы