By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,674 Members | 1,258 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,674 IT Pros & Developers. It's quick & easy.

Why it does not work?

P: 5
Dear friends,
I took this code from the Python and Tkinter Programming book by JOHN E. GRAYSON. A calculator example from there.
Why it does not work?
Yours Foggylake
Attached Files
File Type: zip (854 Bytes, 53 views)
Feb 24 '12 #1

✓ answered by bvdet

If you are in Python 3, that would be:
import tkinter

Share this Question
Share on Google+
8 Replies

Expert Mod 2.5K+
P: 2,851
I'll bet you received a syntax error. The first three lines are not valid code. Remove the first two lines and modify the third line to look like:
Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
In addition, your indentation is wrong and must be corrected in order to work.
Feb 25 '12 #2

P: 5
Sorry, dear bvdet
, but what do you mean saying of "my indentation"? And are you joking about cleaning def frame(root, side):
w = Frame(root)? Please,be kind and correct the code in the file for to work and put the last one, please.
I am a pupil.
Feb 26 '12 #3

Expert Mod 2.5K+
P: 2,851
I was not joking. In the file you posted, the first three lines are:
Expand|Select|Wrap|Line Numbers
  1. Python 3.2.2 (default, Sep  4 2011, 09:51:08) [MSC v.1500 32 bit (Intel)] on win32
  2. Type "copyright", "credits" or "license()" for more information.
  3. >>> from Tkinter import *
They are not valid code and will produce a syntax error.

Python uses whitespace indentation to delimit blocks of code. The indentation of the code you posted appears to be incorrect. For example, in the class definition Calculator, you are creating frames and buttons, but there is no indentation whatsoever (for key in and for char in). Maybe you should go back to where you copied the code and see what the indentation is supposed to look like. Before that, maybe you should learn more about Python indentation. Python: Myths about Indentation should help.
Feb 26 '12 #4

P: 5
Dear bvdet,
many thanks for the link and I got of the first two lines/
Could you repair that file for to work, because the code was written in the book by Guru and I can not understand where is mistake.
Seeing the result of your repairing can be better for me to learn.
Test it working, please.
I am a pupil.
Feb 26 '12 #5

Expert Mod 2.5K+
P: 2,851
Don't use being a pupil as an excuse. There is a lot of code posted on the internet that you could study to better understand indentation.

I found a GUI calculator script I saved two years ago that I got off the internet (author unknown). Instead of trying to fix your indentation errors, I'll post it here. As luck would have it, it's the same code as yours!
Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  3. def frame(root, side): 
  4.     w = Frame(root)
  5.     w.pack(side=side, expand=YES, fill=BOTH)
  6.     return w
  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
  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")
  21.         display = StringVar()
  22.         displayEntry = Entry(self, relief=SUNKEN, textvariable=display)
  23.         displayEntry.pack(side=TOP, expand=YES, fill=BOTH)
  25.         for key in ("123", "456", "789", "-0."):
  26.             keyF = frame(self, TOP)
  27.             for char in key:
  28.                 button(keyF, LEFT, char,
  29.                        lambda w=display, c=char: w.set(w.get() + c))
  31.         opsF = frame(self, TOP)
  32.         for char in "+-*/=":
  33.             if char == '=':
  34.                 btn = button(opsF, LEFT, char)
  35.                 btn.bind('<ButtonRelease-1>',
  36.                          lambda e, s=self, w=display: s.calc(w), '+')
  37.             else:
  38.                 btn = button(opsF, LEFT, char,
  39.                    lambda w=display, s=' %s '%char: w.set(w.get()+s))
  41.         clearF = frame(self, BOTTOM)
  42.         button(clearF, LEFT, 'Clr', lambda w=display: w.set(''))
  44.     def calc(self, display):
  45.         try:
  46.             display.set(str(eval(display.get())))
  47.         except:
  48.             display.set("ERROR")
  50. if __name__ == '__main__':
  51.     Calculator().mainloop()
Feb 27 '12 #6

P: 5
Dear bvdet,
many thanks for you to worry, but F5 and ImportError: No module named Tkinter ... :)
Feb 27 '12 #7

Expert Mod 2.5K+
P: 2,851
If you are in Python 3, that would be:
import tkinter
Feb 27 '12 #8

P: 5
Dear bvdet,
thank you very much for your help!
Feb 28 '12 #9

Post your reply

Sign in to post your reply or Sign up for a free account.