Salome HOME
PN bug notation scientifique
[tools/eficas.git] / Tools / foztools / foztools.py
1 # -*- coding: utf-8 -*-
2 # $Header: /home/eficas/CVSROOT/EficasV1/Tools/foztools/foztools.py,v 1.1.1.1 2002/03/26 09:08:47 eficas Exp $
3
4 ###########################################################################
5 # This is a set of Python widgets, built on top of pythonTk.  They are
6 # designed to be highly customizable, flexible, and useful.  They are
7 # also all built from the base PythonTk widgets so no other external
8 # libraries are needed.
9 #
10 # Use it in good health.  It's hereby released under the GPL, if you
11 #  have questions about the GPL contact the Free Software Foundation.
12 #
13 # Author: Gary D. Foster <Gary.Foster@corp.sun.com>
14 #  with some ideas stolen from Mitch Chapman's stuff.
15 #
16 ###########################################################################
17
18 __version__ = "$Revision: 1.1.1.1 $"
19
20 import Tkinter
21 Tk=Tkinter
22
23 class Slider:
24     def __init__(self, master=None, orientation="horizontal", min=0, max=100,
25                  width=100, height=25, autoLabel="true", appearance="sunken",
26                  fillColor="blue", background="black", labelColor="yellow",
27                  labelText="", labelFormat="%d%%", value=50, bd=2):
28         # preserve various values
29         self.master=master
30         self.orientation=orientation
31         self.min=min
32         self.max=max
33         self.width=width
34         self.height=height
35         self.autoLabel=autoLabel
36         self.fillColor=fillColor
37         self.labelColor=labelColor
38         self.background=background
39         self.labelText=labelText
40         self.labelFormat=labelFormat
41         self.value=value
42         self.frame=Tk.Frame(master, relief=appearance, bd=bd)
43         self.canvas=Tk.Canvas(self.frame, height=height, width=width, bd=0,
44                               highlightthickness=0, background=background)
45         self.scale=self.canvas.create_rectangle(0, 0, width, height,
46                                                 fill=fillColor)
47         self.label=self.canvas.create_text(self.canvas.winfo_reqwidth() / 2,
48                                            height / 2, text=labelText,
49                                            anchor="c", fill=labelColor)
50         self.update()
51         self.canvas.pack(side='top', fill='x', expand='no')
52
53     def update(self):
54         # Trim the values to be between min and max
55         value=self.value
56         if value > self.max:
57             value = self.max
58         if value < self.min:
59             value = self.min
60         # Preserve the new value
61         c=self.canvas
62         # Adjust the rectangle
63         if self.orientation == "horizontal":
64             c.coords(self.scale,
65                      0, 0,
66                      float(value) / self.max * self.width, self.height)
67         else:
68             c.coords(self.scale,
69                      0, self.height - (float(value) / self.max*self.height),
70                      self.width, self.height)
71         # Now update the colors
72         c.itemconfig(self.scale, fill=self.fillColor)
73         c.itemconfig(self.label, fill=self.labelColor)
74         # And update the label
75         if self.autoLabel=="true":
76             c.itemconfig(self.label, text=self.labelFormat % value)
77         else:
78             c.itemconfig(self.label, text=self.labelFormat % self.labelText)
79         c.update_idletasks()
80
81 class Indicator:
82     def __init__(self, master=None, width=25, height=25, appearance="sunken",
83                  onColor="green", offColor="black", onLabelColor="black",
84                  offLabelColor="green", onLabelText="", offLabelText="",
85                  on=1, bd=2):
86         # preserve various values
87         self.master=master
88         self.onColor=onColor
89         self.offColor=offColor
90         self.onLabelColor=onLabelColor
91         self.offLabelColor=offLabelColor
92         self.onLabelText=onLabelText
93         self.offLabelText=offLabelText
94         self.on=on
95         self.frame=Tk.Frame(master, relief=appearance, bd=bd)
96         self.canvas=Tk.Canvas(self.frame, height=height, width=width, bd=0,
97                               highlightthickness=0)
98         self.light=self.canvas.create_rectangle(0, 0, width, height,
99                                                 fill=onLabelColor)
100         self.label=self.canvas.create_text(self.canvas.winfo_reqwidth() / 2,
101                                            height / 2, text=onLabelText,
102                                            anchor="c", fill=onLabelColor)
103         self.update()
104         self.canvas.pack(side="top", fill='x', expand='no')
105
106     def update(self):
107         c=self.canvas
108         # now update the status
109         if self.on:
110             c.itemconfig(self.light, fill=self.onColor)
111             c.itemconfig(self.label, fill=self.onLabelColor)
112             c.itemconfig(self.label, text=self.onLabelText)
113         else:
114             c.itemconfig(self.light, fill=self.offColor)
115             c.itemconfig(self.label, fill=self.offLabelColor)
116             c.itemconfig(self.label, text=self.offLabelText)
117         c.update_idletasks()
118
119     def toggle(self):
120         self.on=not self.on
121         self.update()
122
123     def turnon(self):
124         self.on=1
125         self.update()
126
127     def turnoff(self):
128         self.on=0
129         self.update()
130
131 class Blinker(Indicator):
132     def __init__(self, master=None, blinkrate=1, enabled=1, width=25,
133                  height=25, appearance="sunken", onColor="green",
134                  offColor="black", onLabelColor="black", offLabelColor="green",
135                  onLabelText="", offLabelText="", on=1, bd=2):
136         self.blinkrate=blinkrate
137         self.enabled=enabled
138         Indicator.__init__(self, master, width=width, height=height,
139                              appearance=appearance, onColor=onColor,
140                              offColor=offColor, onLabelColor=onLabelColor,
141                              offLabelColor=offLabelColor,
142                              onLabelText=onLabelText,
143                              offLabelText=offLabelText, on=on, bd=bd)
144
145     def update(self):
146         if self.enabled:
147             self.on=not self.on
148         Indicator.update(self)
149         self.frame.after(self.blinkrate * 1000, self.update)
150
151