Martin v. Löwis | 20efa68 | 2001-11-11 14:07:37 +0000 | [diff] [blame] | 1 | # -*-mode: python; fill-column: 75; tab-width: 8; coding: iso-latin-1-unix -*- |
| 2 | # |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 3 | # $Id$ |
Martin v. Löwis | 20efa68 | 2001-11-11 14:07:37 +0000 | [diff] [blame] | 4 | # |
| 5 | # Tix Demostration Program |
| 6 | # |
| 7 | # This sample program is structured in such a way so that it can be |
Martin v. Löwis | 8ec03e0 | 2002-03-17 18:19:13 +0000 | [diff] [blame] | 8 | # executed from the Tix demo program "tixwidgets.py": it must have a |
Martin v. Löwis | 20efa68 | 2001-11-11 14:07:37 +0000 | [diff] [blame] | 9 | # procedure called "RunSample". It should also have the "if" statment |
| 10 | # at the end of this file so that it can be run as a standalone |
| 11 | # program using tixwish. |
| 12 | |
| 13 | # This file demonstrates the use of the tixDirTree widget -- you can |
| 14 | # use it for the user to select a directory. For example, an installation |
| 15 | # program can use the tixDirTree widget to ask the user to select the |
| 16 | # installation directory for an application. |
| 17 | # |
| 18 | |
| 19 | import Tix, os, copy |
| 20 | from Tkconstants import * |
| 21 | |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 22 | TCL_ALL_EVENTS = 0 |
Martin v. Löwis | 8ec03e0 | 2002-03-17 18:19:13 +0000 | [diff] [blame] | 23 | |
| 24 | def RunSample (root): |
| 25 | dirtree = DemoDirTree(root) |
| 26 | dirtree.mainloop() |
| 27 | dirtree.destroy() |
Martin v. Löwis | 20efa68 | 2001-11-11 14:07:37 +0000 | [diff] [blame] | 28 | |
| 29 | class DemoDirTree: |
| 30 | def __init__(self, w): |
| 31 | self.root = w |
Martin v. Löwis | 8ec03e0 | 2002-03-17 18:19:13 +0000 | [diff] [blame] | 32 | self.exit = -1 |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 33 | |
Martin v. Löwis | 20efa68 | 2001-11-11 14:07:37 +0000 | [diff] [blame] | 34 | z = w.winfo_toplevel() |
Martin v. Löwis | 8ec03e0 | 2002-03-17 18:19:13 +0000 | [diff] [blame] | 35 | z.wm_protocol("WM_DELETE_WINDOW", lambda self=self: self.quitcmd()) |
| 36 | |
Martin v. Löwis | 20efa68 | 2001-11-11 14:07:37 +0000 | [diff] [blame] | 37 | # Create the tixDirTree and the tixLabelEntry widgets on the on the top |
| 38 | # of the dialog box |
| 39 | |
| 40 | # bg = root.tk.eval('tix option get bg') |
| 41 | # adding bg=bg crashes Windows pythonw tk8.3.3 Python 2.1.0 |
| 42 | |
| 43 | top = Tix.Frame( w, relief=RAISED, bd=1) |
| 44 | |
| 45 | # Create the DirTree widget. By default it will show the current |
| 46 | # directory |
| 47 | # |
| 48 | # |
| 49 | top.dir = Tix.DirTree(top) |
| 50 | top.dir.hlist['width'] = 40 |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 51 | |
Martin v. Löwis | 20efa68 | 2001-11-11 14:07:37 +0000 | [diff] [blame] | 52 | # When the user presses the ".." button, the selected directory |
| 53 | # is "transferred" into the entry widget |
| 54 | # |
| 55 | top.btn = Tix.Button(top, text = " >> ", pady = 0) |
| 56 | |
| 57 | # We use a LabelEntry to hold the installation directory. The user |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 58 | # can choose from the DirTree widget, or he can type in the directory |
Martin v. Löwis | 20efa68 | 2001-11-11 14:07:37 +0000 | [diff] [blame] | 59 | # manually |
| 60 | # |
| 61 | top.ent = Tix.LabelEntry(top, label="Installation Directory:", |
| 62 | labelside = 'top', |
| 63 | options = ''' |
| 64 | entry.width 40 |
| 65 | label.anchor w |
| 66 | ''') |
| 67 | |
| 68 | self.dlist_dir = copy.copy(os.curdir) |
| 69 | top.ent.entry['textvariable'] = self.dlist_dir |
| 70 | top.btn['command'] = lambda dir=top.dir, ent=top.ent, self=self: \ |
| 71 | self.copy_name(dir,ent) |
| 72 | |
| 73 | top.ent.entry.bind('<Return>', lambda self=self: self.okcmd () ) |
| 74 | |
| 75 | top.pack( expand='yes', fill='both', side=TOP) |
| 76 | top.dir.pack( expand=1, fill=BOTH, padx=4, pady=4, side=LEFT) |
| 77 | top.btn.pack( anchor='s', padx=4, pady=4, side=LEFT) |
| 78 | top.ent.pack( expand=1, fill=X, anchor='s', padx=4, pady=4, side=LEFT) |
| 79 | |
| 80 | # Use a ButtonBox to hold the buttons. |
| 81 | # |
| 82 | box = Tix.ButtonBox (w, orientation='horizontal') |
| 83 | box.add ('ok', text='Ok', underline=0, width=6, |
| 84 | command = lambda self=self: self.okcmd () ) |
| 85 | box.add ('cancel', text='Cancel', underline=0, width=6, |
| 86 | command = lambda self=self: self.quitcmd () ) |
| 87 | |
| 88 | box.pack( anchor='s', fill='x', side=BOTTOM) |
| 89 | |
| 90 | def copy_name (self, dir, ent): |
| 91 | # This should work as it is the entry's textvariable |
| 92 | self.dlist_dir = dir.cget('value') |
| 93 | # but it isn't so I'll do it manually |
| 94 | ent.entry.delete(0,'end') |
| 95 | ent.entry.insert(0, self.dlist_dir) |
| 96 | |
| 97 | def okcmd (self): |
Martin v. Löwis | 8ec03e0 | 2002-03-17 18:19:13 +0000 | [diff] [blame] | 98 | # tixDemo:Status "You have selected the directory" + self.dlist_dir |
Martin v. Löwis | 20efa68 | 2001-11-11 14:07:37 +0000 | [diff] [blame] | 99 | self.quitcmd() |
| 100 | |
| 101 | def quitcmd (self): |
Martin v. Löwis | 8ec03e0 | 2002-03-17 18:19:13 +0000 | [diff] [blame] | 102 | # tixDemo:Status "You have selected the directory" + self.dlist_dir |
| 103 | self.exit = 0 |
| 104 | |
| 105 | def mainloop(self): |
| 106 | while self.exit < 0: |
| 107 | self.root.tk.dooneevent(TCL_ALL_EVENTS) |
| 108 | |
| 109 | def destroy (self): |
Martin v. Löwis | 20efa68 | 2001-11-11 14:07:37 +0000 | [diff] [blame] | 110 | self.root.destroy() |
| 111 | |
| 112 | # This "if" statement makes it possible to run this script file inside or |
Martin v. Löwis | 8ec03e0 | 2002-03-17 18:19:13 +0000 | [diff] [blame] | 113 | # outside of the main demo program "tixwidgets.py". |
Martin v. Löwis | 20efa68 | 2001-11-11 14:07:37 +0000 | [diff] [blame] | 114 | # |
| 115 | if __name__== '__main__' : |
| 116 | root=Tix.Tk() |
| 117 | RunSample(root) |