0

Hey I'm trying to write a UI for an orbital simulator using PyQt5. I have created 8 planet objects and written functions for the UI to read to and from the class so that it can display the right information to the user.

if planet == "Planet 1":
     self.slider1.setValue(p.p1.radius)
     self.colors.setId(p.p1.color)
     self.slider2.setValue(p.p1.mass)
     self.x_slider.setValue(p.p1.x)
     self.y_slider.setValue(p.p1.y)
     self.z_slider.setValue(p.p1.z)`

This code basically says if planet 1 is selected set the values on the UI to display the attributes of planet 1. I created a similar method to write to the planet when the user wanted to change the attributes of the planet. When I try either routes I get an attribute error.

P.s I also tried to write a method within the planet class to change the attributes inside the class as I thought this may have been the issue.

def change(attribute,value):
    if attribute == "radius":
        self.radius = value
    elif attribute == "colour":
        self.colour = colour
    elif attribute == "mass":
        self.mass = value
    elif attribute == "x":
        self.x = value
    elif attribute == "y":
        self.y = value
    elif attribute == "z":
        self.z = value
    elif attribute == "vx":
        self.vx = value
    elif attribute == "vy":
        self.vy = value
    elif attribute == "vz":
        self.vz = value    
Laurent LAPORTE
  • 21,958
  • 6
  • 58
  • 103
Moselle
  • 3
  • 3

1 Answers1

0

Your change() function is not a method. You need to pass the self argument as fist parameter:

class Planet (object):
    def change(self, attribute, value):
        ...

You can use the built-in function setattr to set an attribute value (instead of defining your own):

    def your_meth(self):
        ...
        setattr(self, your_attr, your_value)
Laurent LAPORTE
  • 21,958
  • 6
  • 58
  • 103
  • Thank you that really shortens my code. I'm still getting an attribute error when I try to reassign a value to an attribute. Do you have any idea why this would be. – Moselle Jan 07 '19 at 13:44