2017-12-04 3 views
-1

내 프로그램에는 네 개의 레이블이 있습니다. 3은 독립 실행 형 변수이지만 4 번째 변수는 100.0으로 변수 중 하나를 나눈 값이며 잘 작동합니다.레이블에서 내 변수가 변경되지 않습니까? (파이썬)

내 프로그램이 제대로 작동하고 잘 실행되지만 5 번째 (2 변수의 곱셈)에 추가되지만 0에서 변경되지는 않습니다. 프로그램의 코드에서 4 번째, 그러나 나는 아직도 그것이 작동하지 않을 이유를 알 수 없다. 내가 생각할 수있는 유일한 것, 나는 그것을 재 장전하지 않았지만 나는 보았다. 나는 정말 붙어있어서 누군가 제발 도와 줄 수 있니? 여기에 코드가 있습니다 :

import tkinter as tk 
from PIL import Image, ImageTk 

# --- functions --- 

def moar_eggz(): 
    global eggzps, chookz 

    chookz += 1 


def update_labels(): 
    try: 
     label1.config(text="Eggs: " + str(round(eggz))) 
     if eggzps >= 10: 
      label2.config(text="Eggs Per Second: " + str(round(int(eggzps)))) 
     elif eggzps < 10: 
      label2.config(text="Eggs Per Second: " + str(eggzps)) 
     label3.config(text="Egg Value: " + str(eggvalue)) 
     label4.config(text="Chickens: " + str(chookz)) 
     label5.config(text="Money: " + str(money)) 
    except Exception as e: 
     print(e) # display exception to see problem 

    # repeat it after 20ms 
    root.after(20, update_labels) 

def main_loop(): 
    global eggz, eggzps 
    eggzps = chookz/100.0 
    money = eggz * eggvalue 
    update_labels() 
    try: 
     eggz += eggzps 
    except Exception as e: 
     print(e) # display exception to see problem 

    # repeat it after 1000ms 
    root.after(1000, main_loop) 

# --- main --- 

root = tk.Tk() 
root.title("Chicken Clicker") 

eggz = 0 
eggvalue = 0.2 
chookz = 0 
eggzps = 0.0 
eggzpms = 0 
money = eggz * eggvalue 


# empty labels - `update_labels` will add text 
label5 = tk.Label(root) 
label4 = tk.Label(root) 
label3 = tk.Label(root) 
label2 = tk.Label(root) 
label1 = tk.Label(root) 
label5.pack() 
label4.pack() 
label3.pack() 
label2.pack() 
label1.pack() 

chickencnv = Image.open("img\\1.png") 
chicken = ImageTk.PhotoImage(chickencnv) 

openbutton6= tk.Button(root, image=chicken, width=500, height=500, command=moar_eggz) 
openbutton6.pack() 


# run it first time at once 
main_loop() 


root.mainloop() 

나는 게임에서 많은 변화를 시도했지만 아무 것도 변경하지 못했기 때문에 여기에 왔습니다.

관련없는 또는 관련있는 답변이나 주제에 대한 제안은 높이 평가됩니다.

미리 감사드립니다.

+1

'main_loop' 내에'global money'를 사용해야합니다. 지역 변수'money'를 생성하지 않습니다. – furas

+1

BTW :'main_loop'에서 값을 변경하고'main_loop'에서'update_labels()'를 호출하면'root.after (20, update_labels)'를 실행하는 것은 의미가 없습니다 – furas

답변

2

글로벌 변수에 main_loop을 추가하는 것을 잊어 버렸습니다. 따라서 update_labels에서 항상 표시되는 초기 값은 0입니다.

def main_loop(): 
    global eggz, eggzps, money 
    ... 
관련 문제