-1
\$\begingroup\$
 Traceback (most recent call last):
  File "C:\Users\User\Desktop\game 1.9.1.py", line 76, in <module>
    but_1 = Button(text='1', width=3, height=1, bg='blue', font='Hack 30', command=click_button, true = 1)
  File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 2679, in __init__
    Widget.__init__(self, master, 'button', cnf, kw)
  File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 2601, in __init__
    self.tk.call(
_tkinter.TclError: unknown option "-true"

I planned to make 1 correct button, and that it would change. When you click on the correct button, the score is added to 1, and if it is incorrect, the score is reset The game code itself:

from tkinter import *
import random as rn
import time

rn.seed()    
timer = TIME = 5

root = Tk()
root.title('game')
root.geometry('720x1280')

true = 0

clicks = 0



def click_button():
    global clicks, timer, true
    clicks += 1
    timer = TIME
    labelClick['text'] = str(clicks)
    labelClick.pack() #Variable clicks

    count = rn.randrange(1,10,1)
    print(count)
    count = str(count) #randomizer of the correct button

    if count == '1':
        bgcolor='blue'
    if count == '2':
        bgcolor='green'
    if count == '3':
        bgcolor='red'
    if count == '4':
        bgcolor='yellow'
    if count == '5':
        bgcolor='orange'
    if count == '6':
        bgcolor='brown'
    if count == '7':
        bgcolor='violet'
    if count == '8':
        bgcolor='grey'     
    if count == '9':
        bgcolor='pink'
    #bgcolor='blue' #random numbers and colors

    but_0 = Button(text=count, width=3, height=1, bg= bgcolor, font='Hack 30', command=click_button)
    print(bgcolor)
    but_0.place(x=310, y=140)
    but_0['state'] = 'disabled'

counter = 1000
def countdown():
    global timer, counter
    global clicks
    print(timer)
    timer -= 1
    counter -= 5
    if timer <= 0:
        clicks -= clicks
        timer = 5
    root.after(counter, countdown)
        
    but_10 = Button(text=timer, width=3, height=1, font='Hack 30', command=click_button)
    print(timer)
    but_10.place(x=310, y=40)
    but_10['state'] = 'disabled' #A button with the right button

labelClick = Label(root, text=clicks)
labelClick.pack()



but_1 = Button(text='1', width=3, height=1, bg='blue', font='Hack 30', command=click_button, true = 1)
    
but_2 = Button(text='2', width=3, height=1, bg='green', font='Hack 30', command=click_button, true = 2)
    
but_3 = Button(text='3', width=3, height=1, bg='red', font='Hack 30', command=click_button, true = 3)
    
but_4 = Button(text='4', width=3, height=1, bg='yellow', font='Hack 30', command=click_button, true = 4)
    
but_5 = Button(text='5', width=3, height=1, bg='orange', font='Hack 30', command=click_button, true = 5)
    
but_6 = Button(text='6', width=3, height=1, bg='brown', font='Hack 30', command=click_button, true = 6)
    
but_7 = Button(text='7', width=3, height=1, bg='violet', font='Hack 30', command=click_button, true = 7)
    
but_8 = Button(text='8', width=3, height=1, bg='grey', font='Hack 30', command=click_button, true = 8)
    
but_9 = Button(text='9', width=3, height=1, bg='pink', font='Hack 30', command=click_button, true = 9) #Buttons
    


if true == count:
    clicks += 1
else:
    clicks = 0



but_1.place(x=210, y=240)
but_2.place(x=310, y=240)
but_3.place(x=410, y=240)
but_4.place(x=210, y=340)
but_5.place(x=310, y=340)
but_6.place(x=410, y=340)
but_7.place(x=210, y=440)
but_8.place(x=310, y=440)
but_9.place(x=410, y=440) #button layout


root.after(1, countdown)
root.mainloop()
New contributor
kot may is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
\$\endgroup\$
3
  • 1
    \$\begingroup\$ 1) What have you tried? 2) It is usually 1 question per post 3) This is hard to read 4) Have you tried to use the search function as about 90% of your problems are covered and the last 10% are probably just copy/ paste errors \$\endgroup\$
    – Zibelas
    Jun 20 at 20:52
  • \$\begingroup\$ @Zibelas 1) Use the library random (so that the correct button is one and it changes), but it didn't work out. 4) I tried, but I couldn't find the right one \$\endgroup\$
    – kot may
    Jun 20 at 21:34
  • \$\begingroup\$ You realize that the error message is telling you exactly what's going on, right? unknown option "-true" The true parameter that you added is unexpected. \$\endgroup\$
    – Vaillancourt
    yesterday

