99# This is a demo program of all Tix widgets available from Python. If
1010# you have installed Python & Tix properly, you can execute this as
1111#
12- # % python tixwidget .py
12+ # % python tixwidgets .py
1313#
1414
15- import os , sys , Tix
15+ import os , os . path , sys , Tix
1616from Tkconstants import *
1717
1818TCL_DONT_WAIT = 1 << 1
@@ -60,16 +60,16 @@ def MkMainMenu(self):
6060 help = Tix .Menubutton (w , text = 'Help' , underline = 0 , takefocus = 0 )
6161 file .pack (side = LEFT )
6262 help .pack (side = RIGHT )
63- fm = Tix .Menu (file )
63+ fm = Tix .Menu (file , tearoff = 0 )
6464 file ['menu' ] = fm
65- hm = Tix .Menu (help )
65+ hm = Tix .Menu (help , tearoff = 0 )
6666 help ['menu' ] = hm
6767
6868 if w .tk .eval ('info commands console' ) == "console" :
6969 fm .add_command (label = 'Console' , underline = 1 ,
7070 command = lambda w = w : w .tk .eval ('console show' ))
7171
72- fm .add_command (label = 'Exit' , underline = 1 , accelerator = 'Ctrl+X' ,
72+ fm .add_command (label = 'Exit' , underline = 1 ,
7373 command = lambda self = self : self .quitcmd () )
7474 hm .add_checkbutton (label = 'BalloonHelp' , underline = 0 , command = ToggleHelp ,
7575 variable = self .useBalloons )
@@ -128,25 +128,43 @@ def build(self):
128128 z .wm_protocol ("WM_DELETE_WINDOW" , lambda self = self : self .quitcmd ())
129129
130130 def quitcmd (self ):
131- # self. root.destroy()
131+ """Quit our mainloop. It is up to you to call root.destroy() after."""
132132 self .exit = 0
133133
134134 def loop (self ):
135+ import tkMessageBox , traceback
136+ while self .exit < 0 :
137+ try :
135138 while self .exit < 0 :
136139 self .root .tk .dooneevent (TCL_ALL_EVENTS )
137- # self.root.tk.dooneevent(TCL_DONT_WAIT)
140+ except SystemExit :
141+ #print 'Exit'
142+ self .exit = 1
143+ break
144+ except KeyboardInterrupt :
145+ if tkMessageBox .askquestion ('Interrupt' , 'Really Quit?' ) == 'yes' :
146+ # self.tk.eval('exit')
147+ return
148+ else :
149+ pass
150+ continue
151+ except :
152+ t , v , tb = sys .exc_info ()
153+ text = ""
154+ for line in traceback .format_exception (t ,v ,tb ):
155+ text += line + '\n '
156+ try : tkMessageBox .showerror ('Error' , text )
157+ except : pass
158+ tkinspect_quit (1 )
138159
139160 def destroy (self ):
140161 self .root .destroy ()
141162
142- def RunMain (top ):
143- global demo , root
163+ def RunMain (root ):
164+ global demo
144165
145- demo = Demo (top )
166+ demo = Demo (root )
146167
147- # top.withdraw()
148- # root = Tix.Toplevel()
149- root = top
150168 demo .build ()
151169 demo .loop ()
152170 demo .destroy ()
@@ -500,17 +518,32 @@ def SList_reset(rh, list):
500518 list .update ()
501519 rh .attach_widget (list )
502520
521+ # See below why this is necessary.
522+ global image1
523+ image1 = None
503524def MkSWindow (w ):
504- global demo
525+ global demo , image1
526+
527+ text = 'The TixScrolledWindow widget allows you to scroll any kind of Tk widget. It is more versatile than a scrolled canvas widget.'
528+
529+ file = os .path .join (demo .dir , 'bitmaps' , 'tix.gif' )
530+ if not os .path .isfile (file ):
531+ text += ' (Image missing)'
505532
506533 top = Tix .Frame (w , width = 330 , height = 330 )
507534 bot = Tix .Frame (w )
508535 msg = Tix .Message (top ,
509536 relief = Tix .FLAT , width = 200 , anchor = Tix .N ,
510- text = 'The TixScrolledWindow widget allows you to scroll any kind of Tk widget. It is more versatile than a scrolled canvas widget.' )
537+ text = text )
538+
511539 win = Tix .ScrolledWindow (top , scrollbar = 'auto' )
512- image = Tix .Image ('photo' , file = demo .dir + "/bitmaps/tix.gif" )
513- lbl = Tix .Label (win .window , image = image )
540+
541+ # This image is not showing up under Python unless it is set to a
542+ # global variable - no problem under Tcl. I assume it is being garbage
543+ # collected some how, even though the tcl command 'image names' shows
544+ # that as far as Tcl is concerned, the image exists and is called pyimage1.
545+ image1 = Tix .Image ('photo' , file = file )
546+ lbl = Tix .Label (win .window , image = image1 )
514547 lbl .pack (expand = 1 , fill = Tix .BOTH )
515548
516549 win .place (x = 30 , y = 150 , width = 190 , height = 120 )
@@ -581,26 +614,27 @@ def MkPanedWindow(w):
581614 msg = Tix .Message (w ,
582615 relief = Tix .FLAT , width = 240 , anchor = Tix .N ,
583616 text = 'The PanedWindow widget allows the user to interactively manipulate the sizes of several panes. The panes can be arranged either vertically or horizontally.' )
584- group = Tix .Label (w , text = 'Newsgroup: comp.lang.python' )
617+ group = Tix .LabelEntry (w , label = 'Newsgroup:' , options = 'entry.width 25' )
618+ group .entry .insert (0 ,'comp.lang.python' )
585619 pane = Tix .PanedWindow (w , orientation = 'vertical' )
586620
587621 p1 = pane .add ('list' , min = 70 , size = 100 )
588622 p2 = pane .add ('text' , min = 70 )
589623 list = Tix .ScrolledListBox (p1 )
590624 text = Tix .ScrolledText (p2 )
591625
592- list .listbox .insert (Tix .END , " 12324 Re: TK is good for your health" )
593- list .listbox .insert (Tix .END , "+ 12325 Re: TK is good for your health" )
594- list .listbox .insert (Tix .END , "+ 12326 Re: Tix is even better for your health (Was: TK is good...)" )
595- list .listbox .insert (Tix .END , " 12327 Re: Tix is even better for your health (Was: TK is good...)" )
596- list .listbox .insert (Tix .END , "+ 12328 Re: Tix is even better for your health (Was: TK is good...)" )
597- list .listbox .insert (Tix .END , " 12329 Re: Tix is even better for your health (Was: TK is good...)" )
598- list .listbox .insert (Tix .END , "+ 12330 Re: Tix is even better for your health (Was: TK is good...)" )
626+ list .listbox .insert (Tix .END , " 12324 Re: Tkinter is good for your health" )
627+ list .listbox .insert (Tix .END , "+ 12325 Re: Tkinter is good for your health" )
628+ list .listbox .insert (Tix .END , "+ 12326 Re: Tix is even better for your health (Was: Tkinter is good...)" )
629+ list .listbox .insert (Tix .END , " 12327 Re: Tix is even better for your health (Was: Tkinter is good...)" )
630+ list .listbox .insert (Tix .END , "+ 12328 Re: Tix is even better for your health (Was: Tkinter is good...)" )
631+ list .listbox .insert (Tix .END , " 12329 Re: Tix is even better for your health (Was: Tkinter is good...)" )
632+ list .listbox .insert (Tix .END , "+ 12330 Re: Tix is even better for your health (Was: Tkinter is good...)" )
599633
600634 text .text ['bg' ] = list .listbox ['bg' ]
601635 text .text ['wrap' ] = 'none'
602636 text .text .insert (Tix .END , """
603- Mon, 19 Jun 1995 11:39:52 comp.lang.tcl Thread 34 of 220
637+ Mon, 19 Jun 1995 11:39:52 comp.lang.python Thread 34 of 220
604638Lines 353 A new way to put text and bitmaps together iNo responses
605639[email protected] Ioi K. Lam at University of Pennsylvania606640
@@ -717,6 +751,7 @@ def MkExFileWidget(w):
717751 'Control' : 'Control' ,
718752 'Notebook' : 'NoteBook' ,
719753 'Option Menu' : 'OptMenu' ,
754+ 'Paned Window' : 'PanedWin' ,
720755 'Popup Menu' : 'PopMenu' ,
721756 'ScrolledHList (1)' : 'SHList1' ,
722757 'ScrolledHList (2)' : 'SHList2' ,
@@ -795,8 +830,8 @@ def MkExFileWidget(w):
795830##
796831## set manager {
797832##na {f ListNoteBook ListNBK.tcl }
798- ## {f NoteBook NoteBook.tcl }
799- ## {f PanedWindow PanedWin.tcl }
833+ ##done {f NoteBook NoteBook.tcl }
834+ ##done {f PanedWindow PanedWin.tcl }
800835## }
801836##
802837## set misc {
@@ -817,7 +852,7 @@ def MkExFileWidget(w):
817852stypes = {}
818853stypes ['widget' ] = ['Balloon' , 'Button Box' , 'Combo Box' , 'Control' ,
819854 'Directory List' , 'Directory Tree' ,
820- 'Notebook' , 'Option Menu' , 'Popup Menu' ,
855+ 'Notebook' , 'Option Menu' , 'Popup Menu' , 'Paned Window' ,
821856 'ScrolledHList (1)' , 'ScrolledHList (2)' , 'Tree (dynamic)' ]
822857stypes ['image' ] = ['Compound Image' ]
823858
@@ -826,43 +861,57 @@ def MkSample(nb, name):
826861 prefix = Tix .OptionName (w )
827862 if not prefix :
828863 prefix = ''
829- w .option_add ('*' + prefix + '*TixLabelFrame*label.padX' , 4 )
864+ else :
865+ prefix = '*' + prefix
866+ w .option_add (prefix + '*TixLabelFrame*label.padX' , 4 )
830867
831- lab = Tix .Label (w , text = 'Select a sample program:' , anchor = Tix .W )
832- lab1 = Tix .Label (w , text = 'Source:' , anchor = Tix .W )
868+ pane = Tix .PanedWindow (w , orientation = 'horizontal' )
869+ pane .pack (side = Tix .TOP , expand = 1 , fill = Tix .BOTH )
870+ f1 = pane .add ('list' , expand = '1' )
871+ f2 = pane .add ('text' , expand = '5' )
872+ f1 ['relief' ] = 'flat'
873+ f2 ['relief' ] = 'flat'
833874
834- slb = Tix .ScrolledHList (w , options = 'listbox.exportSelection 0' )
835- slb .hlist ['command' ] = lambda args = 0 , w = w ,slb = slb : Sample_Action (w , slb , 'run' )
836- slb .hlist ['browsecmd' ] = lambda args = 0 , w = w ,slb = slb : Sample_Action (w , slb , 'browse' )
875+ lab = Tix .Label (f1 , text = 'Select a sample program:' , anchor = Tix .W )
876+ lab .pack (side = Tix .TOP , expand = 0 , fill = Tix .X , padx = 5 , pady = 5 )
877+ lab1 = Tix .Label (f2 , text = 'Source:' , anchor = Tix .W )
878+ lab1 .pack (side = Tix .TOP , expand = 0 , fill = Tix .X , padx = 5 , pady = 5 )
837879
838- stext = Tix .ScrolledText (w , name = 'stext' )
880+ slb = Tix .Tree (f1 , options = 'hlist.width 25' )
881+ slb .pack (side = Tix .TOP , expand = 1 , fill = Tix .BOTH , padx = 5 )
882+
883+ stext = Tix .ScrolledText (f2 , name = 'stext' )
839884 font = root .tk .eval ('tix option get fixed_font' )
840885 stext .text .config (font = font )
841- # stext.text.bind('<1>', stext.text.focus())
842886 stext .text .bind ('<Up>' , lambda w = stext .text : w .yview (scroll = '-1 unit' ))
843887 stext .text .bind ('<Down>' , lambda w = stext .text : w .yview (scroll = '1 unit' ))
844888 stext .text .bind ('<Left>' , lambda w = stext .text : w .xview (scroll = '-1 unit' ))
845889 stext .text .bind ('<Right>' , lambda w = stext .text : w .xview (scroll = '1 unit' ))
890+ stext .pack (side = Tix .TOP , expand = 1 , fill = Tix .BOTH , padx = 7 )
846891
847- run = Tix .Button ( w , text = 'Run ...' , name = 'run' , command = lambda args = 0 , w = w , slb = slb : Sample_Action ( w , slb , 'run' ) )
848- view = Tix . Button ( w , text = 'View Source ...' , name = 'view' , command = lambda args = 0 , w = w , slb = slb : Sample_Action ( w , slb , 'view' ) )
892+ frame = Tix .Frame ( f2 , name = 'frame' )
893+ frame . pack ( side = Tix . TOP , expand = 0 , fill = Tix . X , padx = 7 )
849894
850- lab .form (top = 0 , left = 0 , right = '&' + str (slb ))
851- slb .form (left = 0 , top = lab , bottom = - 4 )
852- lab1 .form (left = '&' + str (stext ), top = 0 , right = '&' + str (stext ), bottom = stext )
853- run .form (left = str (slb )+ ' 30' , bottom = - 4 )
854- view .form (left = run , bottom = - 4 )
855- stext .form (bottom = str (run )+ ' -5' , left = '&' + str (run ), right = '-0' , top = '&' + str (slb ))
895+ run = Tix .Button (frame , text = 'Run ...' , name = 'run' )
896+ view = Tix .Button (frame , text = 'View Source ...' , name = 'view' )
897+ run .pack (side = Tix .LEFT , expand = 0 , fill = Tix .NONE )
898+ view .pack (side = Tix .LEFT , expand = 0 , fill = Tix .NONE )
856899
857900 stext .text ['bg' ] = slb .hlist ['bg' ]
858901 stext .text ['state' ] = 'disabled'
859902 stext .text ['wrap' ] = 'none'
903+ stext .text ['width' ] = 80
860904
861905 slb .hlist ['separator' ] = '.'
862906 slb .hlist ['width' ] = 25
863907 slb .hlist ['drawbranch' ] = 0
864908 slb .hlist ['indent' ] = 10
865909 slb .hlist ['wideselect' ] = 1
910+ slb .hlist ['command' ] = lambda args = 0 , w = w ,slb = slb ,stext = stext ,run = run ,view = view : Sample_Action (w , slb , stext , run , view , 'run' )
911+ slb .hlist ['browsecmd' ] = lambda args = 0 , w = w ,slb = slb ,stext = stext ,run = run ,view = view : Sample_Action (w , slb , stext , run , view , 'browse' )
912+
913+ run ['command' ] = lambda args = 0 , w = w ,slb = slb ,stext = stext ,run = run ,view = view : Sample_Action (w , slb , stext , run , view , 'run' )
914+ view ['command' ] = lambda args = 0 , w = w ,slb = slb ,stext = stext ,run = run ,view = view : Sample_Action (w , slb , stext , run , view , 'view' )
866915
867916 for type in ['widget' , 'image' ]:
868917 if type != 'widget' :
@@ -879,13 +928,9 @@ def MkSample(nb, name):
879928 run ['state' ] = 'disabled'
880929 view ['state' ] = 'disabled'
881930
882- def Sample_Action (w , slb , action ):
931+ def Sample_Action (w , slb , stext , run , view , action ):
883932 global demo
884933
885- run = w ._nametowidget (str (w ) + '.run' )
886- view = w ._nametowidget (str (w ) + '.view' )
887- stext = w ._nametowidget (str (w ) + '.stext' )
888-
889934 hlist = slb .hlist
890935 anchor = hlist .info_anchor ()
891936 if not anchor :
0 commit comments