0

I need some help! I'm learning tkinter and I decided to make a game. I made a button and one label, so when you click the button, it will move randomly on screen and label will count your clicks. Now, I need a timer, but I don't know how to make it. The game should be like this: https://scratch.mit.edu/projects/2245518/

Here's my code:

    from tkinter import *
from random import randint
import 

root = Tk()
root.geometry('600x470')
root.title('Catch The Button - Game')

clicks = 0

def change():
    global clicks
    clicks += 1
    clicksLabel['text'] = 'Clicks: ' + str(clicks) + 180 * ' '
    rand = randint(1,5)

    if rand == 1:
        button.pack(side = LEFT, padx = randint(1, 220), pady = randint(1, 220))
    elif rand == 2:
        button.pack(side = RIGHT, padx = randint(1, 220), pady = randint(1, 220))
    elif rand == 3:
        button.pack(side = TOP, padx = randint(1, 220), pady = randint(1, 220))
    else:
        button.pack(side = BOTTOM, padx = randint(1, 220), pady = randint(1, 220))


clicksLabel = Label(root, text = 'Clicks: 0' + 180 * ' ')
clicksLabel.pack()

button = Button(root, text = 'Catch me  :P', command = change)
button.pack(side = RIGHT, padx = randint(1, 220), pady = randint(1, 220))

root.mainloop()

2 Answers 2

1

You can use the after method to let a function run periodically. You could make a label which shows the time and write a function that updates it every 100ms and check when time has run out to show the score:

time = 20

def update_time():
    global time
    time = time-0.1
    timeLabel.config(text=str(time))
    if time<0.01:
        timeLabel.config(text='0')
        button.pack_forget()
        Label(root, text='You scored {}'.format(clicks), font=('Helvetica', 20, 'bold')).pack()
    else:
        timeLabel.after(100, update_time)

In your code (I changed a few other things too) that looks like this

from tkinter import *
from random import randint

root = Tk()
root.geometry('600x470')
root.title('Catch The Button - Game')

clicks = 0
time = 20

def change():
    global clicks
    clicks += 1
    clicksLabel['text'] = 'Clicks: ' + str(clicks)
    rand = randint(1,5)

    if rand == 1:
        button.pack(side = LEFT, padx = randint(1, 220), pady = randint(1, 220))
    elif rand == 2:
        button.pack(side = RIGHT, padx = randint(1, 220), pady = randint(1, 220))
    elif rand == 3:
        button.pack(side = TOP, padx = randint(1, 220), pady = randint(1, 220))
    else:
        button.pack(side = BOTTOM, padx = randint(1, 220), pady = randint(1, 220))

def update_time():
    global time
    time = time-0.1
    timeLabel.config(text=str(time))
    if time<0.01: # Account for rounding errors
        timeLabel.config(text='0')
        button.pack_forget()
        Label(root, text='You scored {}'.format(clicks), font=('Helvetica', 20, 'bold')).pack()
    else:
        timeLabel.after(100, update_time)

clicksLabel = Label(root, text = 'Clicks: 0')
clicksLabel.pack(side=LEFT, anchor=N)

timeLabel = Label(root, text=str(time))
timeLabel.pack(side=RIGHT, anchor=N)
timeLabel.after(100, update_time)

button = Button(root, text = 'Catch me  :P', command = change)
button.pack(side = BOTTOM, padx = randint(1, 220), pady = randint(1, 220))

root.mainloop()
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, that's just what I wanted! :)
I didn't know it.. XD Accepted.
1

I've managed that with the following code:

from Tkinter import * # if using python 3.x
from tkinter import * # if using python 2.x
from random import randint
import time

root = Tk()
root.geometry('600x470')
root.title('Catch The Button - Game')

t0 = time.time()
ctime = 0
clicks = 0

def change():
    global clicks
    global ctime
    global t0

    t1 = time.time()
    dt = t1 - t0
    t0 = t1

    ctime += dt
    clicks += 1

    clicksLabel['text'] = 'Clicks: ' + str(clicks) + 180 * ' '
    timeLabel['text'] = 'Time: ' + str(ctime)[:4] + 's' + 180 * ' '

    rand = randint(1,5)

    if rand == 1:
        button.pack(side = LEFT, padx = randint(1, 220), pady = randint(1, 220))
    elif rand == 2:
        button.pack(side = RIGHT, padx = randint(1, 220), pady = randint(1, 220))
    elif rand == 3:
        button.pack(side = TOP, padx = randint(1, 220), pady = randint(1, 220))
    else:
        button.pack(side = BOTTOM, padx = randint(1, 220), pady = randint(1, 220))


clicksLabel = Label(root, text = 'Clicks: 0' + 180 * ' ')
clicksLabel.pack()
timeLabel = Label(root, text = 'Time: 0.00 s' + 180 * ' ')
timeLabel.pack()

button = Button(root, text = 'Catch me  :P', command = change)
button.pack(side = RIGHT, padx = randint(1, 220), pady = randint(1, 220))

root.mainloop()

I simply defined a start time and a running time (t0 and ctime respectively). When the button is pressed, the code

t1 = time.time()
dt = t1 - t0
t0 = t1
ctime += dt

simply adds the time since the last button press to ctime and then the timeLabel er label updates with the new time (just as how clicksLabel updates).

Diclaimer: I didn't write a timer that visibly increases without clicking the button, as that would require a while loop, exit conditions and constantly updating, whereas your program is structured to only update on click!

Hope this helps

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.