2 Answers 2

1
\$\begingroup\$

Your question is a little unclear, and there seems to be some checks which are missing, so this answer won't give you a finished product. Also, you seem to be referencing saving the record (high score?) possibly to a file. File access is easy, well-documented, and I'm sure has been answered in a hundred different posts, so I won't add it here.

I suppose I'm just answering the timer question. I've also provided this answer in the style of the code you've already written.

For the timer you can initialize a global timer variable called timer using the line

timer = TIME = 5   #timer is the timer, TIME is the reset value

You should seed your randomizer, so I added that in there too. Inside your click_button() function you would include a global reference to timer

global clicks, timer

and you would reset it whenever there was a good click

timer = TIME

You would have another function which takes care of counting down the timer:

def countdown():
    global timer    #global reference to timer
    print(timer)    #print out current value
    timer -= 1      #decrement the timer by one
    if timer < 0:   #did it timeout?
        root.quit() #yes it did, so quit
    root.after(1000, countdown)    #waits 1000ms then runs countdown again

And at the bottom of your code, you would run mainloop like this:

run.after(1, countdown)    #starts the countdown timer
run.mainloop()             #enters the mainloop

You can also reduce the wait time between counter decrements by 1/20th of a second each time it's guessed correctly to provide some added challenge.

The modified code, with the added speed challenge, looks like this:

from tkinter import *
import random as rn
import time

random.seed()    
timer = TIME = 5

root = Tk()
root.title('game')
root.geometry('720x1280')

clicks = 0
def click_button():
    global clicks, timer
    clicks += 1
    timer = TIME
    labelClick['text'] = str(clicks)
    labelClick.pack() #Variable clicks

    count = rn.randrange(1,10,1)
    print(count)
    count = str(count) #randomizer of the correct button

    if count == '1':
        bgcolor='blue'
    if count == '2':
        bgcolor='green'
    if count == '3':
        bgcolor='red'
    if count == '4':
        bgcolor='yellow'
    if count == '5':
        bgcolor='orange'
    if count == '6':
        bgcolor='brown'
    if count == '7':
        bgcolor='violet'
    if count == '8':
        bgcolor='grey'     
    if count == '9':
        bgcolor='pink'
    #bgcolor='blue' #random numbers and colors



    but_0 = Button(text=count, width=3, height=1, bg= bgcolor, font='Hack 30', command=click_button)
    print(bgcolor)
    but_0.place(x=310, y=140)
    but_0['state'] = 'disabled' #A button with the right button
    
counter = 1000
def countdown():
    global timer, counter
    print(timer)
    timer -= 1
    counter -= 5
    if timer < 0:
        root.quit()
    root.after(counter, countdown)
        


labelClick = Label(root, text=clicks)
labelClick.pack()



but_1 = Button(text='1', width=3, height=1, bg='blue', font='Hack 30', command=click_button)
but_2 = Button(text='2', width=3, height=1, bg='green', font='Hack 30', command=click_button)
but_3 = Button(text='3', width=3, height=1, bg='red', font='Hack 30', command=click_button)
but_4 = Button(text='4', width=3, height=1, bg='yellow', font='Hack 30', command=click_button)
but_5 = Button(text='5', width=3, height=1, bg='orange', font='Hack 30', command=click_button)
but_6 = Button(text='6', width=3, height=1, bg='brown', font='Hack 30', command=click_button)
but_7 = Button(text='7', width=3, height=1, bg='violet', font='Hack 30', command=click_button)
but_8 = Button(text='8', width=3, height=1, bg='grey', font='Hack 30', command=click_button)
but_9 = Button(text='9', width=3, height=1, bg='pink', font='Hack 30', command=click_button) #Buttons


but_1.place(x=210, y=240)
but_2.place(x=310, y=240)
but_3.place(x=410, y=240)
but_4.place(x=210, y=340)
but_5.place(x=310, y=340)
but_6.place(x=410, y=340)
but_7.place(x=210, y=440)
but_8.place(x=310, y=440)
but_9.place(x=410, y=440) #button layout


root.after(1, countdown)
root.mainloop()
\$\endgroup\$
1
\$\begingroup\$

This question seems to have changed since the last time I answered it.

I don't remember there being a true= option for Button(), so I looked it up and no, there is no true= option. So take all those out.

Python allows you to add attributes to an object, so you could do something like this:

but_1 = Button(text='1', width=3, height=1, bg='blue', font='Hack 30', command=click_button)
but_1.true=1

And take this out:

 if true == count:
     clicks += 1
 else:
     clicks = 0
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .