What am I doing wrong? I want the ‘refreshB’ button to call the ‘update’ def but I get a nameError instead
class monitor():
def update(self):
print "Called"
mon = Tk()
mainFrame = Frame(mon)
mainFrame.grid(row=1, column=1)
optionFrame = Frame(mainFrame)
optionFrame.grid(row=1, column=1)
refreshB = ttk.Button(optionFrame, text='Refresh', command=lambda: update('self') )
refreshB.grid(row=1, column=1)
mon.mainloop()
monitor()
**NameError: global name 'update' is not defined**
I an not very familiar with Classes, is there something else I am supposed to add?
If the script above was not a class then I would use:
refreshB = ttk.Button(optionFrame, text='Refresh', command=lambda: update )
Which would works fine…
Place all your initialization code inside an initialization function. Then refer to
update()asself.update().the
update()reference doesn’t work here because it’s an instancemethod and not a classmethod. Not because of the use of lambda–although I have no idea why you’d need to use a lambda function anyways. My solution involves you creating an instance ofMonitor. This is useful because it allows you to control when the code withinMonitoris executed. (Otherwise the code in your class body is executed at definition time. All callingmonitor()does is return an instance of the classmonitor–it doesn’t execute the body of the code)