> Ah, right you are. I should know better than to post untested code, it's usually buggy! > > I think you need to do the same thing for add(), subtract(), multiply() and divide(). For some > reason I don't understand, for me add works from the keyboard and multiply doesn't!?
The same thing happened to me. That's when I realized that we were using 'x' for the text on the multiply button and binding 'x' to multiply(), whereas both of us were trying to use '*' to initiate multiply(). If I don't like the * in the text of the button, I'll have to set up a seperate binding. Here's the code again, a few things changed. -Added binding of spacebar to clear function -Added binding of enter key to equal - for use with the number off to the right of the keyboard. -Changed text on button so 'x' now reads '*' As always, Feel free to give suggestions. Thanks for helping out so much! Jacob Schmidt ###Start of Calculator.py### from __future__ import division from Tkinter import * class Application(Frame): def ctb(self): if self.shouldblank: self.distext.set('') self.shouldblank = False def adddigit(self, digit): self.ctb() self.distext.set(self.distext.get()+digit) def adddigitdot(self): if not self.distext.get().count('.'): self.ctb() self.distext.set(self.distext.get()+'.') def equal(self): if self.action: self.newnum = self.distext.get() self.newnum = str(eval(self.oldnum+self.action+self.newnum)) self.distext.set(self.newnum) self.oldnum = '0' self.action = '' self.shouldblank = True def add(self): self.handleOperator('+') def subtract(self): self.handleOperator('-') def multiply(self): self.handleOperator('*') def divide(self): self.handleOperator('/') def handleOperator(self, oper): if self.action: self.equal() self.oldnum = self.distext.get() self.action = oper else: self.oldnum = self.distext.get() self.action = oper self.shouldblank = True def clear(self): self.action = '' self.oldnum = '0' self.distext.set('0') self.shouldblank = True def memrecall(self): self.distext.set(self.memory) self.shouldblank = True def memminus(self): self.memory = str(eval(self.memory+"-"+self.distext.get())) self.shouldblank = True def memplus(self): self.memory = str(eval(self.memory+"+"+self.distext.get())) self.shouldblank = True def makeButton(self, text, command, row, column): button = Button(self,text=text,command=command,width=4,height=3) button.grid(row=row,column=column) if len(text) == 1: self.bind_all(text,lambda x: command()) def createWidgets(self): self.distext = StringVar() self.display =Entry(self,textvariable=self.distext,width=22,justify='right') self.display.grid(row=0,column=1,columnspan=4) self.makeButton(text='0',command=lambda *x: self.adddigit('0'),row=5,column=1) self.makeButton(text='1',command=lambda *x: self.adddigit('1'),row=4,column=1) self.makeButton(text='2',command=lambda *x: self.adddigit('2'),row=4,column=2) self.makeButton(text='3',command=lambda *x: self.adddigit('3'),row=4,column=3) self.makeButton(text='4',command=lambda *x: self.adddigit('4'),row=3,column=1) self.makeButton(text='5',command=lambda *x: self.adddigit('5'),row=3,column=2) self.makeButton(text='6',command=lambda *x: self.adddigit('6'),row=3,column=3) self.makeButton(text='7',command=lambda *x: self.adddigit('7'),row=2,column=1) self.makeButton(text='8',command=lambda *x: self.adddigit('8'),row=2,column=2) self.makeButton(text='9',command=lambda *x: self.adddigit('9'),row=2,column=3) self.makeButton(text='.',command=self.adddigitdot,row=5,column=2) self.makeButton(text="=",command=self.equal,row=5,column=3) self.makeButton(text='+',command=self.add,row=5,column=4) self.makeButton(text="-",command=self.subtract,row=4,column=4) self.makeButton(text='*',command=self.multiply,row=3,column=4) self.makeButton(text='/',command=self.divide,row=2,column=4) self.makeButton(text='ON/C',command=self.clear,row=1,column=4) self.makeButton(text='MRC',command=self.memrecall,row=1,column=1) self.makeButton(text="M-",command=self.memminus,row=1,column=2) self.makeButton(text="M+",command=self.memplus,row=1,column=3) def __init__(self, master=None): Frame.__init__(self,master) self.master.title("Calculator by Jacob, Inc.") self.pack(expand=True) self.bind_all('<space>',lambda x: self.clear()) self.bind_all('<Return>',lambda x: self.equal()) self.oldnum = '0' self.memory = '0' self.action = '' self.shouldblank = True self.createWidgets() app = Application() app.mainloop() ###End of Calculator.py### _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor