Project calculator

Reply

Join Date: Aug 2005
Posts: 148
Reputation: Micko is on a distinguished road 
Solved Threads: 6
Micko Micko is offline Offline
Junior Poster

Project calculator

 
0
  #1
Nov 29th, 2005
Little, by little my calculator is getting a look I want.
However, I find that using Tkinter is not very intuitive, and i cannot find detail documentation.
have a look at this
  1. # menu-example-2.py
  2.  
  3. #from Tkinter import *
  4.  
  5. #root = Tk()
  6.  
  7. #def hello():
  8. #print "hello!"
  9.  
  10. ## create a toplevel menu
  11. #try:
  12. #menubar = Menu(root)
  13. #except: pass
  14. #menubar.add_command(label="Hello!", command=hello)
  15. #menubar.add_command(label="Quit!", command=root.quit)
  16.  
  17. ## display the menu
  18. #root.config(menu=menubar)
  19.  
  20. #mainloop()
  21.  
  22. from Tkinter import *
  23.  
  24. class CalcApp:
  25.  
  26. def __init__ ( self, parent ):
  27. #define menubar
  28. try:
  29. self.menubar = Menu ( parent )
  30. except: pass
  31. #create edit menu
  32. self.edit_menu = Menu ( self.menubar, tearoff = 0 )
  33. self.edit_menu.add_command ( label = "Copy", command = self.hello )
  34. self.edit_menu.add_command ( label = "Paste", command = self.hello )
  35. self.menubar.add_cascade ( label = "Edit", menu = self.edit_menu )
  36.  
  37. #create about
  38. self.menubar.add_command ( label = "About", command = self.hello )
  39. parent.config ( menu = self.menubar )
  40.  
  41. #define how main windwow will look
  42. parent.title ( "Calculator" )
  43. parent.geometry ( "255x228+0+0" )
  44. parent.resizable ( width = False, height = False )
  45.  
  46. try:
  47. self.fr_entry = Frame ( parent )#here, I tried to define height and width, but no luck
  48. except: pass
  49. self.fr_entry. pack ( )
  50. self.entry = Entry ( self.fr_entry, justify = "right", width = 45, relief = "groove" )
  51. self.entry.focus_force ( )
  52. self.entry.pack ( )
  53.  
  54.  
  55. def hello ( self ):
  56. print "hello"
  57.  
  58. root = Tk ( )
  59. calc = CalcApp ( root )
  60. root.mainloop ( )

Now I want to disable user to enter more than 20 numbers (numbers only) and I want to make Entry field a little more in height, just like win calcluator?
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 133
Reputation: mawe is an unknown quantity at this point 
Solved Threads: 58
mawe mawe is offline Offline
Junior Poster

Re: Project calculator

 
0
  #2
Nov 29th, 2005
Hi!

Originally Posted by Micko
I want to make Entry field a little more in height
An Entry has height = 1 per definition. If you want a bigger one, you'll have to use a Text-Widget.
Originally Posted by Micko
self.fr_entry = Frame ( parent )#here, I tried to define height and width, but no luck
A Frame is a Container-Widget. It is always as big, as the Widgets it contains.
Originally Posted by Micko
However, I find that using Tkinter is not very intuitive,
I hear this quite often. For me, Tkinter is the easiest GUI-Toolkit to use, but that's mostly a matter of taste.
Originally Posted by Micko
and i cannot find detail documentation.
No? Do you know this one? You find it when you just follow the links at www.python.org
Anyway, if you have questions, just post them here, we'll try our best to help you

Regards, mawe
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 148
Reputation: Micko is on a distinguished road 
Solved Threads: 6
Micko Micko is offline Offline
Junior Poster

Re: Project calculator

 
0
  #3
Nov 29th, 2005
Thanks for reply, I have downloaded "an Introduction to Tkinter" and I'll read it througly. Prviously, I used Thinking in Tkinter and it's tutorial, aslo I fond a lot of materials and tutorials, but I didn't find "official documentation" in which every widgets and it's properties are explained. Anyway, thank you. I guess I'll need to spend more time to read and learn about different widgets, and then try to use it, maybe it will be more efficient.
I used to read MSDN and find it very well organized when I learned basics of Win API and Socket programming, and i hoped there are something similar about Tkinter.

Cheers
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 133
Reputation: mawe is an unknown quantity at this point 
Solved Threads: 58
mawe mawe is offline Offline
Junior Poster

Re: Project calculator

 
0
  #4
Nov 29th, 2005
Originally Posted by Micko
I guess I'll need to spend more time to read and learn about different widgets, and then try to use it, maybe it will be more efficient.
Hm, don't know. There are so many snippets and programs using Tkinter. I learned most by reading and experimenting with this code.

Have a look at the german Python-Forum. In the Codesnippets and Showcase you'll find lots of Tkinter code, e.g. some useless games written by me This might give you an expression of what is possible with Tkinter and how it is done.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,009
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: 928
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Project calculator

 
0
  #5
Nov 29th, 2005
Micko, why do you want to use a Frame?
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 148
Reputation: Micko is on a distinguished road 
Solved Threads: 6
Micko Micko is offline Offline
Junior Poster

Re: Project calculator

 
0
  #6
Nov 29th, 2005
Well I start to learn about Tkinter from:
http://www.ferg.org/thinking_in_tkin..._programs.html
as you can they use Frame as mandatory component...
I think I'll take some time first to read and learn from "introduction to Tkinter" and then, I'll try to write actuall code...
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 133
Reputation: mawe is an unknown quantity at this point 
Solved Threads: 58
mawe mawe is offline Offline
Junior Poster

Re: Project calculator

 
0
  #7
Nov 29th, 2005
Using a Frame is ok, it makes the application more flexible. You could then easily switch between 2 or more Button-layouts (e.g. normal and scientific). All you have to do is create the two Frames and pack all the buttons into them. When you want to switch them, just use pack_forget() at the current one, and pack() on the other one. Voila!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 148
Reputation: Micko is on a distinguished road 
Solved Threads: 6
Micko Micko is offline Offline
Junior Poster

Re: Project calculator

 
0
  #8
Nov 29th, 2005
Unfortunately,after reading about Text widgets, I'm unable to make sure that user can enter only 20 digits and not letters or other nondigits into Text widgets, it's very complicated, so I guess, I'll try to switch to wxPython. At first glance it seems more intuitive.
Thank you for help mawe, I really appreciate it, but some things are just not meant to be...
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 133
Reputation: mawe is an unknown quantity at this point 
Solved Threads: 58
mawe mawe is offline Offline
Junior Poster

Re: Project calculator

 
0
  #9
Nov 29th, 2005
Originally Posted by Micko
I'm unable to make sure that user can enter only 20 digits and not letters or other nondigits into Text widgets, it's very complicated, so I guess, I'll try to switch to wxPython
Is this easy with wxPython?

In Tkinter I would probably do it like this:
Bind keypresses to the Text widget. If the pressed key is a digit and the text in the Text widget is shorter than 20, insert the digit. Otherwise do nothing.
Doesn't seem to be too complicated.

But as I said, choosing a GUI toolkit is a matter of taste. So try wxPython, maybe you like it better.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 148
Reputation: Micko is on a distinguished road 
Solved Threads: 6
Micko Micko is offline Offline
Junior Poster

Re: Project calculator

 
0
  #10
