Advertisement
furas

Python - Tkinter - Button hovering

Jan 9th, 2016
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.96 KB | None | 0 0
  1. from tkinter import *
  2.  
  3. ''' hovering button '''
  4.  
  5. ''' normal `activebackground` works on hovering in Linux but not in other systems '''
  6.  
  7. # ---
  8.  
  9. class RedButton(Button):
  10.  
  11.     def __init__(self, parent, **options):
  12.         Button.__init__(self, parent, **options)
  13.  
  14.         self['bg'] = 'red'
  15.         self['fg'] = 'black'
  16.  
  17.         # or self.config(bg='red')
  18.  
  19.         self['activebackground'] = 'blue'
  20.         self['activeforeground'] = 'white'
  21.  
  22.         self.bind('<Enter>', self.on_hover)
  23.         self.bind('<Leave>', self.on_unhover)
  24.  
  25.         #self.bind('<Button-1>', self.on_left_mouse_click)
  26.         #self.bind('<Button-2>', self.on_middle_mouse_click)
  27.         #self.bind('<Button-3>', self.on_right_mouse_click)
  28.  
  29.         #self.bind('<Double-Button-1>', self.on_left_mouse_doubleclick)
  30.         #self.bind('<Triple-Button-1>', self.on_left_mouse_tripleclick)
  31.  
  32.     def on_hover(self, event=None):
  33.         #print('hover')
  34.  
  35.         self['bg'] = 'blue'
  36.         self['fg'] = 'white'
  37.  
  38.         self['activebackground'] = 'blue'
  39.         self['activeforeground'] = 'white'
  40.  
  41.     def on_unhover(self, event=None):
  42.         #print('unhover')
  43.  
  44.         self['bg'] = 'red'
  45.         self['fg'] = 'black'
  46.  
  47.         self['activebackground'] = 'red'
  48.         self['activeforeground'] = 'black'
  49.        
  50.     # ---
  51.  
  52.     def on_left_mouse_click(self, event=None):
  53.         print('nuclear missile launched')
  54.  
  55.     def on_right_mouse_click(self, event=None):
  56.         print('we came with peace')
  57.        
  58.     def on_middle_mouse_click(self, event=None):
  59.         print('where am I ?')
  60.        
  61.     def on_left_mouse_doubleclick(self, event=None):
  62.         print('double nuclear missile launched')
  63.  
  64.     def on_left_mouse_tripleclick(self, event=None):
  65.         print('triple nuclear missile launched')
  66.  
  67. # ---
  68.  
  69. window = Tk()
  70.  
  71. RedButton(window, text="Button #1").pack()
  72. RedButton(window, text="Button #2").pack()
  73. RedButton(window, text="Button #3").pack()
  74.  
  75. window.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement