| 1 | title = 'Color spectrum demonstration' |
| 2 | |
| 3 | # Import Pmw from this directory tree. |
| 4 | import sys |
| 5 | sys.path[:0] = ['../../..'] |
| 6 | |
| 7 | import string |
| 8 | import Tkinter |
| 9 | import Pmw |
| 10 | |
| 11 | class Demo: |
| 12 | def __init__(self, parent): |
| 13 | parent = Tkinter.Frame(parent) |
| 14 | parent.pack(padx=10, pady=10, fill='both', expand=1) |
| 15 | self.width = 350 |
| 16 | self.height = 250 |
| 17 | self.canvas = Tkinter.Canvas(parent, |
| 18 | width = self.width, height = self.height) |
| 19 | self.canvas.grid(row = 0, column = 0, columnspan = 2, sticky = 'news') |
| 20 | |
| 21 | self.numColors = Pmw.EntryField(parent, |
| 22 | labelpos = 'w', |
| 23 | label_text = 'Number of colors:', |
| 24 | entry_width = 5, |
| 25 | validate = 'numeric', |
| 26 | command = Pmw.busycallback(self.execute)) |
| 27 | self.numColors.grid(row = 1, column = 0, sticky = 'ew') |
| 28 | |
| 29 | self.correction = Pmw.EntryField(parent, |
| 30 | labelpos = 'w', |
| 31 | label_text = 'Correction:', |
| 32 | validate = 'real', |
| 33 | entry_width = 5, |
| 34 | command = Pmw.busycallback(self.execute)) |
| 35 | self.correction.grid(row = 1, column = 1, sticky = 'ew') |
| 36 | |
| 37 | self.saturation = Pmw.EntryField(parent, |
| 38 | labelpos = 'w', |
| 39 | label_text = 'Saturation:', |
| 40 | validate = 'real', |
| 41 | entry_width = 5, |
| 42 | command = Pmw.busycallback(self.execute)) |
| 43 | self.saturation.grid(row = 2, column = 0, sticky = 'ew') |
| 44 | |
| 45 | self.intensity = Pmw.EntryField(parent, |
| 46 | labelpos = 'w', |
| 47 | label_text = 'Intensity:', |
| 48 | validate = 'real', |
| 49 | entry_width = 5, |
| 50 | command = Pmw.busycallback(self.execute)) |
| 51 | self.intensity.grid(row = 2, column = 1, sticky = 'ew') |
| 52 | |
| 53 | self.extraOrange = Pmw.EntryField(parent, |
| 54 | labelpos = 'w', |
| 55 | label_text = 'Emphasize orange (0 or 1):', |
| 56 | validate = {'validator' : 'numeric', 'min' : 0, 'max' : 1}, |
| 57 | entry_width = 5, |
| 58 | command = Pmw.busycallback(self.execute)) |
| 59 | self.extraOrange.grid(row = 3, column = 0, sticky = 'ew') |
| 60 | |
| 61 | self.text = Pmw.EntryField(parent, |
| 62 | labelpos = 'w', |
| 63 | label_text = 'Text:', |
| 64 | entry_width = 20, |
| 65 | command = Pmw.busycallback(self.execute)) |
| 66 | self.text.grid(row = 4, column = 0, sticky = 'ew') |
| 67 | |
| 68 | self.brightness = Pmw.EntryField(parent, |
| 69 | labelpos = 'w', |
| 70 | label_text = 'Brightness:', |
| 71 | validate = 'real', |
| 72 | entry_width = 5, |
| 73 | command = Pmw.busycallback(self.execute)) |
| 74 | self.brightness.grid(row = 3, column = 1, sticky = 'ew') |
| 75 | |
| 76 | self.radiobuttons = Pmw.RadioSelect(parent, |
| 77 | command = Pmw.busycallback(self.radio_cb), |
| 78 | ) |
| 79 | self.radiobuttons.grid(row = 4, column = 1) |
| 80 | self.radiobuttons.add('Use saturation\nand intensity') |
| 81 | self.radiobuttons.add('Use\nbrightness') |
| 82 | |
| 83 | parent.grid_columnconfigure(0, weight = 1) |
| 84 | parent.grid_columnconfigure(1, weight = 1) |
| 85 | parent.grid_rowconfigure(0, weight = 1) |
| 86 | |
| 87 | Pmw.alignlabels((self.numColors, self.saturation, self.extraOrange)) |
| 88 | Pmw.alignlabels((self.correction, self.intensity, self.brightness)) |
| 89 | |
| 90 | # Set initial values for all entries. |
| 91 | self.numColors.setentry('64') |
| 92 | self.correction.setentry('1.0') |
| 93 | self.saturation.setentry('1.0') |
| 94 | self.intensity.setentry('1.0') |
| 95 | self.extraOrange.setentry('1') |
| 96 | self.brightness.setentry('0.7') |
| 97 | self.text.setentry('This is a test') |
| 98 | self.radiobuttons.invoke('Use saturation\nand intensity') |
| 99 | |
| 100 | self.execute() |
| 101 | |
| 102 | def radio_cb(self, value): |
| 103 | self.execute() |
| 104 | |
| 105 | def execute(self): |
| 106 | try: |
| 107 | numColors = string.atoi(self.numColors.get()) |
| 108 | correction = string.atof(self.correction.get()) |
| 109 | saturation = string.atof(self.saturation.get()) |
| 110 | intensity = string.atof(self.intensity.get()) |
| 111 | extraOrange = string.atof(self.extraOrange.get()) |
| 112 | brightness = string.atof(self.brightness.get()) |
| 113 | except ValueError: |
| 114 | self.numColors.bell() |
| 115 | return |
| 116 | |
| 117 | if numColors <= 0: |
| 118 | self.numColors.bell() |
| 119 | return |
| 120 | |
| 121 | self.canvas.delete('all') |
| 122 | |
| 123 | colorList = Pmw.Color.spectrum( |
| 124 | numColors, correction, saturation, intensity, extraOrange) |
| 125 | extent = 360.0 / numColors |
| 126 | |
| 127 | useBrightness = \ |
| 128 | (self.radiobuttons.getcurselection() == 'Use\nbrightness') |
| 129 | |
| 130 | if numColors == 1: |
| 131 | # Special case circle, since create_arc does not work when |
| 132 | # extent is 360. |
| 133 | background = colorList[0] |
| 134 | if useBrightness: |
| 135 | background = Pmw.Color.changebrightness( |
| 136 | self.canvas, background, brightness) |
| 137 | self.canvas.create_oval(10, 10, self.width - 10, self.height - 10, |
| 138 | fill = background, outline = background) |
| 139 | |
| 140 | for index in range(numColors): |
| 141 | start = index * extent - extent / 2 |
| 142 | background = colorList[index] |
| 143 | if useBrightness: |
| 144 | background = Pmw.Color.changebrightness( |
| 145 | self.canvas, background, brightness) |
| 146 | self.canvas.create_arc(10, 10, self.width - 10, self.height - 10, |
| 147 | start = start, extent = extent, |
| 148 | fill = background, outline = background) |
| 149 | |
| 150 | text = self.text.get() |
| 151 | self.canvas.create_text(self.width / 2, self.height / 3, text = text) |
| 152 | self.canvas.create_text(self.width / 2, self.height / 2, text = text) |
| 153 | self.canvas.create_text(self.width / 2, 2 * self.height / 3, text = text) |
| 154 | |
| 155 | ###################################################################### |
| 156 | |
| 157 | # Create demo in root window for testing. |
| 158 | if __name__ == '__main__': |
| 159 | root = Tkinter.Tk() |
| 160 | Pmw.initialise(root) |
| 161 | root.title(title) |
| 162 | |
| 163 | exitButton = Tkinter.Button(root, text = 'Exit', command = root.destroy) |
| 164 | exitButton.pack(side = 'bottom') |
| 165 | widget = Demo(root) |
| 166 | root.mainloop() |