Ok new problem, I want to build off of a program I saw in a tutorial that has a "menu" and can calculate two different shapes. The two shapes are circle and rectangle. I've built it into a working order.(event bindings, buttons, area calculation programs) The program works all the way up to the done button(the button that begins calculation and prints it in a label) but when I click on the button it brings up a error message in the IDLE shell prompt that says that I can't do the following:

This is a snippet of the script to get you orented:

def answerSquare(event):
ansLabel = Label(root)
ansLabel.configure(text=' ')
ansLabel.configure(text='The area of your square is' + squareHight.get() * squareWidth.get()
^^both of these are entry fields later in the script and have global assigned to them.

Help appreciated :)

Recommended Answers

All 9 Replies

At first glance it looks like you are trying to multiply two strings. This is what you get from an entry field. Put a float() around each.

could you give me an example on the float() thing ive never seen it before.

Do I just do this?

float(squareHight.get())

if so what exactly does float do?

Hi danizzil14,

You'll need to do:

ansLabel.configure(text='The area of your square is' + \
(float(squareHight.get()) * float(squareWidth.get())))

This should work.

Why doesn't squareHight.get() work?

I'm guessing that squareHight and squareWidth are both StringVars, correct? If that is the case, then squareHight.get() and squareWidth.get() both resolve to string values, and the * operator doesn't know what to do with two string values, so it gives you an error message like 'TypeError: can't multiply sequence by non-int'. To make this work, you need to transform the two string values into two floating-point values using the float() function. That's the short answer.

Now for the long answer:

Generally speaking (and this is a simplification), a value is a simplified expression of data. For example, in the following code:

a = 13
b = 3.1415
c = "stick"

...a, b, and c are variables, and 13, 3.1415, and "stick" are values. Python distinguishes between different kinds of values. The number 13 is an integer value (for our purposes, this means whole numbers), the number 3.1415 is a floating-point value (for our purposes, this means it has a decimal point and a remainder), and the word "stick" is a string value (for our purposes, this means it is a string of characters joined sequentially). Here is another instructive example:

d = 21
e = "21"

The variables d and e do not share the same value - d has been assigned the integer value 21 and e has been assigned the string value "21". As far as Python is concerned, these are different, even though human beings such as you or I can tell that they mean essentially the same thing.

Now, part two: in many programming languages, you can design a function to work only with certain kinds of values. For example, suppose that we want to make a factorial() function for calculating the factorial of integers in Python. It might look something like this:

# Recall that factorial(n) = n*(n-1)*(n-2)*(n-3)*...*1 and
# that factorial(0) = 1, by definition
# Example: factorial(5) = 5*4*3*2*1 = 120
# Also, factorial(n) is undefined for n < 0
def factorial(some_int):
     if some_int < 0: return "UNDEFINED"
     if some_int == 0: return 1
     else: return some_int*factorial(some_int-1)
print factorial(5)

Question: what if some wiseass surrealist uses our code and tries to say factorial("fish")? Clearly, this is an invalid type of input. Our little function should deal with it by checking the input before doing anything else, and raising an Exception if the input is bogus. Another way of preventing this problem is to never accept string values, period - instead, accept only integer values, because they'll always make mathematical sense.

The multiplication operator does a similar thing - whoever designed it in Python knew that eventually, somebody might try to multiply unmultipliable things, so s/he limited it so that it will only accept int or floating-point values (actually this is an oversimplification - the multiplication operator is overloaded to work on a single string, too, but don't worry about that just now).

However, we are all reasonable people here. The Python designer(s) knew that we would eventually want to convert string values which represent numbers (like "21" or "3.1415") into their integer value and floating-point value equivalents (21 and 3.1415, respectively). In other languages, this is called "casting." So Python provides int(), float(), and str() to allow transformations between all three. Of course, you can't say float("fish") and expect to get a number.

Play around with it!

:cheesy: Thanks a bundle! I'm hoping that it will work! :cheesy:

Well I tried what both of you said but it still didn't work.

Just as a reminder I am very new to python and I am just entering HighSchool.

Mabie If you saw my script it might help.

################################################################################
# Tkinter version of my easy to use area calculation program.
# Created by Daniel Looney
# Using Python 2.3
################################################################################

# Import Modules
from Tkinter import *

def square(event):
    global entrySquareh, entrySquarew
    
    instLabel1 = Label(root)
    instLabel1.configure(text="Enter the hight of the rectangle below.")
    instLabel1.pack()
    
    entrySquareh = Entry(root)
    entrySquareh.pack()

    instLabel2 = Label(root)
    instLabel2.configure(text="Enter the width of the rectangle below.")
    instLabel2.pack()
    
    entrySquarew = Entry(root)
    entrySquarew.pack()

    instLabel3 = Label(root)
    instLabel3.configure(text="The area of the rectangle will be displayed below.")
    instLabel3.pack()

    btn1 = Button(root, text="Get Answer!")
    btn1.pack()
    btn1.bind('<Button-1>', answerSquare)

def answerSquare(event):
    global entrySquareh, entrySquarew
    
    ansLabel = Label(root)
    ansLabel.config(text='')
    ansLabel.config(text=int(entrySquareh) * int(entrySquarew))
    ansLabel.pack()

def answerCircle(event):
    global radius
    
    ansLabel2 = Label(root)
    ansLabel2.config(text='')
    ansLabel2.config(text='The area of your circle is:' + 3.14 * radius.get())
    ansLabel2.pack()

def circle(event):
    global radius
    
    instLabel4 = Label(root)
    instLabel4.configure(text='Enter the radius of the circle below.')
    instLabel4.pack()

    radius = Entry(root)
    radius.pack()
    
    instLabel5 = Label(root)
    instLabel5.configure(text="The area of the circle will be displayed below.")
    instLabel5.pack()

    btn2 = Button(root, text="Get Answer")
    btn2.pack()
    btn2.bind('<Button-1>', answerCircle)

root = Tk()

MenuLabel = Label(root)
MenuLabel.configure(text="Select a shape below")
MenuLabel.pack()

SquBtn = Button(root, text="Square")
SquBtn.pack()
SquBtn.bind('<Button-1>', square)

CirButton = Button(root, text='Circle')
CirButton.pack()
CirButton.bind('<Button-1>', circle)

root.mainloop()

The error that it returns is something like:

Entry instance has no attribute __int__ if I use int()
" " " " " __float__ if I use float() :sad:

Well i found out what was wrong....

I forgot to put in the .get() entirely...

sometimes i amaze myself.

Well thanks for all of your help!

Since this post was taliking about how programming languages handle data types I thought the following was interesting. This how the new windows scripting language monad handles data

I am not sure if other language behave like this, but I thouhgt this was interesting.
lets say I make a varibale as a string

msh>$a = "3"
msh>$a + 4
msh>34  #it concatenates them, like two strings

but if I put a number first in the statemnet, it treats them both as integers

msh>$a = "3"
msh>4 + $a
msh>7  #this time it treated them both as integers

so if you want to add a bunch of strings as integers, you do it like this

msh>$a = "3"
msh>$b = "5"
msh>0 + $a + $b
msh>8

Looks like Monad tries to cast to the type of the first member of a statement. This could lead to some pretty unexpected results. Maybe the designer of this particular language was simply too lazy to deal with errors.

BTW danizzil14, go with float() in your calculations.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.