1 # -*-mode: python; fill-column: 75; tab-width: 8; coding: iso-latin-1-unix -*-
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 "tixwidgets.py": 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.
20 from Tkconstants
import *
25 dirlist
= DemoDirList(root
)
30 def __init__(self
, w
):
34 z
= w
.winfo_toplevel()
35 z
.wm_protocol("WM_DELETE_WINDOW", lambda self
=self
: self
.quitcmd())
37 # Create the tixDirList and the tixLabelEntry widgets on the on the top
40 # bg = root.tk.eval('tix option get bg')
41 # adding bg=bg crashes Windows pythonw tk8.3.3 Python 2.1.0
43 top
= Tix
.Frame( w
, relief
=RAISED
, bd
=1)
45 # Create the DirList widget. By default it will show the current
49 top
.dir = Tix
.DirList(top
)
50 top
.dir.hlist
['width'] = 40
52 # When the user presses the ".." button, the selected directory
53 # is "transferred" into the entry widget
55 top
.btn
= Tix
.Button(top
, text
= " >> ", pady
= 0)
57 # We use a LabelEntry to hold the installation directory. The user
58 # can choose from the DirList widget, or he can type in the directory
61 top
.ent
= Tix
.LabelEntry(top
, label
="Installation Directory:",
68 font
= self
.root
.tk
.eval('tix option get fixed_font')
69 # font = self.root.master.tix_option_get('fixed_font')
70 top
.ent
.entry
['font'] = font
72 self
.dlist_dir
= copy
.copy(os
.curdir
)
73 # This should work setting the entry's textvariable
74 top
.ent
.entry
['textvariable'] = self
.dlist_dir
75 top
.btn
['command'] = lambda dir=top
.dir, ent
=top
.ent
, self
=self
: \
76 self
.copy_name(dir,ent
)
78 # top.ent.entry.insert(0,'tix'+repr(self))
79 top
.ent
.entry
.bind('<Return>', lambda self
=self
: self
.okcmd () )
81 top
.pack( expand
='yes', fill
='both', side
=TOP
)
82 top
.dir.pack( expand
=1, fill
=BOTH
, padx
=4, pady
=4, side
=LEFT
)
83 top
.btn
.pack( anchor
='s', padx
=4, pady
=4, side
=LEFT
)
84 top
.ent
.pack( expand
=1, fill
=X
, anchor
='s', padx
=4, pady
=4, side
=LEFT
)
86 # Use a ButtonBox to hold the buttons.
88 box
= Tix
.ButtonBox (w
, orientation
='horizontal')
89 box
.add ('ok', text
='Ok', underline
=0, width
=6,
90 command
= lambda self
=self
: self
.okcmd () )
91 box
.add ('cancel', text
='Cancel', underline
=0, width
=6,
92 command
= lambda self
=self
: self
.quitcmd () )
94 box
.pack( anchor
='s', fill
='x', side
=BOTTOM
)
96 def copy_name (self
, dir, ent
):
97 # This should work as it is the entry's textvariable
98 self
.dlist_dir
= dir.cget('value')
99 # but it isn't so I'll do it manually
100 ent
.entry
.delete(0,'end')
101 ent
.entry
.insert(0, self
.dlist_dir
)
104 # tixDemo:Status "You have selected the directory" + self.dlist_dir
112 self
.root
.tk
.dooneevent(TCL_ALL_EVENTS
)
117 # This "if" statement makes it possible to run this script file inside or
118 # outside of the main demo program "tixwidgets.py".
120 if __name__
== '__main__' :
121 import tkMessageBox
, traceback
127 t
, v
, tb
= sys
.exc_info()
128 text
= "Error running the demo script:\n"
129 for line
in traceback
.format_exception(t
,v
,tb
):
130 text
= text
+ line
+ '\n'
131 d
= tkMessageBox
.showerror ( 'Tix Demo Error', text
)