Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to update colour variable outside of the function and update the window/canvas? (tkinter)

So I’m trying to create this pokeball designer. I’ve created a colour variable which is set to ‘red’ at the start, then there is a button which assigns green to the colour variable though it doesn’t seem to update it outside the function and it doesn’t update the colour of the shape in the window.

So how would I update the colour variable outside the window?

And how would I get it to update on the canvas?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

from tkinter import *

width = 500  # size of window is set
height = 500

colour = 'red'  # colour is initially set to red


def colourchange():
    colour = 'green'
    window.update()


window = Tk()  # creates window
canvas = Canvas(window, width=width, height=height)  # creates canvas
canvas.create_arc(10, 10, width - 10, height - 10, fill=colour, style=PIESLICE, extent=180, width=10)  # creates top shell of pokeball
canvas.create_arc(10, 10, width - 10, height - 10, fill='white', style=PIESLICE, extent=180, width=10, start=180) # creates bottom shell of pokeball

colourButton = Button(window, text='Switch Colour', command=colourchange) # creates button to switch colour of pokeball
colourButton.pack() 

canvas.pack()
window.mainloop()

>Solution :

If you want to change the color then you need to configure it like you did when you created it. You do not need to use the update() method here. You can do something like this.

from tkinter import *

width = 500  # size of window is set
height = 500

colour = 'red'  # colour is initially set to red


def colourchange():
    colour = 'green'
    canvas.create_arc(10, 10, width - 10, height - 10, fill=colour, style=PIESLICE, extent=180, width=10)


window = Tk()  # creates window
canvas = Canvas(window, width=width, height=height)  # creates canvas
canvas.create_arc(10, 10, width - 10, height - 10, fill=colour, style=PIESLICE, extent=180, width=10)  # creates top shell of pokeball
canvas.create_arc(10, 10, width - 10, height - 10, fill='white', style=PIESLICE, extent=180, width=10, start=180) # creates bottom shell of pokeball

colourButton = Button(window, text='Switch Colour', command=colourchange) # creates button to switch colour of pokeball
colourButton.pack() 

canvas.pack()
window.mainloop()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading