source: trunk/anuga_core/source/anuga/pmesh/toolbarbutton.py @ 8879

Last change on this file since 8879 was 8690, checked in by steve, 12 years ago

Rolling in Padarn's update to fit_interpolation

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