from Tkinter import * import string, time class ToolBarButton(Label): def __init__(self, top, parent, tag=None, image=None, command=None, statushelp='', balloonhelp='', height=21, width=21, bd=1, activebackground='lightgrey', padx=0, pady=0, state='normal', bg='grey'): Label.__init__(self, parent, height=height, width=width, relief='flat', bd=bd, bg=bg) self.bg = bg self.activebackground = activebackground if image != None: if string.splitfields(image, '.')[1] == 'bmp': self.Icon = BitmapImage(file='icons/%s' % image) else: self.Icon = PhotoImage(file='icons/%s' % image) else: self.Icon = PhotoImage(file='icons/blank.gif') self.config(image=self.Icon) self.tag = tag self.icommand = command self.command = self.activate self.bind("", self.buttonEnter) self.bind("", self.buttonLeave) self.bind("", self.buttonDown) self.bind("", self.buttonUp) self.pack(side='left', anchor=NW, padx=padx, pady=pady) if balloonhelp or statushelp: top.balloon().bind(self, balloonhelp, statushelp) self.state = state def activate(self): self.icommand(self.tag) def buttonEnter(self, event): if self.state != 'disabled': self.config(relief='raised', bg=self.bg) def buttonLeave(self, event): if self.state != 'disabled': self.config(relief='flat', bg=self.bg) def buttonDown(self, event): if self.state != 'disabled': self.config(relief='sunken', bg=self.activebackground) def buttonUp(self, event): if self.state != 'disabled': if self.command != None: self.command() time.sleep(0.05) if (self in ToolBarButton.cyclelist): if (ToolBarButton.sunkenbutton) and ToolBarButton.sunkenbutton != self: ToolBarButton.sunkenbutton.config(relief='flat', bg=self.bg) ToolBarButton.sunkenbutton = self else: self.config(relief='flat', bg=self.bg) def enable(self): self.state = 'normal' def disable(self): self.state = 'disabled' def cycle(self, name): """Add button to list of buttons where one is left sunken, untill the next button is pressed.""" ToolBarButton.cyclelist.append(self) def setInitialSunkenButton(self, name): if not ToolBarButton.sunkenbutton: ToolBarButton.sunkenbutton = self self.config(relief='sunken', bg=self.activebackground) #class variable cyclelist = [] sunkenbutton = None