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
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:
In addition, your indentation is wrong and must be corrected in order to work.
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
bvdet 2,851
Expert Mod 2GB
I was not joking. In the file you posted, the first three lines are: - Python 3.2.2 (default, Sep 4 2011, 09:51:08) [MSC v.1500 32 bit (Intel)] on win32
-
Type "copyright", "credits" or "license()" for more information.
-
>>> 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.
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
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! - from Tkinter import *
-
-
def frame(root, side):
-
w = Frame(root)
-
w.pack(side=side, expand=YES, fill=BOTH)
-
return w
-
-
def button(root, side, text, command=None):
-
w = Button(root, text=text, command=command)
-
w.pack(side=side, expand=YES, fill=BOTH)
-
return w
-
-
class Calculator(Frame):
-
def __init__(self):
-
Frame.__init__(self)
-
self.option_add('*Font', 'Verdana 12 bold')
-
self.pack(expand=YES, fill=BOTH)
-
self.master.title('Simple Calculator')
-
self.master.iconname("calc1")
-
-
display = StringVar()
-
displayEntry = Entry(self, relief=SUNKEN, textvariable=display)
-
displayEntry.pack(side=TOP, expand=YES, fill=BOTH)
-
-
for key in ("123", "456", "789", "-0."):
-
keyF = frame(self, TOP)
-
for char in key:
-
button(keyF, LEFT, char,
-
lambda w=display, c=char: w.set(w.get() + c))
-
-
opsF = frame(self, TOP)
-
for char in "+-*/=":
-
if char == '=':
-
btn = button(opsF, LEFT, char)
-
btn.bind('<ButtonRelease-1>',
-
lambda e, s=self, w=display: s.calc(w), '+')
-
else:
-
btn = button(opsF, LEFT, char,
-
lambda w=display, s=' %s '%char: w.set(w.get()+s))
-
-
clearF = frame(self, BOTTOM)
-
button(clearF, LEFT, 'Clr', lambda w=display: w.set(''))
-
-
def calc(self, display):
-
try:
-
display.set(str(eval(display.get())))
-
except:
-
display.set("ERROR")
-
-
if __name__ == '__main__':
-
Calculator().mainloop()
Dear bvdet,
many thanks for you to worry, but F5 and ImportError: No module named Tkinter ... :)
yours
bvdet 2,851
Expert Mod 2GB
If you are in Python 3, that would be: import tkinter
Dear bvdet,
thank you very much for your help!
yours
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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
|
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...
|
by: Tom |
last post by:
Hi
Is this a conditional ? what is the structure of the statement?
ch Tom
|
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...
|
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...
|
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
|
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. ...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |