Updated for hfsplus module, new gusi libs.
[python/dscho.git] / Demo / tix / samples / DirList.py
blob3064768b41e614c55804cb7f1813d991ccc99e85
1 # -*-mode: python; fill-column: 75; tab-width: 8; coding: iso-latin-1-unix -*-
3 # $Id$
5 # Tix Demostration Program
7 # This sample program is structured in such a way so that it can be
8 # executed from the Tix demo program "widget": it must have a
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.
13 # This file demonstrates the use of the tixDirList widget -- you can
14 # use it for the user to select a directory. For example, an installation
15 # program can use the tixDirList widget to ask the user to select the
16 # installation directory for an application.
19 import Tix, os, copy
20 from Tkconstants import *
22 TCL_DONT_WAIT = 1<<1
23 TCL_WINDOW_EVENTS = 1<<2
24 TCL_FILE_EVENTS = 1<<3
25 TCL_TIMER_EVENTS = 1<<4
26 TCL_IDLE_EVENTS = 1<<5
27 TCL_ALL_EVENTS = 0
29 def RunSample (root):
30 global dirlist
31 dirlist = DemoDirList(root)
32 dirlist.mainloop()
33 dirlist.destroy()
35 class DemoDirList:
36 def __init__(self, w):
37 self.root = w
38 self.exit = -1
40 z = w.winfo_toplevel()
41 z.wm_title('Tix.DirList Widget Demo')
43 # Create the tixDirList and the tixLabelEntry widgets on the on the top
44 # of the dialog box
46 # bg = root.tk.eval('tix option get bg')
47 # adding bg=bg crashes Windows pythonw tk8.3.3 Python 2.1.0
49 top = Tix.Frame( w, relief=RAISED, bd=1)
51 # Create the DirList widget. By default it will show the current
52 # directory
55 top.dir = Tix.DirList(top)
56 top.dir.hlist['width'] = 40
58 # When the user presses the ".." button, the selected directory
59 # is "transferred" into the entry widget
61 top.btn = Tix.Button(top, text = " >> ", pady = 0)
63 # We use a LabelEntry to hold the installation directory. The user
64 # can choose from the DirList widget, or he can type in the directory
65 # manually
67 top.ent = Tix.LabelEntry(top, label="Installation Directory:",
68 labelside = 'top',
69 options = '''
70 entry.width 40
71 label.anchor w
72 ''')
74 font = self.root.tk.eval('tix option get fixed_font')
75 # font = self.root.master.tix_option_get('fixed_font')
76 top.ent.entry['font'] = font
78 self.dlist_dir = copy.copy(os.curdir)
79 # This should work setting the entry's textvariable
80 top.ent.entry['textvariable'] = self.dlist_dir
81 top.btn['command'] = lambda dir=top.dir, ent=top.ent, self=self: \
82 self.copy_name(dir,ent)
84 # top.ent.entry.insert(0,'tix'+`self`)
85 top.ent.entry.bind('<Return>', lambda self=self: self.okcmd () )
87 top.pack( expand='yes', fill='both', side=TOP)
88 top.dir.pack( expand=1, fill=BOTH, padx=4, pady=4, side=LEFT)
89 top.btn.pack( anchor='s', padx=4, pady=4, side=LEFT)
90 top.ent.pack( expand=1, fill=X, anchor='s', padx=4, pady=4, side=LEFT)
92 # Use a ButtonBox to hold the buttons.
94 box = Tix.ButtonBox (w, orientation='horizontal')
95 box.add ('ok', text='Ok', underline=0, width=6,
96 command = lambda self=self: self.okcmd () )
97 box.add ('cancel', text='Cancel', underline=0, width=6,
98 command = lambda self=self: self.quitcmd () )
100 box.pack( anchor='s', fill='x', side=BOTTOM)
101 z.wm_protocol("WM_DELETE_WINDOW", lambda self=self: self.quitcmd())
103 def copy_name (self, dir, ent):
104 # This should work as it is the entry's textvariable
105 self.dlist_dir = dir.cget('value')
106 # but it isn't so I'll do it manually
107 ent.entry.delete(0,'end')
108 ent.entry.insert(0, self.dlist_dir)
110 def okcmd (self):
111 # tixDemo:Status "You have selected the directory" + $self.dlist_dir
113 self.quitcmd()
115 def quitcmd (self):
116 # self.root.destroy()
117 self.exit = 0
119 def mainloop(self):
120 while self.exit < 0:
121 self.root.tk.dooneevent(TCL_ALL_EVENTS)
122 # self.root.tk.dooneevent(TCL_DONT_WAIT)
124 def destroy (self):
125 self.root.destroy()
127 # This "if" statement makes it possible to run this script file inside or
128 # outside of the main demo program "widget".
130 if __name__== '__main__' :
131 import tkMessageBox, traceback
133 try:
134 root=Tix.Tk()
135 RunSample(root)
136 except:
137 t, v, tb = sys.exc_info()
138 text = "Error running the demo script:\n"
139 for line in traceback.format_exception(t,v,tb):
140 text = text + line + '\n'
141 d = tkMessageBox.showerror ( 'Tix Demo Error', text)