Home > Software design >  Error trying to create gradient between files
Error trying to create gradient between files

Time:12-04

I'm trying to create a gradient effect using two files and an import, but I keep getting an error, here are the files.

backrounds.py

# Keigen Sheppard, CS 115, Autumn 2021
# Programming Project #6,
# This is a simulated fitness tracker that
# creates a gradient color backround of the user's
# choosing and correlates how many steps they get
# to the height and number of flowers

#--------------------
import math
import random
from ECGUI import *
#--------------------
#imports

width = 1920
height = 1080
canvas = drawing_panel(width,height, "white")
#width,height and canvas
#--------------------
globalbool = True
# bool for while statement later in the program
#--------------------
x_color1 = 0
y_color1 = 0
#gradient x/y
#--------------------
red = 0
green = 0
blue = 0
#defining color variables
#--------------------



red_change = 0
green_change = 0
blue_change = 0
# gradient direction
#--------------------


#----varibles,variables, and the canvas, all the varible names are self---
#----explanatory---
#--------------------



#calculation for the change integer
#--------------------
def color_change(color_change):
    global red
    global green
    global blue
    global red_change
    global green_change
    global blue_change
    red  = red_change
    green  = green_change
    blue  = blue_change
    color_change  =1  

def gradient(start_red,start_green,start_blue,stop_red,stop_green,stop_blue,gradient_direction):
    red = start_red
    green = start_green
    blue = start_blue


    if gradient_direction == "left" or gradient_direction == "right":
    
        red_change = float((stop_red-start_red)) / float(width)

        green_change = float((stop_green-start_green)) / float(width)

        blue_change = float((stop_blue-start_blue)) / float(width)

    else:
        red_change = float((stop_red-start_red)) / float(height)

        green_change = float((stop_green-start_green)) / float(height)

        blue_change = float((stop_blue-start_blue)) / float(height)


        
    global x_color1
    global x_color2
    global y_color1
    global y_color2
    global globalbool
    genbool = True
        
    while genbool:
        if gradient_direction == "left" or gradient_direction == "right":
        #gradient direction check, will default to up/down if any other input besides up/down is 
            canvas.draw_line(x_color1,y_color1,x_color1,y_color1 height, (round(red),round(green),round(blue)))

            y_color1 = 0
            x_color1 = color_change(x_color1)

            if  x_color1 >= width:# checks if the color gradient has finished/ hit the edge of frame
                genbool = False
                
        else:
            canvas.draw_line(x_color1,y_color1,x_color1 width,y_color1, (round(red),round(green),round(blue)))

            y_color1 = color_change(y_color1)
            x_color1 = 0

            if y_color1 >= height:# checks if the color gradient has finished/ hit the edge of frame
                genbool = False
        
            #--------------------
            # gradient function, draws the gradient inputed at the top of the page
          

# color change function, calculates the change of the rgb values and
# an extra variable that is used either for the x or y coordinates of the line

paint.py

from backrounds import *
gradient(1,20,100,100,50,200,"right")

error

Traceback (most recent call last):
  File "C:\Users\Kshep\AppData\Local\Programs\Python\Python39\Code\paint.py", line 2, in <module>
    gradient(1,20,100,100,50,200,"right")
  File "C:\Users\Kshep\AppData\Local\Programs\Python\Python39\Code\backrounds.py", line 127, in gradient
    if  x_color1 >= width:# checks if the color gradient has finished/ hit the edge of frame
TypeError: '>=' not supported between instances of 'NoneType' and 'int'
>>> 

CodePudding user response:

color_change() is a function that does not have a return value, so when you do:

x_color1 = color_change(x_color1)

You are assigning a null return value to x_color1, hence the error saying that it is of NoneType. To solve the warning you can change it to:

color_change(x_color1)

You will want to do the same for y_color1 = color_change(y_color1), changing it to color_change(y_color1)

I would also recommend to change the name of the argument of def color_change(color_change) to something like def color_change(color). In general it is best to use a name for arguments different from the name of the function, since it can create confusion and it does not allow you to call the function inside it.

  • Related