Krish .G
Krish .G

Reputation: 1

how to bind button in tkinter

import tkinter as tk
from tkinter import ttk

root = tk.Tk()
root.title("Tic Tac Toe")
root.geometry("505x500")
root.resizable(0,0)

Blank = tk.PhotoImage(file='Blank.png')
X = tk.PhotoImage(file='X.png')
O = tk.PhotoImage(file='O.png')

def configB(event):
    print('hello')

btn1 = tk.Button(root,image=Blank)
btn1.place(x=0,y=0)

btn2 = ttk.Button(image=Blank)
btn2.place(x=165,y=0)
btn3 = ttk.Button(image=Blank)
btn3.place(x=330,y=0)
btn4 = ttk.Button(image=Blank)
btn4.place(x=0,y=165)
btn5 = ttk.Button(image=Blank)
btn5.place(x=165,y=165)
btn6 = ttk.Button(image=Blank)
btn6.place(x=330,y=165)
btn7 = ttk.Button(image=Blank)
btn7.place(x=0,y=330)
btn8 = ttk.Button(image=Blank)
btn8.place(x=165,y=330)
btn9 = ttk.Button(image=Blank)
btn9.place(x=330,y=330)

btn1.bind('<Return>',configB)

root.mainloop()

i want to bind btn1 and i want it to work when i press enter but nothing happens when i press enter as per my code it should print hello . please help thanks in advance.

Upvotes: 0

Views: 2428

Answers (3)

toyota Supra
toyota Supra

Reputation: 4537

You don't need parameter in configB method. Also don't need bind for button1. I also add command in button1 widget. Comment out in line 36 #btn1.bind('<Return>',configB)

def configB():
    print('hello')

btn1 = tk.Button(root, command=configB)

Result:

enter image description here

Btw, I don't have png image.

Upvotes: 0

Derek
Derek

Reputation: 2234

There is no quick answer to this question.

Buttons must be bound so as to duplicate (as close as possible) normal button behaviour.

This includes changing button relief and colors, then restoring button.

Finally it has to execute the button command.

The following example does this for two buttons.

  1. 'button' responds to Mouse 'Button-3'
  2. 'buttonX' responds to Key 'Return'
import tkinter as tk

def working():
    print("Working...")

def actionPress(event):
    event.widget.config(
        relief = "sunken",
        background = "red",
        foreground = "yellow")

def actionRelease(event):
    event.widget.config(
        relief = "raised",
        background = "SystemButtonFace",
        foreground = "SystemButtonText")
    # activate buttons' command on release
    event.widget.invoke()


window = tk.Tk()
button = tk.Button(window, text = "press", command = working)
button.pack()

buttonX = tk.Button(window, text = "pressX", command = working)
buttonX.pack()

# bind returns unique ID
boundP = button.bind("<ButtonPress-3>", actionPress)
boundR = button.bind("<ButtonRelease-3>", actionRelease)

boundXP = buttonX.bind("<KeyPress-Return>", actionPress)
boundXR = buttonX.bind("<KeyRelease-Return>", actionRelease)

# This is how to unbind (if necessary)
# button.unbind(""<ButtonPress-3>", boundP)
# button.unbind(""<ButtonRelease-3>", boundR)
# buttonX.unbind(""<KeyPress-Return>", boundXP)
# buttonX.unbind(""<KeyRelease-Return>", boundXR)

window.mainloop()

Upvotes: 0

furas
furas

Reputation: 142631

As @jasonharper said it will work only if button is focused

btn1.focus()
btn1.bind('<Return>', configB)

and if you click other button then it will not work again

so better bind to main winodw

root.bind('<Return>', configB)

Minimal working code

import tkinter as tk

# --- functions ---  # PEP8: lower_case_names

def config_b(event):
    print('hello')

# --- main ---

root = tk.Tk()

btn1 = tk.Button(root, text='1')
btn1.pack()

btn1 = tk.Button(root, text='2')
btn1.pack()

#btn1.focus()
#btn1.bind('<Return>', config_b)

root.bind('<Return>', config_b)

root.mainloop()

Upvotes: 2

Related Questions