Nov 29th, 2005
Thank you for your effort.
Actually, this is what I manage to do so far. I assume it's possible to put a lot of this in loops, but since this is only very beginner's I think it's OK.
  1. from Tkinter import *
  2. from __future__ import division
  3.  
  4. class CalcApp:
  5.  
  6. def __init__ ( self, parent ):
  7. #define menubar
  8. try:
  9. self.menubar = Menu ( parent )
  10. except: pass
  11. #create edit menu
  12. self.edit_menu = Menu ( self.menubar, tearoff = 0 )
  13. self.edit_menu.add_command ( label = "Copy" )
  14. self.edit_menu.add_command ( label = "Paste" )
  15. self.menubar.add_cascade ( label = "Edit" )
  16.  
  17. #create about
  18. self.menubar.add_command ( label = "About" )
  19. parent.config ( menu = self.menubar )
  20.  
  21. #define how main windwow will look
  22. parent.title ( "Calculator" )
  23. parent.geometry ( "160x200+0+0" )
  24. parent.resizable ( width = False, height = False )
  25.  
  26. try:
  27. self.fr_entry = Frame ( parent )#here, I tried to define height and width, but no luck
  28. except: pass
  29. self.fr_entry. pack ( )
  30. self.entry = Entry ( self.fr_entry, justify = "right", width = 45, relief = "groove", borderwidth = 2 )
  31. self.entry.focus_force ( )
  32. self.entry.pack ( )
  33.  
  34. try:
  35. self.fr_but789 = Frame ( self.fr_entry )
  36. except: pass
  37. self.fr_but789. pack ( )
  38.  
  39. self.button7 = Button (self.fr_but789, text = "7" , width = 5, height = 2, command = self. insert_7 )
  40. self.button7.pack (side = LEFT )
  41. self.button8 = Button (self.fr_but789, text = "8" , width = 5, height = 2, command = self. insert_8 )
  42. self.button8.pack (side = LEFT )
  43. self.button9 = Button (self.fr_but789, text = "9" , width = 5, height = 2, command = self. insert_9)
  44. self.button9.pack (side = LEFT )
  45. self.button_div = Button (self.fr_but789, text = "/" , width = 5, height = 2, command = self. insert_div )
  46. self.button_div.pack (side = LEFT )
  47.  
  48. try:
  49. self.fr_but456 = Frame ( self.fr_entry )
  50. except: pass
  51. self.fr_but456. pack ( )
  52.  
  53. self.button4 = Button (self.fr_but456, text = "4" , width = 5, height = 2, command = self. insert_4 )
  54. self.button4.pack (side = LEFT )
  55. self.button5 = Button (self.fr_but456, text = "5" , width = 5, height = 2, command = self. insert_5 )
  56. self.button5.pack (side = LEFT )
  57. self.button6 = Button (self.fr_but456, text = "6" , width = 5, height = 2, command = self. insert_6)
  58. self.button6.pack (side = LEFT )
  59. self.button_mul = Button (self.fr_but456, text = "*" , width = 5, height = 2, command = self. insert_mul )
  60. self.button_mul.pack (side = LEFT )
  61.  
  62. try:
  63. self.fr_but123 = Frame ( self.fr_entry )
  64. except: pass
  65. self.fr_but123. pack ( )
  66.  
  67. self.button1 = Button (self.fr_but123, text = "1" , width = 5, height = 2, command = self. insert_1 )
  68. self.button1.pack (side = LEFT )
  69. self.button2 = Button (self.fr_but123, text = "2" , width = 5, height = 2, command = self. insert_2 )
  70. self.button2.pack (side = LEFT )
  71. self.button3 = Button (self.fr_but123, text = "3" , width = 5, height = 2, command = self. insert_3 )
  72. self.button3.pack (side = LEFT )
  73. self.button_sub = Button (self.fr_but123, text = "-" , width = 5, height = 2, command = self. insert_sub)
  74. self.button_sub.pack (side = LEFT )
  75.  
  76.  
  77. try:
  78. self.fr_but0 = Frame ( self.fr_entry )
  79. except: pass
  80. self.fr_but0. pack ( )
  81.  
  82. self.button0 = Button (self.fr_but0, text = "0" , width = 5, height = 2, command = self. insert_0 )
  83. self.button0.pack (side = LEFT )
  84. self.button_sign = Button (self.fr_but0, text = "+/-" , width = 5, height = 2, command = self. insert_sign )
  85. self.button_sign.pack (side = LEFT )
  86. self.button_point = Button (self.fr_but0, text = "." , width = 5, height = 2, command = self. insert_point )
  87. self.button_point.pack (side = LEFT )
  88. self.button_add = Button (self.fr_but0, text = "+" , width = 5, height = 2, command = self. insert_add )
  89. self.button_add.pack (side = LEFT )
  90.  
  91. try:
  92. self.fr_but_equal = Frame ( self.fr_entry )
  93. except: pass
  94. self.fr_but_equal. pack ( )
  95.  
  96. self.button_equal = Button (self.fr_but_equal, text = "=", width = 25, height = 2, command = self.evaluate )
  97. self.button_equal.pack (side = LEFT )
  98.  
  99.  
  100.  
  101. def insert_7 ( self ):
  102. self.entry.insert (END, "7")
  103. def insert_8 ( self ):
  104. self.entry.insert (END, "8")
  105. def insert_9 ( self ):
  106. self.entry.insert (END, "9")
  107. def insert_div ( self ):
  108. self.entry.insert (END, "/")
  109. def insert_4 ( self ):
  110. self.entry.insert (END, "4")
  111. def insert_5 ( self ):
  112. self.entry.insert (END, "5")
  113. def insert_6 ( self ):
  114. self.entry.insert (END, "6")
  115. def insert_mul ( self ):
  116. self.entry.insert (END, "*")
  117. def insert_1 ( self ):
  118. self.entry.insert (END, "1")
  119. def insert_2 ( self ):
  120. self.entry.insert (END, "2")
  121. def insert_3 ( self ):
  122. self.entry.insert (END, "3")
  123. def insert_sub ( self ):
  124. self.entry.insert (END, "-")
  125. def insert_0 ( self ):
  126. self.entry.insert (END, "0")
  127. def insert_sign ( self ):
  128. str = self.entry.get ( )
  129. if str == None:
  130. pass
  131. elif str != None and str[0] == "-":
  132. self.entry.delete (0, 1)
  133. else:
  134. self.entry.insert (ANCHOR, "-" )
  135. def insert_point ( self ):
  136. self.entry.insert(END, ".")
  137. def insert_add ( self ):
  138. self.entry.insert (END, "+")
  139. def evaluate ( self ):
  140. str = self.entry.get ( )
  141. try:
  142. x = eval ( str )
  143. except SyntaxError:
  144. print "Syntax Error!"
  145. return
  146. self.entry.delete ( 0, END )
  147. self.entry.insert (END, x )
  148.  
  149. root = Tk ( )
  150. calc = CalcApp ( root )
  151. root.mainloop ( )

I thought about similar solution, but I tried to figure out how to use all that tags and options in Text widgets, I think it's possible to achive this by only using options and tags, but...

I'm doing this only two days, maybe I need more time...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC