Skip to content Skip to sidebar Skip to footer

Trouble With Classes And Functions

I'm basically writing a module for a basic pygame drawing app. im using a Tk window to get the three color values for a custom color. i have a file that opens a Tk window and askes

Solution 1:

The problem is that the return value of return_color is not used, since the reference to the function passed as a command option is used to call it but not to store the result. What you can do is to store the values as attributes of the class in return_color and add a return statement in get_color after the call to start the mainloop:

defget_color()
    # Initialize the attributes with a default value
    self.r = ''
    self.g = ''
    self.b = ''# ...
    root.mainloop()
    return self.r, self.g, self.b

defreturn_color(self):
    # Entry.get returns a string, don't need to call to str()
    self.r = self.enter1.get()
    self.g = self.enter2.get()
    self.b = self.enter3.get()
    root.destroy()

Before using the color, you can check that the format is correct. Then I suggest you to rename the functions with more meaningful names; and create a Tk element, withdraw it and use a Toplevel in your class (if you create more than one Custom object, you are actually creating multiple Tk elements, and that's something which should be avoided).

Post a Comment for "Trouble With Classes And Functions"