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
#2
please attach images
Reply
#3
Why are you defining all the functions of the class inside the __init__ function
Reply
#4
(Apr-22-2019, 02:59 AM)SheeppOSU Wrote: Why are you defining all the functions of the class inside the __init__ function

I am just learning the __init__ function and am just scratching the surface of tkinter.

"So far, it probably doesn't look very pretty, but please ignore that."

I don't like to think of myself as a kiddie, but I am definitely a beginner.

In conclusion, I don't know why I am defining all of the functions in the init function, but I don't know of any alternate solutions.

Let me post those images...
Reply
#5
I would suggest the following tutorial on class use:
https://interactivepython.org/runestone/...ctree.html

You are doing many things wrong
Besides defining all in the __init__ method, you should never have to use a single global, nor define variables outside of the class if needed within.
Reply
#6
Ok, I'll tell you about the __init__ function. The __init__ function is for defining self variables and sometimes doing a few things. If I create a player class I would do the following:
  1. Make an __init__ function with parameters self, x, y, width, height, color
  2. I would define all the variables as goes -
    self.x = x
    self.y = y
    #etc.
    and you can add more variables that aren't in the parameters like this self.rect = (self.x, self.y, self.width, self.height)
  3. make a draw function in the player class. It is able to access the self variables defined in __init__ as long as I put self in the draw parameter.
  4. Then I save the class in a variables like this p = Player(paramters)
    I don't have to self or anything like that in the parameters because it automatically does that.
  5. finally to draw it I would call upon the draw function like this p.draw()

Hope you understand now
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to correctly update score based on elapsed time? temlotresid6 8 1,918 Oct-26-2024, 05:57 AM
Last Post: temlotresid6
  Python 3 Global Variable Help Tridium 5 4,116 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