blob: 7bf813c321e05968d60f788aa92d95164f039f88 [file] [log] [blame]
Martin v. Löwis20efa682001-11-11 14:07:37 +00001# -*-mode: python; fill-column: 75; tab-width: 8; coding: iso-latin-1-unix -*-
2#
3# $Id$
4#
5# Tix Demostration Program
6#
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.
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
19import Tix, os, copy
20from Tkconstants import *
21
22def RunSample (w):
23 DemoDirTree(w)
24
25class DemoDirTree:
26 def __init__(self, w):
27 self.root = w
28
29 z = w.winfo_toplevel()
30 z.wm_title('Tix.DirTree Widget Demo')
31
32 # Create the tixDirTree and the tixLabelEntry widgets on the on the top
33 # of the dialog box
34
35 # bg = root.tk.eval('tix option get bg')
36 # adding bg=bg crashes Windows pythonw tk8.3.3 Python 2.1.0
37
38 top = Tix.Frame( w, relief=RAISED, bd=1)
39
40 # Create the DirTree widget. By default it will show the current
41 # directory
42 #
43 #
44 top.dir = Tix.DirTree(top)
45 top.dir.hlist['width'] = 40
46
47 # When the user presses the ".." button, the selected directory
48 # is "transferred" into the entry widget
49 #
50 top.btn = Tix.Button(top, text = " >> ", pady = 0)
51
52 # We use a LabelEntry to hold the installation directory. The user
53 # can choose from the DirTree widget, or he can type in the directory
54 # manually
55 #
56 top.ent = Tix.LabelEntry(top, label="Installation Directory:",
57 labelside = 'top',
58 options = '''
59 entry.width 40
60 label.anchor w
61 ''')
62
63 self.dlist_dir = copy.copy(os.curdir)
64 top.ent.entry['textvariable'] = self.dlist_dir
65 top.btn['command'] = lambda dir=top.dir, ent=top.ent, self=self: \
66 self.copy_name(dir,ent)
67
68 top.ent.entry.bind('<Return>', lambda self=self: self.okcmd () )
69
70 top.pack( expand='yes', fill='both', side=TOP)
71 top.dir.pack( expand=1, fill=BOTH, padx=4, pady=4, side=LEFT)
72 top.btn.pack( anchor='s', padx=4, pady=4, side=LEFT)
73 top.ent.pack( expand=1, fill=X, anchor='s', padx=4, pady=4, side=LEFT)
74
75 # Use a ButtonBox to hold the buttons.
76 #
77 box = Tix.ButtonBox (w, orientation='horizontal')
78 box.add ('ok', text='Ok', underline=0, width=6,
79 command = lambda self=self: self.okcmd () )
80 box.add ('cancel', text='Cancel', underline=0, width=6,
81 command = lambda self=self: self.quitcmd () )
82
83 box.pack( anchor='s', fill='x', side=BOTTOM)
84
85 def copy_name (self, dir, ent):
86 # This should work as it is the entry's textvariable
87 self.dlist_dir = dir.cget('value')
88 # but it isn't so I'll do it manually
89 ent.entry.delete(0,'end')
90 ent.entry.insert(0, self.dlist_dir)
91
92 def okcmd (self):
93 # tixDemo:Status "You have selected the directory" + $self.dlist_dir
94
95 self.quitcmd()
96
97 def quitcmd (self):
98 self.root.destroy()
99
100# This "if" statement makes it possible to run this script file inside or
101# outside of the main demo program "widget".
102#
103if __name__== '__main__' :
104 root=Tix.Tk()
105 RunSample(root)
106 root.mainloop()
107 root.destroy()
108