Home > front end >  How to check if the mouse button is released outside the button? Python
How to check if the mouse button is released outside the button? Python

Time:10-18

I am trying to assign some behaviors to the buttons, some I have already achieved like:

  1. Change the color of the button if the mouse is positioned over it.
  2. Restore the default button color.
  3. Save the last button pressed in green.

Today I realized that when I press a button without releasing the click, and I move the mouse pointer off the button and release the click, it turns green, but without having executed the linked function, I would like the button not to change color . I am trying to eliminate this behavior, but I have no ideas. The code is executable, it works with python 3.7. Thanks.

from tkinter import *

class TypeButton(Button):
    def __init__(self, parent, *args, **kwargs):
        kwargs = {'font':('Calibri',9,'bold'), 'bg': '#11161d', 'fg':'white',
                  'width':10, 'bd':0, 'activebackground':'#bdfe04', **kwargs}
        super().__init__(parent, *args, **kwargs)

class Example(Frame):
    def __init__(self,parent, *args, **kwargs):
        super().__init__(parent, *args, **kwargs)
        self.frame = Frame(self, bg='#11161d')
        self.frame .grid(padx=(10,10), pady=(6,6))
        self.container1 = None

        self.creator_buttons()
        #????????????????
        self.bind_all("<B1-Motion>", self.callback)

    def creator_buttons(self):  

        mobiles = [['Frog', 'Fox', 'Boomer', 'Ice', 'J.d', 'Grub', 'Lightning', 'Aduka', 'Knight', 'Kalsiddon', 'Mage'],
                   ['Randomizer', 'Jolteon', 'Turtle', 'Armor', 'A.sate', 'Raon', 'Trico', 'Nak', 'Bigfoot', 'Barney', 'Dragon']]                
        self.mobiles2 = ['Fox','Knight','Jolteon','Barney','Dragon'] 

        self.buttons22 = []
        for index1, mobil in enumerate(mobiles):
            for index2, texto in enumerate(mobil):
                number = 11 if index1 == 1 else 0

                btn = B1ButtonCls (self.frame_1, text=texto, command= self.callback)            
                n1 = 5 if index2 == 0 else 0        
                n2 = 5 if index2 == 10 else 0
                btn .grid(column=index2 , row=index1 , pady=3, padx=(n1,n2))

                btn.bind("<Enter>", self.enter_mouse)
                btn.bind("<Leave>", self.leave_mouse)
                btn.bind("<Button-1>", self.clic_mouse)

                if texto in self.mobiles2: btn.config(fg='yellow')
                self.buttons22.append(btn)   

    def enter_mouse(self, event):
        widget1 = event.widget
        if not widget1 .cget('bg') == '#bdfe04': 
            widget1 .config(bg="#24364a")

    def leave_mouse(self, event):
        if not event.widget .cget('bg') == '#bdfe04':
            event.widget.config(bg='#11161d')                               

    def clic_mouse(self, event):
        widget1 = event.widget
        widget1.config(bg='#bdfe04', fg='black')
            
        if self.container1 is not None and self.container1 != widget1:
            if self.container1 .cget('text') in self.mobiles2:
                self.container1 .config (bg='#11161d', fg='yellow')
            else:
                self.container1 .config (bg='#11161d', fg='white')          
        self.container1 = widget1
    
    def callback(self):
        print('Closed')
 
root = Tk()
app = Example(root).pack()
root.mainloop()

CodePudding user response:

You can use the winfo_containing method to know which widget is under the mouse when you release a button. You can then compare the result of that function call to the widget that was clicked on.

Here's an example that displays one of two text messages depending on whether or not you released the mouse button over the same widget that was clicked on.

import tkinter as tk

def _button_press(event):
    label.configure(text="")

def _button_release(event):
    widget_under_cursor = event.widget.winfo_containing(event.x_root, event.y_root)
    if widget_under_cursor == event.widget:
        label.configure(text="you released over the button")
    else:
        label.configure(text="you did not release over the button")


root = tk.Tk()
label = tk.Label(root, width=40)
label.pack(side="top", fill="x")
for i in range(10):
    button = tk.Button(root, text=f"Button #{i 1}")
    button.pack()
    button.bind("<ButtonPress-1>", _button_press)
    button.bind("<ButtonRelease-1>", _button_release)

root.mainloop()
  • Related