| 1 | title = 'Pmw LogicalFont 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 | |
| 13 | # The fonts to demonstrate. |
| 14 | fontList = ( |
| 15 | (('Times', 0), {}), |
| 16 | (('Helvetica', 0), {}), |
| 17 | (('Typewriter', 0), {}), |
| 18 | (('Fixed', 0), {}), |
| 19 | (('Courier', 0), {}), |
| 20 | (('Helvetica', 2), {'slant' : 'italic'}), |
| 21 | (('Helvetica', 0), {'size' : 18}), |
| 22 | (('Helvetica', 0), {'weight' : 'bold'}), |
| 23 | (('Helvetica', 12), {'weight' : 'bold', 'slant' : 'italic'}), |
| 24 | (('Typewriter', 0), {'size' : 8, 'weight' : 'bold'}), |
| 25 | (('Fixed', 0), {'size' : 8, 'weight' : 'bold'}), |
| 26 | (('Times', 0), {'size' : 24, 'weight' : 'bold', 'slant' : 'italic'}), |
| 27 | (('Typewriter', 0), {'width' : 'condensed'}), |
| 28 | (('Typewriter', -1), {'width' : 'condensed'}), |
| 29 | (('Fixed', 0), {'width' : 'condensed'}), |
| 30 | (('Fixed', -1), {'width' : 'condensed'}), |
| 31 | (('Helvetica', 0), {'weight' : 'bogus'}), |
| 32 | ) |
| 33 | |
| 34 | fontText = [] |
| 35 | |
| 36 | def __init__(self, parent): |
| 37 | |
| 38 | self.parent = parent |
| 39 | |
| 40 | # Create the text to display to the user to represent each font. |
| 41 | if Demo.fontText == []: |
| 42 | for args, dict in Demo.fontList: |
| 43 | text = args[0] |
| 44 | if args[1] != 0: |
| 45 | text = text + ' ' + str(args[1]) |
| 46 | for name, value in dict.items(): |
| 47 | text = text + ' ' + name + ': ' + str(value) |
| 48 | Demo.fontText.append(text) |
| 49 | |
| 50 | # Create a listbox to contain the font selections. |
| 51 | self.box = Pmw.ScrolledListBox(parent, listbox_selectmode='single', |
| 52 | listbox_width = 35, |
| 53 | listbox_height = 10, |
| 54 | items=Demo.fontText, |
| 55 | label_text='Font', labelpos='nw', |
| 56 | selectioncommand=self.selectionCommand) |
| 57 | self.box.pack(fill = 'both', expand = 1, padx = 10, pady = 10) |
| 58 | |
| 59 | # Create a label to display the selected font. |
| 60 | self.target = Tkinter.Label(parent, |
| 61 | text = 'The quick brown fox jumps\nover the lazy dog', |
| 62 | relief = 'sunken', padx = 10, pady = 10) |
| 63 | self.target.pack(fill = 'both', expand = 1, padx = 10, pady = 10) |
| 64 | |
| 65 | def selectionCommand(self): |
| 66 | sel = self.box.curselection() |
| 67 | if len(sel) > 0: |
| 68 | args, dict = Demo.fontList[string.atoi(sel[0])] |
| 69 | font = apply(Pmw.logicalfont, args, dict) |
| 70 | self.target.configure(font = font) |
| 71 | print font |
| 72 | |
| 73 | ###################################################################### |
| 74 | |
| 75 | # Create demo in root window for testing. |
| 76 | if __name__ == '__main__': |
| 77 | root = Tkinter.Tk() |
| 78 | Pmw.initialise(root) |
| 79 | root.title(title) |
| 80 | |
| 81 | exitButton = Tkinter.Button(root, text = 'Exit', command = root.destroy) |
| 82 | exitButton.pack(side = 'bottom') |
| 83 | widget = Demo(root) |
| 84 | root.mainloop() |