Home > other >  I am trying to make a button where if you click it it turns purple, button was originally blue, but
I am trying to make a button where if you click it it turns purple, button was originally blue, but

Time:01-26

I am trying to make a button where if you click it it turns purple, button was originally blue, what I made. But now I want button to change to blue if clicked twice or if double clicked. How to I keep track of clicks??

from tkinter import *
root = Tk()


def click_row_1_B():
    B_bingo_row_1.config(bg="#B900FF")
## if clicked twice :B_bingo_row_1.config(bg="#B900FF")

B_bingo_row_1 = Button(
    root, text="B1", bg="#00CCFF", font=("Helvetica", 20), command=click_row_1_B
)

B_bingo_row_1.grid(row=9, column=1, sticky="nsew")

root.mainloop()

CodePudding user response:

You can always bind and to one widget but with different functions

from tkinter import *
root = Tk()


def click_row_1_B(event):
    B_bingo_row_1.config(bg="#B900FF")
def double_click_row_1_B(event):
    B_bingo_row_1.config(bg="#00CCFF")

B_bingo_row_1 = Button(root, text="B1", bg="#00CCFF", font=("Helvetica", 20))
B_bingo_row_1.grid(row=9, column=1, sticky="nsew")

B_bingo_row_1.bind('<Double-Button-1>', double_click_row_1_B)
B_bingo_row_1.bind('<Button-1>', click_row_1_B)

root.mainloop()

For each widget, it's possible to bind Python functions and methods to an event.

widget.bind(event, handler)

A mouse button is pressed with the mouse pointer over the widget. The detail part specifies which button, e.g. The left mouse button is defined by the event Button-1, the middle button by Button-2, and the rightmost mouse button by Button-3.

Button-4 defines the scroll up event on mice with wheel support and and Button-5 the scroll down.

Similar to the Button event, see above, but the button is double clicked instead of a single click. To specify the left, middle or right mouse button use Double-Button-1, Double-Button-2, and Double-Button-3 respectively.

CodePudding user response:

This handles counting multiple single clicks and doing something based on that - not using the double clicked handler for fast double clicks - see Start after 1st click after 2nd click Start

  •  Tags:  
  • Related