Advertisement
AceScottie

tkRoundButtons.py

Sep 5th, 2019
431
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.65 KB | None | 0 0
  1. from tkinter import *
  2. import os
  3. from PIL import Image, ImageTk, ImageDraw, ImageFont
  4.  
  5. if getattr(sys, 'frozen', False): #windows path fix
  6.     exe_path = os.path.dirname(sys.executable)
  7. elif __file__:
  8.     exe_path = os.path.dirname(__file__)
  9.    
  10. class RoundButton:
  11.     def __init__(self, holder):
  12.         self.holder = holder
  13.         self.b = None
  14.         self.icon_buider()
  15.     def create_icon(self, url, w=32, h=32): #creates icons from the url filename
  16.         im = Image.open(exe_path+url)
  17.         im = im.resize((w, h), Image.ANTIALIAS)
  18.         icon = ImageTk.PhotoImage(im)
  19.         return icon
  20.     def icon_buider(self):
  21.         self.icons = {
  22.         "normal":self.create_icon("\\button.gif"),
  23.         "hover":self.create_icon("\\button_hover.gif"),
  24.         "pressed":self.create_icon("\\button_pressed.gif")
  25.         }
  26.     def switch_icon(self, event, state):
  27.         if state == 1:
  28.             self.b.configure(image=self.icons["pressed"])
  29.         elif state == 2:
  30.             self.b.configure(image=self.icons["hover"])
  31.         elif state == 3:
  32.             self.b.configure(image=self.icons["normal"])
  33.     def create_Button(self, **kwargs):
  34.         self.b = Button(self.holder, **kwargs)
  35.         self.b.configure(image=self.icons["normal"])
  36.         self.b.bind("<Button-1>", lambda e=Event(), s=1:self.switch_icon(e, s))
  37.         self.b.bind("<ButtonRelease-1>", lambda e=Event(), s=2:self.switch_icon(e, s))
  38.         self.b.bind("<Enter>", lambda e=Event(), s=2:self.switch_icon(e, s))
  39.         self.b.bind("<Leave>", lambda e=Event(), s=3:self.switch_icon(e, s))
  40.         return self.b
  41.  
  42. ##usage.
  43. #holderFrame = Frame(Master)
  44. #holderFrame.pack()
  45. #roundButton = RoundButton(holderFrame).create_Button(text="Hello World", compound=CENTER)##standard tkinter options supported. returns a normal button widget
  46. #roundButton.pack()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement