0
import tkinter
import random

# GUI
window = tkinter.Tk()
window.title("Hangman")
window.geometry("640x400+100+100")
frame = tkinter.Frame(window)
frame.pack()
button_animal = tkinter.Button(frame)
button_animal['text'] = "Animal"
button_animal['background'] = 'yellow'
button_animal.pack()
button_capital = tkinter.Button(frame)
button_capital['text'] = "Capital"
button_capital['background'] = 'blue'
button_capital.pack()

This is what I wrote so far, what am I supposed to do to make the button to make event happens?

if I click animal, it should play a game, but I am not sure how to do at first.

1
  • You should take a look at some documentation about tkinter (for instance here. Buttons have a command option to execute a function when clicked. Commented Dec 14, 2018 at 9:39

1 Answer 1

3

You should add command parameter to your button initialization. You can find more information in here tkinter button widget

Basically, you can bind your button to function in two ways:

button_animal = tkinter.Button(frame, command=your_function)

or if your function requires arguments, you can use lambda, like:

button_animal = tkinter.Button(frame, command=lambda : your_function(arg))

If you want to bind multiple function, you can do following:

button_animal = tkinter.Button(frame, command=lambda :[funct1(arg),funct2(arg)])
Sign up to request clarification or add additional context in comments.

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.