from tkinter import * class Calculator(Frame): def __init__(self, parent): Frame.__init__(self, parent) self.parent = parent print("11111") self.setUI() def button_clicked(self, event): print("event") self.output.delete("0.0","end") self.output.insert("0.0","sdfdsfsd") def setUI(self): print("UI") self.pack(fill=BOTH, expand=1) self.output=Text(self, height=1, width=25) self.output.grid(row=0, column=0, columnspan = 5) #i=0 #currow=1 #while i<=9: # if i>=5: # currow=2 # btn = Button(self, text=i, width=3) # btn.grid(row=currow, column=i % 5) # btn.bind("Button-",self.button_clicked) # i=i+1 btn = Button(self, text="Кнопка", width=10, command=self.button_clicked) btn.grid(row=1, column=0) #btn.bind("Button-1",self.button_clicked) print("end UI") def main(): root = Tk() root.geometry("1500x900") #root.textvar=root.StringVar() app = Calculator(root) root.mainloop() if __name__ == "__main__": print("start") main()
При нажатии на кнопочку вылазит сообщение об ошибке:
Traceback (most recent call last):
File “D:\Самообразование\Питон\Calk1.py”, line 41, in <module>
main()
File “D:\Самообразование\Питон\Calk1.py”, line 36, in main
app = Calculator(root)
File “D:\Самообразование\Питон\Calk1.py”, line 8, in __init__
self.setUI()
File “D:\Самообразование\Питон\Calk1.py”, line 27, in setUI
btn = Button(self, text=“Кнопка”, width=10, command=self.button_clicked(btn))
UnboundLocalError: local variable ‘btn’ referenced before assignment
Подскажите, пожалуйста, что делаю не так?