0

I have this game where you need to choose the text and not the color it is in:

import tkinter, os, random

colors = ['Green', 'Red', 'Blue','Yellow', 'Pink', 'Orange', 'Purple', 'Grey', 'Brown', 'Black']

window = tkinter.Tk()
os.chdir(os.path.dirname(os.path.abspath(__file__)))

def color(color):
    colors.remove(color)
    text = random.choice(colors)
    label = tkinter.Label(window, text=color, fg = text, highlightthickness = 20)
    label.config(font=("Calibri", 44))
    buttonT = tkinter.Button(window, text=text)
    buttonF = tkinter.Button(window, text=color)
    colors.append(color)
    label.pack()
    buttonT.pack()
    buttonF.pack()

os.chdir(os.path.dirname(os.path.abspath(__file__)))

window.title('Color Game')
window.geometry('250x250')
instructions = tkinter.Label(window, text = 'Select word, not color!')
instructions.pack()
window.iconbitmap('icon.ico')
color(random.choice(colors))

window.mainloop()

It produces this window: enter image description here

How can I check which button the user clicks in order to determine whether his answer is correct? Can you please specify how I should implement your answer in my code? How does it work?

Thanks in advance.

  • Stavros
3
  • @BillalBEGUERADJ yes. How do I do that, and how should I implement it in my code? Commented May 15, 2017 at 18:07
  • @BillalBEGUERADJ thanks! Commented May 15, 2017 at 18:54
  • @StavrosMichalovits Please check my answer. I have updated it and added lambda function in your code. Commented May 16, 2017 at 8:54

2 Answers 2

4

You can use lambda functions for this.

def populateMethod(self, method):
    print "method:", method

for method in ["Red","Green","Blue"]:
    button = Button(window, text=method, 
        command=lambda m=method: self.populateMethod(m))

UPDATE:

I have modified your code and added the lambda function. Check and let me know if it works fine.

import tkinter, os, random

colors = ['Green', 'Red', 'Blue','Yellow', 'Pink', 'Orange', 'Purple', 'Grey', 'Brown', 'Black']

window = tkinter.Tk()
os.chdir(os.path.dirname(os.path.abspath(__file__)))


def populateMethod(method):
    print ("method:", method)

def color(color):
    colors.remove(color)
    text = random.choice(colors)
    label = tkinter.Label(window, text=color, fg = text, highlightthickness = 20)
    label.config(font=("Calibri", 44))
    buttonT = tkinter.Button(window, text=text,command=lambda m=text: populateMethod(m))
    buttonF = tkinter.Button(window, text=color,command=lambda m=color: populateMethod(m))
    colors.append(color)
    label.pack()
    buttonT.pack()
    buttonF.pack()

os.chdir(os.path.dirname(os.path.abspath(__file__)))

window.title('Color Game')
window.geometry('250x250')
instructions = tkinter.Label(window, text = 'Select word, not color!')
instructions.pack()
# window.iconbitmap('icon.ico')
color(random.choice(colors))

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

1 Comment

Sure @StavrosMichalovits
1

You should use command parameter on button, like this:

def callback():
    print('button t clicked')

buttonT = tkinter.Button(window, text=text, command=callback)

1 Comment

How do I implement this in my code, and how does it work?

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.