[349] | 1 | from Tkinter import * |
---|
| 2 | import string, time |
---|
| 3 | |
---|
| 4 | class ToolBarButton(Label): |
---|
| 5 | def __init__(self, top, parent, tag=None, image=None, command=None, |
---|
| 6 | statushelp='', balloonhelp='', height=21, width=21, |
---|
| 7 | bd=1, activebackground='lightgrey', padx=0, pady=0, |
---|
| 8 | state='normal', bg='grey'): |
---|
| 9 | Label.__init__(self, parent, height=height, width=width, |
---|
| 10 | relief='flat', bd=bd, bg=bg) |
---|
| 11 | self.bg = bg |
---|
| 12 | self.activebackground = activebackground |
---|
| 13 | if image != None: |
---|
| 14 | if string.splitfields(image, '.')[1] == 'bmp': |
---|
| 15 | self.Icon = BitmapImage(file='icons/%s' % image) |
---|
| 16 | else: |
---|
| 17 | self.Icon = PhotoImage(file='icons/%s' % image) |
---|
| 18 | else: |
---|
| 19 | self.Icon = PhotoImage(file='icons/blank.gif') |
---|
| 20 | self.config(image=self.Icon) |
---|
| 21 | self.tag = tag |
---|
| 22 | self.icommand = command |
---|
| 23 | self.command = self.activate |
---|
| 24 | self.bind("<Enter>", self.buttonEnter) |
---|
| 25 | self.bind("<Leave>", self.buttonLeave) |
---|
| 26 | self.bind("<ButtonPress-1>", self.buttonDown) |
---|
| 27 | self.bind("<ButtonRelease-1>", self.buttonUp) |
---|
| 28 | self.pack(side='left', anchor=NW, padx=padx, pady=pady) |
---|
| 29 | if balloonhelp or statushelp: |
---|
| 30 | top.balloon().bind(self, balloonhelp, statushelp) |
---|
| 31 | self.state = state |
---|
| 32 | |
---|
| 33 | def activate(self): |
---|
| 34 | self.icommand(self.tag) |
---|
| 35 | |
---|
| 36 | def buttonEnter(self, event): |
---|
| 37 | if self.state != 'disabled': |
---|
| 38 | self.config(relief='raised', bg=self.bg) |
---|
| 39 | |
---|
| 40 | def buttonLeave(self, event): |
---|
| 41 | if self.state != 'disabled': |
---|
| 42 | self.config(relief='flat', bg=self.bg) |
---|
| 43 | |
---|
| 44 | def buttonDown(self, event): |
---|
| 45 | if self.state != 'disabled': |
---|
| 46 | self.config(relief='sunken', bg=self.activebackground) |
---|
| 47 | |
---|
| 48 | def buttonUp(self, event): |
---|
| 49 | if self.state != 'disabled': |
---|
| 50 | if self.command != None: |
---|
| 51 | self.command() |
---|
| 52 | time.sleep(0.05) |
---|
| 53 | if (self in ToolBarButton.cyclelist): |
---|
| 54 | if (ToolBarButton.sunkenbutton) and ToolBarButton.sunkenbutton != self: |
---|
| 55 | ToolBarButton.sunkenbutton.config(relief='flat', bg=self.bg) |
---|
| 56 | ToolBarButton.sunkenbutton = self |
---|
| 57 | else: |
---|
| 58 | self.config(relief='flat', bg=self.bg) |
---|
| 59 | |
---|
| 60 | def enable(self): |
---|
| 61 | self.state = 'normal' |
---|
| 62 | |
---|
| 63 | def disable(self): |
---|
| 64 | self.state = 'disabled' |
---|
| 65 | |
---|
| 66 | def cycle(self, name): |
---|
| 67 | """Add button to list of buttons where one is left sunken, untill |
---|
| 68 | the next button is pressed.""" |
---|
| 69 | ToolBarButton.cyclelist.append(self) |
---|
| 70 | def setInitialSunkenButton(self, name): |
---|
| 71 | if not ToolBarButton.sunkenbutton: |
---|
| 72 | ToolBarButton.sunkenbutton = self |
---|
| 73 | self.config(relief='sunken', bg=self.activebackground) |
---|
| 74 | |
---|
| 75 | #class variable |
---|
| 76 | cyclelist = [] |
---|
| 77 | sunkenbutton = None |
---|