Python Forum
Using Time to Update a Global Variable and Label in TkInter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using Time to Update a Global Variable and Label in TkInter
#1
At the moment, I am trying to make a game using TkInter. So far, it probably doesn't look very pretty, but please ignore that Sleepy.

What I am trying to do is implement the cookies per second actually adding to the total_cookies variable, and I also want to update the label when clicking, and ever second from the cookies per second variable, cookies_psecond. Whenever I try to make a function that adds the cookies_psecond to total_cookies, it always runs the function rather than displaying the GUI. I don't know if I am doing it wrong, or if I am just not putting the function in the right place. Can I have some help with this please?
Thank you in advance.

import tkinter as tk
import PIL as pil
from PIL import Image, ImageTk
import time

# Default Variables
total_cookies = 0
grandmas = 0
cookies_psecond = 0
cookies_pclick = 1
buying = ""
grandma_cps = 0.75

cookies_psecond_float = float(cookies_psecond)
cookies_pclick_float = float(cookies_pclick)

# Times an upgrade was purchased
times_upgraded_clicker = 0
times_grandma_purchased = 0

# Prices of buyable items
upgrade_clicker_cost = 25
grandma_price = 50

class CookieClickerGame:
    def __init__(self, master):
        self.master = master
        self.master.title("Cookie Clicker")
        self.master.resizable(height=False, width=False)

        # Functions


        def cookie_click():
            global total_cookies
            global cookies_pclick

            total_cookies = int(total_cookies)
            total_cookies += cookies_pclick
            total_cookies = str(total_cookies)
            self.total_cookies_display.configure(text="Cookies: {}".format(total_cookies))

        def upgrade_clicker():
            global cookies_pclick
            global upgrade_clicker_cost
            global total_cookies
            global times_upgraded_clicker
            global cookies_pclick_float

            total_cookies = int(total_cookies)

            if total_cookies < upgrade_clicker_cost:
                pass
            elif total_cookies >= upgrade_clicker_cost:
                total_cookies -= upgrade_clicker_cost
                times_upgraded_clicker += 1
                cookies_pclick *= 2
                upgrade_clicker_cost *= 3
                self.upgrade_clicker_button.configure(text='Buy Clicker\nYou have {}\nCost: {}'.format(times_upgraded_clicker, upgrade_clicker_cost))
                self.total_cookies_display.configure(text="Cookies: {}".format(total_cookies))


        def buy_grandma():
            global grandmas
            global cookies_psecond
            global total_cookies
            global times_grandma_purchased
            global cookies_psecond_float

            if total_cookies < grandma_price:
                pass
            elif total_cookies >= grandma_price:
                total_cookies -= grandma_price
                times_grandma_purchased += 1
                cookies_psecond_float += grandma_cps
                cookies_psecond = cookies_psecond_float
                round(cookies_psecond, 0)
                cookies_psecond = int(cookies_psecond)


        # Cookie Clicker Button
        cookie_picture = ImageTk.PhotoImage(file="bestcookie.gif")
        self.cookie_button = tk.Button(master, image=cookie_picture, command=cookie_click)
        self.cookie_button.image = cookie_picture
        self.cookie_button.grid(row=2, columnspan=20, sticky='nsew', padx=10, pady=10)

        # Total Cookies Display
        self.total_cookies_display = tk.Label(master, text="Cookies: 0")
        self.total_cookies_display.grid(row=1, columnspan=10, sticky='nsew')

        # Clicker Upgrade Stuff
        clicker_button_picture = ImageTk.PhotoImage(file="Cursor.png")
        self.upgrade_clicker_button = tk.Button(master, image=clicker_button_picture, text='Buy Clicker\nYou have \nCost: 25', compound="left", command=upgrade_clicker)
        self.upgrade_clicker_button.image = clicker_button_picture
        self.upgrade_clicker_button.grid(row=3, columnspan=10, sticky='nsew', padx=20, pady=20)


root = tk.Tk()
cookie_clicker = CookieClickerGame(root)
root.mainloop()
Reply


Messages In This Thread
Using Time to Update a Global Variable and Label in TkInter - by cameron121901 - Apr-21-2019, 09:27 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python 3 Global Variable Help Tridium 5 3,053 Jul-29-2019, 08:02 AM
Last Post: Tridium

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020