What is wrong? Trying to make a simple calculator in Tkinter.

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2006
Posts: 109
Reputation: chris99 is an unknown quantity at this point 
Solved Threads: 0
chris99's Avatar
chris99 chris99 is offline Offline
Junior Poster

What is wrong? Trying to make a simple calculator in Tkinter.

 
0
  #1
Sep 25th, 2007
  1. global Num1
  2. from Tkinter import *
  3. Num1 = ""
  4. def buttonNPush(n):
  5. global Num1
  6. Num1 = Num1+n
  7. print Num1
  8. def clearScr():
  9. global Num1
  10. Num1 = ""
  11.  
  12. root = Tk()
  13. textbox = Entry(root)
  14. textbox.pack(side=TOP)
  15. #Create Frame with buttons numbered 0-9
  16. frameButtons = Frame(root)
  17. frameButtons.pack(side=LEFT)
  18. button1 = Button(frameButtons, text="1", command=buttonNPush("1"))
  19. button1.grid(row=0, column=0)
  20. button2 = Button(frameButtons, text="2", command=buttonNPush("2"))
  21. button2.grid(row=0, column=1)
  22. button3 = Button(frameButtons, text="3", command=buttonNPush("3"))
  23. button3.grid(row=0, column=2)
  24. button4 = Button(frameButtons, text="4", command=buttonNPush("4"))
  25. button4.grid(row=1, column=0)
  26. button5 = Button(frameButtons, text="5", command=buttonNPush("5"))
  27. button5.grid(row=1, column=1)
  28. button6 = Button(frameButtons, text="6", command=buttonNPush("6"))
  29. button6.grid(row=1, column=2)
  30. button7 = Button(frameButtons, text="7", command=buttonNPush("7"))
  31. button7.grid(row=2, column=0)
  32. button8 = Button(frameButtons, text="8", command=buttonNPush("8"))
  33. button8.grid(row=2, column=1)
  34. button9 = Button(frameButtons, text="9", command=buttonNPush("9"))
  35. button9.grid(row=2, column=2)
  36. button0 = Button(frameButtons, text="0", command=buttonNPush("0"))
  37. button0.grid(row=3, column=0)
  38. buttonClear = Button(frameButtons, text="C", command=clearScr)
  39. buttonClear.grid(row=4, column=0)
  40. root.mainloop()

It just prints:
  1. 1
  2. 12
  3. 123
  4. 1234
  5. 12345
  6. 123456
  7. 1234567
  8. 12345678
  9. 123456789
  10. 1234567890

How do I make it wait until I press the buttons to add the numbers to the string. The buttons also don't run the event that they are meant to run.
I am just like Mandark. I am calm, constantly annoyed and consider everyone insects. And my laugh...

HAhahahaHAhahahahahaHAhahahaaaHAhahaahaa</mandark clone>
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,054
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 297
woooee woooee is offline Offline
Veteran Poster

Re: What is wrong? Trying to make a simple calculator in Tkinter.

 
0
  #2
Sep 25th, 2007
You want to add buttons for the functions to add, multiply, etc and tie that to the proper event. Right now, every button you push calls buttonNPush which prints the button. You also have to issue a get() to retrieve data. See this link for an example.
http://www.uselesspython.com/calculator.py
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,109
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 943
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: What is wrong? Trying to make a simple calculator in Tkinter.

 
0
  #3
Sep 27th, 2007
Two bad things stick out right away from your code:

1) Don't mix Tkinter geometry managers pack and grid within the same frame.

2) Command accepts a function reference not a function call. To pass an argument use lambda, a closure, or the new partial function (Python25).

Take a look at:
http://www.daniweb.com/code/snippet610.html

That snippet uses a lambda function to pass the argument (button value).
Last edited by vegaseat; Sep 27th, 2007 at 1:28 am. Reason: snippet
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Dec 2009
Posts: 2
Reputation: aditri is an unknown quantity at this point 
Solved Threads: 0
aditri aditri is offline Offline
Newbie Poster
 
0
  #4
2 Days Ago
Originally Posted by chris99 View Post
  1. global Num1
  2. from Tkinter import *
  3. Num1 = ""
  4. def buttonNPush(n):
  5. global Num1
  6. Num1 = Num1+n
  7. print Num1
  8. def clearScr():
  9. global Num1
  10. Num1 = ""
  11.  
  12. root = Tk()
  13. textbox = Entry(root)
  14. textbox.pack(side=TOP)
  15. #Create Frame with buttons numbered 0-9
  16. frameButtons = Frame(root)
  17. frameButtons.pack(side=LEFT)
  18. button1 = Button(frameButtons, text="1", command=buttonNPush("1"))
  19. button1.grid(row=0, column=0)
  20. button2 = Button(frameButtons, text="2", command=buttonNPush("2"))
  21. button2.grid(row=0, column=1)
  22. button3 = Button(frameButtons, text="3", command=buttonNPush("3"))
  23. button3.grid(row=0, column=2)
  24. button4 = Button(frameButtons, text="4", command=buttonNPush("4"))
  25. button4.grid(row=1, column=0)
  26. button5 = Button(frameButtons, text="5", command=buttonNPush("5"))
  27. button5.grid(row=1, column=1)
  28. button6 = Button(frameButtons, text="6", command=buttonNPush("6"))
  29. button6.grid(row=1, column=2)
  30. button7 = Button(frameButtons, text="7", command=buttonNPush("7"))
  31. button7.grid(row=2, column=0)
  32. button8 = Button(frameButtons, text="8", command=buttonNPush("8"))
  33. button8.grid(row=2, column=1)
  34. button9 = Button(frameButtons, text="9", command=buttonNPush("9"))
  35. button9.grid(row=2, column=2)
  36. button0 = Button(frameButtons, text="0", command=buttonNPush("0"))
  37. button0.grid(row=3, column=0)
  38. buttonClear = Button(frameButtons, text="C", command=clearScr)
  39. buttonClear.grid(row=4, column=0)
  40. root.mainloop()

It just prints:
  1. 1
  2. 12
  3. 123
  4. 1234
  5. 12345
  6. 123456
  7. 1234567
  8. 12345678
  9. 123456789
  10. 1234567890

How do I make it wait until I press the buttons to add the numbers to the string. The buttons also don't run the event that they are meant to run.
from Tkinter import *

def frame(root, side):
w = Frame(root)
w.pack(side=side, expand=YES, fill=BOTH)
return w

def button(root, side, text, command=None):
w = Button(root, text=text, command=command)
w.pack(side=side, expand=YES, fill=BOTH)
return w

class Calculator(Frame):
def __init__(self):
Frame.__init__(self)
self.option_add('*Font', 'Verdana 12 bold')
self.pack(expand=YES, fill=BOTH)
self.master.title('Simple Calculator')
self.master.iconname("calc1")

display = StringVar()
Entry(self, relief=SUNKEN, textvariable=display).pack(side=TOP, expand=YES, fill=BOTH)

for key in ("123", "456", "789", "-0."):
keyF = frame(self, TOP)
for char in key:
button(keyF, LEFT, char,
lambda w=display, c=char: w.set(w.get() + c))

opsF = frame(self, TOP)
for char in "+-*/=":
if char == '=':
btn = button(opsF, LEFT, char)
btn.bind('<ButtonRelease-1>',
lambda e, s=self, w=display: s.calc(w), '+')
else:
btn = button(opsF, LEFT, char,
lambda w=display, s=' %s '%char: w.set(w.get()+s))

clearF = frame(self, BOTTOM)
button(clearF, LEFT, 'Clr', lambda w=display: w.set(''))

def calc(self, display):
try:
display.set(eval(display.get()))
except:
display.set("ERROR")

if __name__ == '__main__':
Calculator().mainloop()
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,109
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 943
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite
 
0
  #5
1 Day Ago
Please don't bump up an old thread that has already been solved. It is confusing for the rest of us. Start your own thread if you need help!
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC