Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I am trying to make a button where if you click it it turns purple, button was originally blue, but if clicked again then it turn back to blue

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()

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading