Home My Variable won't change on a label? (python)
Reply: 1

My Variable won't change on a label? (python)

Caleb Sim
Caleb Sim Published in 2017-12-04 09:06:22Z

In my program there are four labels. 3 are stand-alone variables, but the 4th is a division of one of the variables by 100.0, and works fine.

My program works fine and runs fine, but I add in a 5th (which is a multiplication of 2 variables), but it does not change from 0. It is in the same place in the program's code as the 4th, but I still can't figure out why it won't work. Here is the code:

import tkinter as tk
from PIL import Image, ImageTk

# --- functions ---

def moar_eggz():
    global eggzps, chookz

    chookz += 1

def update_labels():
        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
        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)

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

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

# run it first time at once


I have tried many changes with the code in my game, but all have failed to change anything, so I came here.

Any unrelated or related answers or suggestions to the topic will be highly appreciated.

Thanks in advance!

j_4321 Reply to 2017-12-04 09:20:06Z

You just have forgotten to add money to the global variables in main_loop. So in update_labels, its always its initial value 0 which is displayed.

def main_loop():
    global eggz, eggzps, money
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.30678 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO