Unsupported operand type(s) for *: 'instance' and 'float'

Question

I'm fairly new to Tkinter. I've tried creating a class for a GUI I'm creating and I've bumped into an error which I am unable to fix. Ive been through other forums and tried different ways of amending the error but had no luck. Code:

root1 = Tk()
root1.geometry("800x500")
class simulation:

    def __init__(self, root1):
    radius_input = Label ( root1 , text="Radius: " )
    direction_input = Label ( root1 , text="Direction (clockwise=1/\n anticlockwise=2): " )
    radius_entry = Entry ( root1 )
    direction_entry = Entry ( root1 )

   #####other code used to display labels and entry boxes

    self.radius = radius_entry
    self.direction = direction_entry

    self.pos_neg = 0

    if self.direction == 1:
        self.pos_neg = (-6.28)
    elif self.direction == 2:
        self.pos_neg = (6.28)

def actual_sim(self):
    r = self.radius  #radius of the circle the ball moves
    t = 1
    omega = self.pos_neg / t  # amount/angle the ball moves each second


  #####other code not needed
        xposition = (r * cosValue)
        yposition = (r * sinValue)

the error i get is:

    xposition = (r * cosValue)
TypeError: unsupported operand type(s) for *: 'instance' and 'float'

i dont understand what this means or how to fix it. Any ideas anyone??


Show source
| class   | python   | tkinter   2017-01-06 00:01 1 Answers

Answers to Unsupported operand type(s) for *: 'instance' and 'float' ( 1 )

  1. 2017-01-06 00:01

    The error message is telling you that you're trying to multiply an instance (ie: an object based on some class) by a float. In this case you are multiplying a widget (not the contents of the widget), which makes no sense.

    You must modify actual_sim to fetch the value and convert it to a number before you can perform calculations with it:

    def actual_sim(self):
        r = float(self.radius.get())
        ...
    

Leave a reply to - Unsupported operand type(s) for *: 'instance' and 'float'

◀ Go back