Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- import os
- from PIL import Image, ImageTk, ImageDraw, ImageFont
- if getattr(sys, 'frozen', False): #windows path fix
- exe_path = os.path.dirname(sys.executable)
- elif __file__:
- exe_path = os.path.dirname(__file__)
- class RoundButton:
- def __init__(self, holder):
- self.holder = holder
- self.b = None
- self.icon_buider()
- def create_icon(self, url, w=32, h=32): #creates icons from the url filename
- im = Image.open(exe_path+url)
- im = im.resize((w, h), Image.ANTIALIAS)
- icon = ImageTk.PhotoImage(im)
- return icon
- def icon_buider(self):
- self.icons = {
- "normal":self.create_icon("\\button.gif"),
- "hover":self.create_icon("\\button_hover.gif"),
- "pressed":self.create_icon("\\button_pressed.gif")
- }
- def switch_icon(self, event, state):
- if state == 1:
- self.b.configure(image=self.icons["pressed"])
- elif state == 2:
- self.b.configure(image=self.icons["hover"])
- elif state == 3:
- self.b.configure(image=self.icons["normal"])
- def create_Button(self, **kwargs):
- self.b = Button(self.holder, **kwargs)
- self.b.configure(image=self.icons["normal"])
- self.b.bind("<Button-1>", lambda e=Event(), s=1:self.switch_icon(e, s))
- self.b.bind("<ButtonRelease-1>", lambda e=Event(), s=2:self.switch_icon(e, s))
- self.b.bind("<Enter>", lambda e=Event(), s=2:self.switch_icon(e, s))
- self.b.bind("<Leave>", lambda e=Event(), s=3:self.switch_icon(e, s))
- return self.b
- ##usage.
- #holderFrame = Frame(Master)
- #holderFrame.pack()
- #roundButton = RoundButton(holderFrame).create_Button(text="Hello World", compound=CENTER)##standard tkinter options supported. returns a normal button widget
- #roundButton.pack()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement