944,090 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 2724
  • Python RSS
Sep 25th, 2007
0

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

Expand Post »
Python Syntax (Toggle Plain Text)
  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:
Python Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster
chris99 is offline Offline
118 posts
since Sep 2006
Sep 25th, 2007
0

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

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
Reputation Points: 741
Solved Threads: 692
Nearly a Posting Maven
woooee is offline Offline
2,307 posts
since Dec 2006
Sep 27th, 2007
0

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

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
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Dec 14th, 2009
0
Re: What is wrong? Trying to make a simple calculator in Tkinter.
Click to Expand / Collapse  Quote originally posted by chris99 ...
Python Syntax (Toggle Plain Text)
  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:
Python Syntax (Toggle Plain Text)
  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.
Python Syntax (Toggle Plain Text)
  1. from Tkinter import *
  2.  
  3. def frame(root, side):
  4. w = Frame(root)
  5. w.pack(side=side, expand=YES, fill=BOTH)
  6. return w
  7.  
  8. def button(root, side, text, command=None):
  9. w = Button(root, text=text, command=command)
  10. w.pack(side=side, expand=YES, fill=BOTH)
  11. return w
  12.  
  13. class Calculator(Frame):
  14. def __init__(self):
  15. Frame.__init__(self)
  16. self.option_add('*Font', 'Verdana 12 bold')
  17. self.pack(expand=YES, fill=BOTH)
  18. self.master.title('Simple Calculator')
  19. self.master.iconname("calc1")
  20.  
  21. display = StringVar()
  22. Entry(self, relief=SUNKEN, textvariable=display).pack(side=TOP, expand=YES, fill=BOTH)
  23.  
  24. for key in ("123", "456", "789", "-0."):
  25. keyF = frame(self, TOP)
  26. for char in key:
  27. button(keyF, LEFT, char,
  28. lambda w=display, c=char: w.set(w.get() + c))
  29.  
  30. opsF = frame(self, TOP)
  31. for char in "+-*/=":
  32. if char == '=':
  33. btn = button(opsF, LEFT, char)
  34. btn.bind('<ButtonRelease-1>',
  35. lambda e, s=self, w=display: s.calc(w), '+')
  36. else:
  37. btn = button(opsF, LEFT, char,
  38. lambda w=display, s=' %s '%char: w.set(w.get()+s))
  39.  
  40. clearF = frame(self, BOTTOM)
  41. button(clearF, LEFT, 'Clr', lambda w=display: w.set(''))
  42.  
  43. def calc(self, display):
  44. try:
  45. display.set(eval(display.get()))
  46. except:
  47. display.set("ERROR")
  48.  
  49. if __name__ == '__main__':
  50. Calculator().mainloop()
Last edited by Nick Evan; Dec 16th, 2009 at 8:45 am. Reason: Added code-tags
Reputation Points: 10
Solved Threads: 0
Newbie Poster
aditri is offline Offline
3 posts
since Dec 2009
Dec 14th, 2009
0
Re: What is wrong? Trying to make a simple calculator in Tkinter.
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!
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: What is wrong? Please help! Python game
Next Thread in Python Forum Timeline: Range and Lists





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC