473,396 Members | 2,024 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Why it does not work?

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 dz4_18.zip (854 Bytes, 74 views)
Feb 24 '12 #1

✓ answered by bvdet

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

8 1818
bvdet
2,851 Expert Mod 2GB
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
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.
yours
Feb 26 '12 #3
bvdet
2,851 Expert Mod 2GB
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
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.
yours
Feb 26 '12 #5
bvdet
2,851 Expert Mod 2GB
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 *
  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.         displayEntry = Entry(self, relief=SUNKEN, textvariable=display)
  23.         displayEntry.pack(side=TOP, expand=YES, fill=BOTH)
  24.  
  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))
  30.  
  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))
  40.  
  41.         clearF = frame(self, BOTTOM)
  42.         button(clearF, LEFT, 'Clr', lambda w=display: w.set(''))
  43.  
  44.     def calc(self, display):
  45.         try:
  46.             display.set(str(eval(display.get())))
  47.         except:
  48.             display.set("ERROR")
  49.  
  50. if __name__ == '__main__':
  51.     Calculator().mainloop()
Feb 27 '12 #6
Dear bvdet,
many thanks for you to worry, but F5 and ImportError: No module named Tkinter ... :)
yours
Feb 27 '12 #7
bvdet
2,851 Expert Mod 2GB
If you are in Python 3, that would be:
import tkinter
Feb 27 '12 #8
Dear bvdet,
thank you very much for your help!
yours
Feb 28 '12 #9

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

Similar topics

5
by: Peter | last post by:
L.S. I am developing a PHP-login script (on Lycos Tripod) that uses Session to pass on variables. Below is the entire (stripped) structure that I use. It opens a page where you can Set and Read...
3
by: cv | last post by:
Hello All, I have used MultipartRequest like the following to upload images. MultipartRequest multi = new MultipartRequest(request, "../webapps/coreprogram/dealerlogos", 1024 * 1024); It...
4
by: Field | last post by:
Hi, the following snippet shows once executed this output: 2 2 I'd have rather expected this output: 2 10
4
by: Das | last post by:
Hi, I have made an application in ASP.net with C#. The application works fine with localhost. I have uploaded the site. I'm using web user controls in the form. but some of the button do not work...
7
by: Tom | last post by:
Hi Is this a conditional ? what is the structure of the statement? ch Tom
6
by: Chris Anderson | last post by:
Anyone know of a fix (ideally) or an easy workaround to the problem of escape characters not working in regex replacement text? They just come out as literal text For example, you'd think that thi...
10
by: Antoine | last post by:
I can't work out what is causing this problem. Can anyone suggest what the typical causes beyond the obvious might be? Could you get it with datasets? Maybe I should run in debug mode and test...
5
by: Jason | last post by:
I've got a small form (400 X 310) and I want it to start up in the lower right corner of the screen. Is there a way I can easily do this? Thanks
1
by: Newbie in ChiTown | last post by:
Here's my code: I am using MS Access and I am trying to update a table (InvoiceDetails) with data input by the user on a form. However, it does not update nor does it give me an error message. ...
11
by: Jim | last post by:
Hi, I want to schedule a Python program that reads the command line for input. However, when adding an argument to the command line Python will not pick it up when using Windows scheduled...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.