0
from tkinter import *
from random import randint
from math import sqrt
def create_bubble():
    x = WIDTH + GAP
    y = randint(0, HEIGHT)
    r = randint(MIN_BUB_R, MAX_BUB_R)
    id1 = c.create_rectangle(x - r, y - r, x + r, y + r, outline="light blue", fill=color)
    bub_id.append(id1)
    bub_r.append(r)
    bub_speed.append(randint(1, MAX_BUB_SPD))
def buton():
    color = 'green'
HEIGHT = 500
WIDTH = 800
window = Tk()
window.title("Distrugatorul de bule")
c = Canvas(window, width=WIDTH, height=HEIGHT, bg="darkblue")
c.pack()
color = 'blue'
bub_id = list()
bub_r = list()
bub_speed = list()
MIN_BUB_R = 10
MAX_BUB_R = 30
MAX_BUB_SPD = 10
GAP = 100
b = Button(window, text="change", command = buton)
b.pack()
mainloop()
BUB_CHANCE = 10
if randint(1, BUB_CHANCE) == 1:
        create_bubble()

color is the variable and, when start the program and press the button the bubbles stay still blue.what should i do? does the button not work or why this is happening?

TheMask
  • 1
  • 1
  • 2
  • `color = 'green'` just sets the value of the local variable `color`. The variable goes away when the function ends. This doesn't accomplish anything because you never do anything with the value. – kindall Jun 18 '18 at 19:42
  • @kindall beat me to it. Read about variable scope: it's a very common problem that tricks up new programmers all the time. There are many tutorials on this, and the following question might help if you want a lot of detail: https://stackoverflow.com/questions/291978/short-description-of-the-scoping-rules – divibisan Jun 18 '18 at 19:46
  • Possible duplicate of [Short Description of the Scoping Rules?](https://stackoverflow.com/questions/291978/short-description-of-the-scoping-rules) – divibisan Jun 18 '18 at 19:47
  • the variable color is used at the id1 variable – TheMask Jun 19 '18 at 03:42

0 Answers0