| | |
Project calculator
![]() |
•
•
Join Date: Aug 2005
Posts: 148
Reputation:
Solved Threads: 6
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
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?
However, I find that using Tkinter is not very intuitive, and i cannot find detail documentation.
have a look at this
Python Syntax (Toggle Plain Text)
# menu-example-2.py #from Tkinter import * #root = Tk() #def hello(): #print "hello!" ## create a toplevel menu #try: #menubar = Menu(root) #except: pass #menubar.add_command(label="Hello!", command=hello) #menubar.add_command(label="Quit!", command=root.quit) ## display the menu #root.config(menu=menubar) #mainloop() from Tkinter import * class CalcApp: def __init__ ( self, parent ): #define menubar try: self.menubar = Menu ( parent ) except: pass #create edit menu self.edit_menu = Menu ( self.menubar, tearoff = 0 ) self.edit_menu.add_command ( label = "Copy", command = self.hello ) self.edit_menu.add_command ( label = "Paste", command = self.hello ) self.menubar.add_cascade ( label = "Edit", menu = self.edit_menu ) #create about self.menubar.add_command ( label = "About", command = self.hello ) parent.config ( menu = self.menubar ) #define how main windwow will look parent.title ( "Calculator" ) parent.geometry ( "255x228+0+0" ) parent.resizable ( width = False, height = False ) try: self.fr_entry = Frame ( parent )#here, I tried to define height and width, but no luck except: pass self.fr_entry. pack ( ) self.entry = Entry ( self.fr_entry, justify = "right", width = 45, relief = "groove" ) self.entry.focus_force ( ) self.entry.pack ( ) def hello ( self ): print "hello" root = Tk ( ) calc = CalcApp ( root ) 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?
•
•
Join Date: Sep 2005
Posts: 133
Reputation:
Solved Threads: 58
Hi!
An Entry has height = 1 per definition. If you want a bigger one, you'll have to use a Text-Widget.
A Frame is a Container-Widget. It is always as big, as the Widgets it contains.
I hear this quite often. For me, Tkinter is the easiest GUI-Toolkit to use, but that's mostly a matter of taste.
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
•
•
•
•
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
•
•
•
•
Originally Posted by Micko
However, I find that using Tkinter is not very intuitive,
•
•
•
•
Originally Posted by Micko
and i cannot find detail documentation.

Anyway, if you have questions, just post them here, we'll try our best to help you

Regards, mawe
•
•
Join Date: Aug 2005
Posts: 148
Reputation:
Solved Threads: 6
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
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
•
•
Join Date: Sep 2005
Posts: 133
Reputation:
Solved Threads: 58
•
•
•
•
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.
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. •
•
Join Date: Aug 2005
Posts: 148
Reputation:
Solved Threads: 6
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...
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...
•
•
Join Date: Sep 2005
Posts: 133
Reputation:
Solved Threads: 58
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!
•
•
Join Date: Aug 2005
Posts: 148
Reputation:
Solved Threads: 6
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...
Thank you for help mawe, I really appreciate it, but some things are just not meant to be...
•
•
Join Date: Sep 2005
Posts: 133
Reputation:
Solved Threads: 58
•
•
•
•
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
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.
•
•
Join Date: Aug 2005
Posts: 148
Reputation:
Solved Threads: 6
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.
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...
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.
Python Syntax (Toggle Plain Text)
from Tkinter import * from __future__ import division class CalcApp: def __init__ ( self, parent ): #define menubar try: self.menubar = Menu ( parent ) except: pass #create edit menu self.edit_menu = Menu ( self.menubar, tearoff = 0 ) self.edit_menu.add_command ( label = "Copy" ) self.edit_menu.add_command ( label = "Paste" ) self.menubar.add_cascade ( label = "Edit" ) #create about self.menubar.add_command ( label = "About" ) parent.config ( menu = self.menubar ) #define how main windwow will look parent.title ( "Calculator" ) parent.geometry ( "160x200+0+0" ) parent.resizable ( width = False, height = False ) try: self.fr_entry = Frame ( parent )#here, I tried to define height and width, but no luck except: pass self.fr_entry. pack ( ) self.entry = Entry ( self.fr_entry, justify = "right", width = 45, relief = "groove", borderwidth = 2 ) self.entry.focus_force ( ) self.entry.pack ( ) try: self.fr_but789 = Frame ( self.fr_entry ) except: pass self.fr_but789. pack ( ) self.button7 = Button (self.fr_but789, text = "7" , width = 5, height = 2, command = self. insert_7 ) self.button7.pack (side = LEFT ) self.button8 = Button (self.fr_but789, text = "8" , width = 5, height = 2, command = self. insert_8 ) self.button8.pack (side = LEFT ) self.button9 = Button (self.fr_but789, text = "9" , width = 5, height = 2, command = self. insert_9) self.button9.pack (side = LEFT ) self.button_div = Button (self.fr_but789, text = "/" , width = 5, height = 2, command = self. insert_div ) self.button_div.pack (side = LEFT ) try: self.fr_but456 = Frame ( self.fr_entry ) except: pass self.fr_but456. pack ( ) self.button4 = Button (self.fr_but456, text = "4" , width = 5, height = 2, command = self. insert_4 ) self.button4.pack (side = LEFT ) self.button5 = Button (self.fr_but456, text = "5" , width = 5, height = 2, command = self. insert_5 ) self.button5.pack (side = LEFT ) self.button6 = Button (self.fr_but456, text = "6" , width = 5, height = 2, command = self. insert_6) self.button6.pack (side = LEFT ) self.button_mul = Button (self.fr_but456, text = "*" , width = 5, height = 2, command = self. insert_mul ) self.button_mul.pack (side = LEFT ) try: self.fr_but123 = Frame ( self.fr_entry ) except: pass self.fr_but123. pack ( ) self.button1 = Button (self.fr_but123, text = "1" , width = 5, height = 2, command = self. insert_1 ) self.button1.pack (side = LEFT ) self.button2 = Button (self.fr_but123, text = "2" , width = 5, height = 2, command = self. insert_2 ) self.button2.pack (side = LEFT ) self.button3 = Button (self.fr_but123, text = "3" , width = 5, height = 2, command = self. insert_3 ) self.button3.pack (side = LEFT ) self.button_sub = Button (self.fr_but123, text = "-" , width = 5, height = 2, command = self. insert_sub) self.button_sub.pack (side = LEFT ) try: self.fr_but0 = Frame ( self.fr_entry ) except: pass self.fr_but0. pack ( ) self.button0 = Button (self.fr_but0, text = "0" , width = 5, height = 2, command = self. insert_0 ) self.button0.pack (side = LEFT ) self.button_sign = Button (self.fr_but0, text = "+/-" , width = 5, height = 2, command = self. insert_sign ) self.button_sign.pack (side = LEFT ) self.button_point = Button (self.fr_but0, text = "." , width = 5, height = 2, command = self. insert_point ) self.button_point.pack (side = LEFT ) self.button_add = Button (self.fr_but0, text = "+" , width = 5, height = 2, command = self. insert_add ) self.button_add.pack (side = LEFT ) try: self.fr_but_equal = Frame ( self.fr_entry ) except: pass self.fr_but_equal. pack ( ) self.button_equal = Button (self.fr_but_equal, text = "=", width = 25, height = 2, command = self.evaluate ) self.button_equal.pack (side = LEFT ) def insert_7 ( self ): self.entry.insert (END, "7") def insert_8 ( self ): self.entry.insert (END, "8") def insert_9 ( self ): self.entry.insert (END, "9") def insert_div ( self ): self.entry.insert (END, "/") def insert_4 ( self ): self.entry.insert (END, "4") def insert_5 ( self ): self.entry.insert (END, "5") def insert_6 ( self ): self.entry.insert (END, "6") def insert_mul ( self ): self.entry.insert (END, "*") def insert_1 ( self ): self.entry.insert (END, "1") def insert_2 ( self ): self.entry.insert (END, "2") def insert_3 ( self ): self.entry.insert (END, "3") def insert_sub ( self ): self.entry.insert (END, "-") def insert_0 ( self ): self.entry.insert (END, "0") def insert_sign ( self ): str = self.entry.get ( ) if str == None: pass elif str != None and str[0] == "-": self.entry.delete (0, 1) else: self.entry.insert (ANCHOR, "-" ) def insert_point ( self ): self.entry.insert(END, ".") def insert_add ( self ): self.entry.insert (END, "+") def evaluate ( self ): str = self.entry.get ( ) try: x = eval ( str ) except SyntaxError: print "Syntax Error!" return self.entry.delete ( 0, END ) self.entry.insert (END, x ) root = Tk ( ) calc = CalcApp ( root ) 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...
![]() |
Other Threads in the Python Forum
- Previous Thread: A problem/Bug with libglade
- Next Thread: Creating Forms with Boa
| Thread Tools | Search this Thread |
address aliased anydbm app bash beginner bits calling casino changecolor cipher clear conversion coordinates corners count cturtle curves definedlines development dictionary digital dynamic events examples excel external feet file float format function gui handling hints homework iframe images import input java keycontrol line linux list lists loan loop matching mouse multiple number numbers output parsing path port prime programming projects py py2exe pygame pymailer python random rational raw_input recursion recursive scrolledtext searchingfile shebang signal singleton split string strings tails terminal text threading time tlapse tooltip tuple tutorial type ubuntu unicode url urllib urllib2 valueerror variable web-scrape whileloop word wxpython xlwt






