0

I can't use hold_x, hold_y and fold_x, fold_y in tav variable. Do you know how to solve this problem?

I have tried hold_mozgat(0,0).hold_x and hold_mozgat(0,0).hold_y without any success.


def hold_mozgat(v, h):
    global x1, y1
    x1, y1 = x1 + v, y1 + h
    can1.coords(hold, x1, y1, x1 + 90, y1 + 90)
    global hold_x, hold_y
    hold_x, hold_y = x1 + 50, y1 + 50
    print('A Hold szélessége : ' + str(x1 + 50) + ' Hosszúsága : ' + str(y1 + 50))
    return hold_x, hold_y


def fold_mozgat(v, h):
    global a1, b1
    a1, b1 = a1 + v, b1 + h
    can1.coords(fold, a1, b1, a1 + 200, b1 + 200)
    global fold_x, fold_y
    fold_x, fold_y = a1 + 100, b1 + 100
    print('A Föld szélessége : ' + str(a1 + 100) + ' Hosszúsága : ' + str(b1 + 100))
    return fold_x, fold_y


hx, hy = hold_mozgat.hold_x, hold_mozgat.hold_y
fx, fy = fold_mozgat.hold_x, fold_mozgat.fold_y

tav = int((sqrt(((fx - hx) ** 2) + ((fy - hy) ** 2))) - 150)

Label(keret, text='A Föld és Hold távolsága : ' + str(tav) + ' fényév', fg='red').pack(side=BOTTOM)

1 Answers1

1

I tried this:

def hold_mozgat(v=0, h=0):
    global x1, y1
    x1, y1 = x1 + v, y1 + h
    can1.coords(hold, x1, y1, x1 + 90, y1 + 90)
    hold_mozgat.hold_x = x1 + 50
    hold_mozgat.hold_y = y1 + 50
    print('A Hold szélessége : ' + str(x1 + 50) + ' Hosszúsága : ' + str(y1 + 50))
    return hold_mozgat.hold_x, hold_mozgat.hold_y

def tavolsag():
    hold_mozgat()
    fold_mozgat()
    fx, fy = fold_mozgat.fold_x, fold_mozgat.fold_y
    hx, hy = hold_mozgat.hold_x, hold_mozgat.hold_y
    tavolsag.tav = int((sqrt(((fx - hx) ** 2) + ((fy - hy) ** 2))) - 150)
    return tavolsag.tav

Label(root, text='A Föld és Hold távolsága : ' + str(tavolsag.tav) + ' fényév', fg='red').pack(side=BOTTOM)

I believed that if i gave default values to hold_mozgat(v,h): than it'll work but it didn't work. The program runs, but it says Cannot find reference 'fold_x' in function, and when i run program it's says on gui <function tavolsag at 0x037C66E8>. I looked this error core in google, but i can't find anythin.

  • I tried it because I read on this forum in a question that it possible. (https://stackoverflow.com/questions/19326004/access-a-function-variable-outside-the-function-without-using-global) – gamergabriel May 07 '20 at 09:27