Dominic Culyer
Dominic Culyer

Reputation: 99

variable being passed through parameters not updated

The user selects a goal which then changes the background colour scheme. I have it working, however when a new function is ran, the colour scheme reverts back to the original.

When the goal is selected, one of these functions gets called (depending on which goal is selected.

def Get_Fit():
    canvas.delete("all")
    goal = "fit"
    Overlay(goal)
    Bottom(goal)

Those functions determine the goal, and then call the overlay function (which is resonsible for the colour scheme).

def Overlay(goal):
    colour = Goal(goal)
    canvas.create_oval(-2000, 50, 1000, 2000, fill='gray33', outline=colour, width=4)
    canvas.create_oval(-300, 1600, 4000, 300, fill="gray21", outline=colour, width=4)
    canvas.create_text(500, 100, text="EZ FITNESS", font=font3, fill = "gray80")

This overlay function calls another function called goal. goal is responsible for determining the colours in the colour scheme.

def Goal(goal):
    if goal == "fit":
        colour="#00e5ff"
    elif goal == "weight":
        colour="#ff00e5"
    elif goal == "strong":
        colour = "#d60000"
    else:
        colour="#ff5d00"
    return colour

Everytime the overlay function is called, it is called as follows:

Overlay(goal)

Now when i select a goal, the colour scheme changes as it is supposed to. However as soon as I run a new function (in this example account) the colour scheme reverts back to the original colour. Any ideas why? Here is the code for Account

def Account():
    canvas.delete("all")
    Bottom(goal)
    Overlay(goal)

    startingweight_label = Label(canvas, width=15, height=1, text="Starting Weight: ", font=font2, bg="gray33", fg="white", relief = "raised", borderwidth=2)
    canvas_startingweight_label = canvas.create_window(475, 350, window=startingweight_label)

    startingweight_entry = Entry(root, width = 10, bg="gray30")
    canvas_startingweight_entry = canvas.create_window(600, 350, window=startingweight_entry) 

Here are some pictures Picture A demonstrates what happens after the Get Fit goal is selected. This works well.

Picture B demonstrates what happens after the get fit goal is selected and then after Account is ran. As you can see, the colour scheme reverts. A

B

Upvotes: 1

Views: 253

Answers (1)

Dominic Culyer
Dominic Culyer

Reputation: 99

I managed to solve this issue!

The problem was that the changes made to goal were staying local to the function. What i had to do return goal so that all changes made in the function were made to the other functions. I did this simply by globalising the variable. You need to be careful when globalising variables, however in this scenario i believe it is ok!

def Get_Fit():
    canvas.delete("all")
    goal = "fit"
    global goal
    Overlay(goal)
    Bottom(goal)

Upvotes: 1

Related Questions