Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- ''' hovering button '''
- ''' normal `activebackground` works on hovering in Linux but not in other systems '''
- # ---
- class RedButton(Button):
- def __init__(self, parent, **options):
- Button.__init__(self, parent, **options)
- self['bg'] = 'red'
- self['fg'] = 'black'
- # or self.config(bg='red')
- self['activebackground'] = 'blue'
- self['activeforeground'] = 'white'
- self.bind('<Enter>', self.on_hover)
- self.bind('<Leave>', self.on_unhover)
- #self.bind('<Button-1>', self.on_left_mouse_click)
- #self.bind('<Button-2>', self.on_middle_mouse_click)
- #self.bind('<Button-3>', self.on_right_mouse_click)
- #self.bind('<Double-Button-1>', self.on_left_mouse_doubleclick)
- #self.bind('<Triple-Button-1>', self.on_left_mouse_tripleclick)
- def on_hover(self, event=None):
- #print('hover')
- self['bg'] = 'blue'
- self['fg'] = 'white'
- self['activebackground'] = 'blue'
- self['activeforeground'] = 'white'
- def on_unhover(self, event=None):
- #print('unhover')
- self['bg'] = 'red'
- self['fg'] = 'black'
- self['activebackground'] = 'red'
- self['activeforeground'] = 'black'
- # ---
- def on_left_mouse_click(self, event=None):
- print('nuclear missile launched')
- def on_right_mouse_click(self, event=None):
- print('we came with peace')
- def on_middle_mouse_click(self, event=None):
- print('where am I ?')
- def on_left_mouse_doubleclick(self, event=None):
- print('double nuclear missile launched')
- def on_left_mouse_tripleclick(self, event=None):
- print('triple nuclear missile launched')
- # ---
- window = Tk()
- RedButton(window, text="Button #1").pack()
- RedButton(window, text="Button #2").pack()
- RedButton(window, text="Button #3").pack()
- window.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement